OutputRedirection
AsyncProcess.swift:176enum OutputRedirection
enum OutputRedirection
import Basics
final class AsyncProcess
Process allows spawning new subprocesses and working with them.
case none
Do not redirect the output
case collect(redirectStderr: Bool)
Collect stdout and stderr output and provide it back via ProcessResult object. If redirectStderr is true, stderr be redirected to stdout.
case stream(stdout: OutputClosure, stderr: OutputClosure, redirectStderr: Bool)
Stream stdout and stderr via the corresponding closures. If redirectStderr is true, stderr be redirected to stdout.
convenience init(args: String..., environment: Environment = .current, outputRedirection: OutputRedirection = .collect, loggingHandler: LoggingHandler? = .none)
init(arguments: [String], environment: Environment = .current, outputRedirection: OutputRedirection = .collect, startNewProcessGroup: Bool = true, loggingHandler: LoggingHandler? = .none)
Create a new process instance.
init(arguments: [String], environment: Environment = .current, workingDirectory: AbsolutePath, outputRedirection: OutputRedirection = .collect, startNewProcessGroup: Bool = true, loggingHandler: LoggingHandler? = .none)
Create a new process instance.
@discardableResult static func checkNonZeroExit(args: String..., environment: Environment = .current, loggingHandler: LoggingHandler? = .none) throws -> String
Execute a subprocess and get its (UTF-8) output if it has a non zero exit.
@discardableResult static func checkNonZeroExit(args: String..., environment: Environment = .current, loggingHandler: LoggingHandler? = .none) async throws -> String
Execute a subprocess and get its (UTF-8) output if it has a non zero exit.
@discardableResult static func checkNonZeroExit(arguments: [String], environment: Environment = .current, loggingHandler: LoggingHandler? = .none) throws -> String
Execute a subprocess and get its (UTF-8) output if it has a non zero exit.
@discardableResult static func checkNonZeroExit(arguments: [String], environment: Environment = .current, loggingHandler: LoggingHandler? = .none) async throws -> String
Execute a subprocess and get its (UTF-8) output if it has a non zero exit.
static func findExecutable(_ program: String, workingDirectory: AbsolutePath? = nil) -> AbsolutePath?
Returns the path of the the given program if found in the search paths.
@discardableResult static func popen(args: String..., environment: Environment = .current, loggingHandler: LoggingHandler? = .none) throws -> AsyncProcessResult
Execute a subprocess and block until it finishes execution
static func popen(args: String..., environment: Environment = .current, loggingHandler: LoggingHandler? = .none) async throws -> AsyncProcessResult
Execute a subprocess and returns the result when it finishes execution
@discardableResult static func popen(arguments: [String], environment: Environment = .current, loggingHandler: LoggingHandler? = .none) throws -> AsyncProcessResult
Execute a subprocess and block until it finishes execution
static func popen(arguments: [String], environment: Environment = .current, loggingHandler: LoggingHandler? = .none) async throws -> AsyncProcessResult
Execute a subprocess and returns the result when it finishes execution
static func popen(arguments: [String], environment: Environment = .current, loggingHandler: LoggingHandler? = .none, queue: DispatchQueue? = nil, completion: @escaping (Result<AsyncProcessResult, Swift.Error>) -> Void)
Execute a subprocess and calls completion block when it finishes execution
let arguments: [String]
The arguments to execute.
let environment: Environment
var launched: Bool { get }
let loggingHandler: LoggingHandler?
let outputRedirection: OutputRedirection
How process redirects its output.
var processID: AsyncProcess.ProcessID { get }
let workingDirectory: AbsolutePath?
The path to the directory under which to run the process.
static func == (lhs: AsyncProcess, rhs: AsyncProcess) -> Bool
func hash(into hasher: inout Hasher)
@discardableResult func launch() throws -> WritableByteStream
Launch the subprocess. Returns a WritableByteStream object that can be used to communicate to the process’s stdin. If needed, the stream can be closed using the close() API. Otherwise, the stream will be closed automatically.
func signal(_ signal: Int32)
Send a signal to the process.
@discardableResult func waitUntilExit() throws -> AsyncProcessResult
Blocks the calling process until the subprocess finishes execution.
@discardableResult func waitUntilExit() async throws -> AsyncProcessResult
Executes the process I/O state machine, returning the result when finished.
enum Error
Errors when attempting to invoke a process
typealias LoggingHandler = (String) -> Void
Typealias for logging handling closure
typealias OutputClosure = ([UInt8]) -> Void
Typealias for stdout/stderr output closure.
typealias ProcessID = pid_t
static var loggingHandler: LoggingHandler? { get set }
Global logging handler. Use with care! preferably use instance level instead of setting one globally.
static let collect: OutputRedirection
Default collect OutputRedirection that defaults to not redirect stderr. Provided for API compatibility.
static func stream(stdout: @escaping OutputClosure, stderr: @escaping OutputClosure) -> AsyncProcess.OutputRedirection
Default stream OutputRedirection that defaults to not redirect stderr. Provided for API compatibility.
var outputClosures: (stdoutClosure: OutputClosure, stderrClosure: OutputClosure)? { get }
var redirectStderr: Bool { get }
var redirectsOutput: Bool { get }