Support for executing subprocesses, using Foundation.Process, and capturing their output asynchronously. Swift 6 ready.
Currently doesn't support a STDIN stream -- only STDOUT and STDERR -- but it should be easy enough to add when I (or someone else?) hit a use case.
Usage examples:
let url = /* url to the executable */
let runner = Runner(for: url)
// execute with some arguments
let session = runner.run(["some", "arguments"])
// process the output asynchronously
for await l in session.stdout.lines {
print(l)
}
// run in a different working directory
runner.cwd = /* url to the directory */
let _ = runner.run(["blah"])
// transfer execution to the subprocess
runner.exec(url)
let runner = Runner(command: "git") /// we'll find git in $PATH if it's there
let session = runner.run("status")
print(await session.stdout.string)
let url = /* url to the executable */
let runner = Runner(for: url)
// execute with some arguments
let session = runner.run(["some", "arguments"])
// wait for termination and read state
if await session.waitUntilExit() == .succeeded {
print("all good")
}
let url = /* url to the executable */
let runner = Runner(for: url)
let session = runner.run(stdoutMode: .forward, stderrMode: .forward)
let _ = session.waitUntilExit()