AsyncProcess
Process allows spawning new subprocesses and working with them.
final class AsyncProcess
Note: This class is thread safe.
Process allows spawning new subprocesses and working with them.
final class AsyncProcess
Note: This class is thread safe.
import Basics
protocol Copyable
A type whose values can be implicitly or explicitly copied.
protocol Equatable
A type that can be compared for value equality.
protocol Escapable
protocol Hashable : Equatable
A type that can be hashed into a Hasher
to produce an integer hash value.
protocol Sendable
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
enum OutputRedirection
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 func != (lhs: Self, rhs: Self) -> Bool
Returns a Boolean value indicating whether two values are not equal.