restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
import Foundation
|
||||
|
||||
public struct ProcessResult: Sendable, Equatable {
|
||||
public var exitCode: Int32
|
||||
public var stdout: String
|
||||
public var stderr: String
|
||||
|
||||
public init(exitCode: Int32, stdout: String, stderr: String) {
|
||||
self.exitCode = exitCode
|
||||
self.stdout = stdout
|
||||
self.stderr = stderr
|
||||
}
|
||||
|
||||
public var succeeded: Bool { exitCode == 0 }
|
||||
}
|
||||
|
||||
public enum ProcessRunnerError: Error, Equatable {
|
||||
case executableNotFound(String)
|
||||
case launchFailed(String)
|
||||
}
|
||||
|
||||
/// Runs a command to completion. Services depend on this protocol so tests can script the output.
|
||||
public protocol ProcessRunning: Sendable {
|
||||
/// Runs `executable` (a name looked up on the search path, or a path) and collects its output.
|
||||
/// A non-zero exit is a normal result, not an error; `environment: nil` inherits ours.
|
||||
func run(_ executable: String, arguments: [String], in directory: URL?, environment: [String: String]?) async throws -> ProcessResult
|
||||
}
|
||||
|
||||
extension ProcessRunning {
|
||||
public func run(_ executable: String, _ arguments: [String], in directory: URL? = nil) async throws -> ProcessResult {
|
||||
try await run(executable, arguments: arguments, in: directory, environment: nil)
|
||||
}
|
||||
}
|
||||
|
||||
public struct ProcessRunner: ProcessRunning {
|
||||
public let locator: ExecutableLocator
|
||||
|
||||
public init(locator: ExecutableLocator = .standard) {
|
||||
self.locator = locator
|
||||
}
|
||||
|
||||
public func run(_ executable: String, arguments: [String], in directory: URL?, environment: [String: String]?) async throws -> ProcessResult {
|
||||
guard let url = locator.locate(executable) else { throw ProcessRunnerError.executableNotFound(executable) }
|
||||
let job = Job(url: url, arguments: arguments, directory: directory, environment: environment)
|
||||
return try await withTaskCancellationHandler {
|
||||
try await withCheckedThrowingContinuation { continuation in
|
||||
job.start(continuation)
|
||||
}
|
||||
} onCancel: {
|
||||
job.cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One child process. Both pipes are drained on their own threads, because a child that
|
||||
/// fills one pipe while we wait on the other would deadlock.
|
||||
private final class Job: @unchecked Sendable {
|
||||
private let process = Process()
|
||||
private let lock = NSLock()
|
||||
private var cancelled = false
|
||||
|
||||
init(url: URL, arguments: [String], directory: URL?, environment: [String: String]?) {
|
||||
process.executableURL = url
|
||||
process.arguments = arguments
|
||||
if let directory { process.currentDirectoryURL = directory }
|
||||
if let environment { process.environment = environment }
|
||||
process.standardInput = FileHandle.nullDevice
|
||||
}
|
||||
|
||||
func start(_ continuation: CheckedContinuation<ProcessResult, Error>) {
|
||||
let out = Pipe()
|
||||
let err = Pipe()
|
||||
process.standardOutput = out
|
||||
process.standardError = err
|
||||
|
||||
let group = DispatchGroup()
|
||||
var outData = Data()
|
||||
var errData = Data()
|
||||
group.enter()
|
||||
process.terminationHandler = { _ in group.leave() }
|
||||
|
||||
lock.lock()
|
||||
let alreadyCancelled = cancelled
|
||||
do {
|
||||
if !alreadyCancelled { try process.run() }
|
||||
} catch {
|
||||
lock.unlock()
|
||||
continuation.resume(throwing: ProcessRunnerError.launchFailed(error.localizedDescription))
|
||||
return
|
||||
}
|
||||
lock.unlock()
|
||||
if alreadyCancelled {
|
||||
continuation.resume(throwing: CancellationError())
|
||||
return
|
||||
}
|
||||
|
||||
group.enter()
|
||||
DispatchQueue.global().async {
|
||||
outData = out.fileHandleForReading.readDataToEndOfFile()
|
||||
group.leave()
|
||||
}
|
||||
group.enter()
|
||||
DispatchQueue.global().async {
|
||||
errData = err.fileHandleForReading.readDataToEndOfFile()
|
||||
group.leave()
|
||||
}
|
||||
group.notify(queue: .global()) { [self, process] in
|
||||
if wasCancelled {
|
||||
continuation.resume(throwing: CancellationError())
|
||||
return
|
||||
}
|
||||
continuation.resume(returning: ProcessResult(
|
||||
exitCode: process.terminationStatus,
|
||||
stdout: String(decoding: outData, as: UTF8.self),
|
||||
stderr: String(decoding: errData, as: UTF8.self)
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
private var wasCancelled: Bool {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
return cancelled
|
||||
}
|
||||
|
||||
func cancel() {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
cancelled = true
|
||||
if process.isRunning { process.terminate() }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user