11import Foundation
2+ #if canImport(FoundationNetworking)
3+ import FoundationNetworking
4+ #endif
25
36public enum SpawnError : Error {
47 case commandFailed( command: String , exitCode: Int32 , stdout: String , stderr: String )
@@ -62,16 +65,19 @@ public struct ShellExecutor: ShellExecuting {
6265 with: arguments,
6366 environmentVariables: environmentVariables,
6467 outputFile: outputFile)
68+ do {
69+ let pipe = Pipe ( )
70+ task. standardOutput = pipe
71+ try task. run ( )
6572
66- let pipe = Pipe ( )
67- task. standardOutput = pipe
68- task. launch ( )
73+ let data = pipe. fileHandleForReading. readDataToEndOfFile ( )
6974
70- let data = pipe . fileHandleForReading . readDataToEndOfFile ( )
75+ task . waitUntilExit ( )
7176
72- task. waitUntilExit ( )
73-
74- return String ( data: data, encoding: . utf8) !. trimmingCharacters ( in: . whitespacesAndNewlines)
77+ return String ( data: data, encoding: . utf8) !. trimmingCharacters ( in: . whitespacesAndNewlines)
78+ } catch {
79+ return error. localizedDescription
80+ }
7581 }
7682
7783 // Similar to above, but can throw, and throws with most of
@@ -90,7 +96,7 @@ public struct ShellExecutor: ShellExecuting {
9096 task. standardOutput = stdout
9197 let stderr = Pipe ( )
9298 task. standardError = stderr
93- task. launch ( )
99+ try task. run ( )
94100
95101 // Pull out the STDOUT as a string because we'll need that regardless
96102 let stdoutData = stdout. fileHandleForReading. readDataToEndOfFile ( )
@@ -131,10 +137,10 @@ public struct ShellExecutor: ShellExecuting {
131137 let script = " \( command) \( arguments. joined ( separator: " " ) ) " + scriptOutputFile
132138
133139 let task = Process ( )
134- task. launchPath = " /bin/sh "
140+ task. executableURL = URL ( fileURLWithPath : " /bin/sh " )
135141 task. arguments = [ " -c " , script]
136142 task. environment = mergeEnvs ( localEnv: environmentVariables, processEnv: ProcessInfo . processInfo. environment)
137- task. currentDirectoryPath = FileManager . default. currentDirectoryPath
143+ task. currentDirectoryURL = URL ( fileURLWithPath : FileManager . default. currentDirectoryPath)
138144 return task
139145 }
140146
0 commit comments