@@ -20,6 +20,7 @@ import Dispatch
20
20
* (at the moment this is only supported on macOS)
21
21
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
22
22
* (at the moment this is only supported on macOS)
23
+ * - parameter environment: The environment for the command.
23
24
*
24
25
* - returns: The output of running the command
25
26
* - throws: `ShellOutError` in case the command couldn't be performed, or it returned an error
@@ -33,14 +34,16 @@ import Dispatch
33
34
at path: String = " . " ,
34
35
process: Process = . init( ) ,
35
36
outputHandle: FileHandle ? = nil ,
36
- errorHandle: FileHandle ? = nil
37
+ errorHandle: FileHandle ? = nil ,
38
+ environment: [ String : String ] ? = nil
37
39
) throws -> String {
38
40
let command = " cd \( path. escapingSpaces) && \( command) \( arguments. joined ( separator: " " ) ) "
39
41
40
42
return try process. launchBash (
41
43
with: command,
42
44
outputHandle: outputHandle,
43
- errorHandle: errorHandle
45
+ errorHandle: errorHandle,
46
+ environment: environment
44
47
)
45
48
}
46
49
@@ -54,6 +57,7 @@ import Dispatch
54
57
* (at the moment this is only supported on macOS)
55
58
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
56
59
* (at the moment this is only supported on macOS)
60
+ * - parameter environment: The environment for the command.
57
61
*
58
62
* - returns: The output of running the command
59
63
* - throws: `ShellOutError` in case the command couldn't be performed, or it returned an error
@@ -66,7 +70,8 @@ import Dispatch
66
70
at path: String = " . " ,
67
71
process: Process = . init( ) ,
68
72
outputHandle: FileHandle ? = nil ,
69
- errorHandle: FileHandle ? = nil
73
+ errorHandle: FileHandle ? = nil ,
74
+ environment: [ String : String ] ? = nil
70
75
) throws -> String {
71
76
let command = commands. joined ( separator: " && " )
72
77
@@ -75,7 +80,8 @@ import Dispatch
75
80
at: path,
76
81
process: process,
77
82
outputHandle: outputHandle,
78
- errorHandle: errorHandle
83
+ errorHandle: errorHandle,
84
+ environment: environment
79
85
)
80
86
}
81
87
@@ -87,6 +93,7 @@ import Dispatch
87
93
* - parameter process: Which process to use to perform the command (default: A new one)
88
94
* - parameter outputHandle: Any `FileHandle` that any output (STDOUT) should be redirected to
89
95
* - parameter errorHandle: Any `FileHandle` that any error output (STDERR) should be redirected to
96
+ * - parameter environment: The environment for the command.
90
97
*
91
98
* - returns: The output of running the command
92
99
* - throws: `ShellOutError` in case the command couldn't be performed, or it returned an error
@@ -101,14 +108,16 @@ import Dispatch
101
108
at path: String = " . " ,
102
109
process: Process = . init( ) ,
103
110
outputHandle: FileHandle ? = nil ,
104
- errorHandle: FileHandle ? = nil
111
+ errorHandle: FileHandle ? = nil ,
112
+ environment: [ String : String ] ? = nil
105
113
) throws -> String {
106
114
return try shellOut (
107
115
to: command. string,
108
116
at: path,
109
117
process: process,
110
118
outputHandle: outputHandle,
111
- errorHandle: errorHandle
119
+ errorHandle: errorHandle,
120
+ environment: environment
112
121
)
113
122
}
114
123
@@ -397,10 +406,14 @@ extension ShellOutError: LocalizedError {
397
406
// MARK: - Private
398
407
399
408
private extension Process {
400
- @discardableResult func launchBash( with command: String , outputHandle: FileHandle ? = nil , errorHandle: FileHandle ? = nil ) throws -> String {
409
+ @discardableResult func launchBash( with command: String , outputHandle: FileHandle ? = nil , errorHandle: FileHandle ? = nil , environment : [ String : String ] ? = nil ) throws -> String {
401
410
launchPath = " /bin/bash "
402
411
arguments = [ " -c " , command]
403
412
413
+ if let environment = environment {
414
+ self . environment = environment
415
+ }
416
+
404
417
// Because FileHandle's readabilityHandler might be called from a
405
418
// different queue from the calling queue, avoid a data race by
406
419
// protecting reads and writes to outputData and errorData on
0 commit comments