1
1
import Foundation
2
+ #if canImport(FoundationNetworking)
3
+ import FoundationNetworking
4
+ #endif
2
5
3
6
public enum SpawnError : Error {
4
7
case commandFailed( command: String , exitCode: Int32 , stdout: String , stderr: String )
@@ -62,16 +65,19 @@ public struct ShellExecutor: ShellExecuting {
62
65
with: arguments,
63
66
environmentVariables: environmentVariables,
64
67
outputFile: outputFile)
68
+ do {
69
+ let pipe = Pipe ( )
70
+ task. standardOutput = pipe
71
+ try task. run ( )
65
72
66
- let pipe = Pipe ( )
67
- task. standardOutput = pipe
68
- task. launch ( )
73
+ let data = pipe. fileHandleForReading. readDataToEndOfFile ( )
69
74
70
- let data = pipe . fileHandleForReading . readDataToEndOfFile ( )
75
+ task . waitUntilExit ( )
71
76
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
+ }
75
81
}
76
82
77
83
// Similar to above, but can throw, and throws with most of
@@ -90,7 +96,7 @@ public struct ShellExecutor: ShellExecuting {
90
96
task. standardOutput = stdout
91
97
let stderr = Pipe ( )
92
98
task. standardError = stderr
93
- task. launch ( )
99
+ try task. run ( )
94
100
95
101
// Pull out the STDOUT as a string because we'll need that regardless
96
102
let stdoutData = stdout. fileHandleForReading. readDataToEndOfFile ( )
@@ -131,10 +137,10 @@ public struct ShellExecutor: ShellExecuting {
131
137
let script = " \( command) \( arguments. joined ( separator: " " ) ) " + scriptOutputFile
132
138
133
139
let task = Process ( )
134
- task. launchPath = " /bin/sh "
140
+ task. executableURL = URL ( fileURLWithPath : " /bin/sh " )
135
141
task. arguments = [ " -c " , script]
136
142
task. environment = mergeEnvs ( localEnv: environmentVariables, processEnv: ProcessInfo . processInfo. environment)
137
- task. currentDirectoryPath = FileManager . default. currentDirectoryPath
143
+ task. currentDirectoryURL = URL ( fileURLWithPath : FileManager . default. currentDirectoryPath)
138
144
return task
139
145
}
140
146
0 commit comments