/
Script.swift
325 lines (280 loc) · 8.21 KB
/
Script.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import Shwift
import Dispatch
import SystemPackage
@_implementationOnly import class Foundation.FileManager
/**
We consider the following to be part of our public API
*/
@_exported import ArgumentParser
@_exported import struct SystemPackage.FilePath
@_exported import struct Shwift.Environment
@_exported import struct Shwift.Input
@_exported import struct Shwift.Output
@_exported import struct Shwift.Process
// MARK: - Script
public protocol Script: ParsableCommand {
func run() async throws
/**
The top level shell for this script.
This value is read once prior to calling `run` and saved, so the execution of the script will not reflect changes to the root shell that happen after the Script has started running.
By default, reads the current state of the this process.
*/
var rootShell: Shell { get }
/**
Scripts can implement to run code before or after the body of the script has run, or post-process any errors encountered
*/
func wrapInvocation<T>(
_ invocation: () async throws -> T
) async throws -> T
/**
Called just before we attempt to launch a process.
Can be used for logging.
*/
func willLaunch(
_ executable: Executable,
withArguments arguments: [String],
in workingDirectory: FilePath)
/**
Called if our attempt to launch an executable failed.
Can be used for logging.
*/
func didFailToLaunch(
_ executable: Executable,
withArguments arguments: [String],
in workingDirectory: FilePath,
dueTo error: Error)
/**
Called after we have launched a process.
Can be used for logging.
*/
func process(
withID processID: Process.ID,
didLaunchWith executable: Executable,
arguments: [String],
in workingDirectory: FilePath)
/**
Called after a process has terminated.
Can be used for logging.
*/
func process(
withID processID: Process.ID,
for executable: Executable,
withArguments arguments: [String],
in workingDirectory: FilePath,
didComplete error: Error?)
}
extension Script {
public var rootShell: Shell {
Shell(
workingDirectory: FilePath(FileManager.default.currentDirectoryPath),
environment: .process,
standardInput: .standardInput,
standardOutput: .standardOutput,
standardError: .standardError)
}
public func wrapInvocation<T>(
_ invocation: () async throws -> T
) async throws -> T {
try await invocation()
}
}
// MARK: - Logging Default Implementations
extension Script {
/**
Called just before we attempt to launch a process.
Can be used for logging.
*/
public func willLaunch(
_ executable: Executable,
withArguments arguments: [String],
in workingDirectory: FilePath
) {}
/**
Called if our attempt to launch an executable failed.
Can be used for logging.
*/
public func didFailToLaunch(
_ executable: Executable,
withArguments arguments: [String],
in workingDirectory: FilePath,
dueTo error: Error
) {}
/**
Called after we have launched a process.
Can be used for logging.
*/
public func process(
withID processID: Process.ID,
didLaunchWith executable: Executable,
arguments: [String],
in workingDirectory: FilePath
) {}
/**
Called after a process has terminated.
Can be used for logging.
*/
public func process(
withID processID: Process.ID,
for executable: Executable,
withArguments arguments: [String],
in workingDirectory: FilePath,
didComplete error: Error?
) {}
}
// MARK: - Adapter for `ParsableCommand`
extension Script {
public func run() throws {
/// Work around for https://forums.swift.org/t/interaction-between-async-main-and-async-overloads/52171
let box = ErrorBox()
let sem = DispatchSemaphore(value: 0)
Task {
defer { sem.signal() }
do {
let shell = self.rootShell
try await Shell.$hostScript.withValue(self) {
try await Shell.$taskLocal.withValue(shell) {
try await self.wrapInvocation {
try await run()
}
}
}
} catch Process.TerminationError.nonzeroTerminationStatus(let status) {
/// Convert `Shell` error into one that `ArgumentParser` understands
box.error = ExitCode(rawValue: status)
} catch let error as SystemPackage.Errno {
/// Convert `SystemPackage` error into one that `ArgumentParser` understands
box.error = ExitCode(rawValue: error.rawValue)
} catch {
box.error = error
}
}
sem.wait()
if let error = box.error {
throw error
}
}
}
private final class ErrorBox {
var error: Error? = nil
}
// MARK: - Shell
public struct Shell {
public init(
workingDirectory: FilePath,
environment: Environment,
standardInput: Input,
standardOutput: Output,
standardError: Output
) {
self.workingDirectory = workingDirectory
self.environment = environment
self.standardInput = standardInput
self.standardOutput = standardOutput
self.standardError = standardError
self.context = Context()
}
fileprivate(set) var workingDirectory: FilePath
fileprivate(set) var environment: Environment
fileprivate(set) var standardInput: Input
fileprivate(set) var standardOutput: Output
fileprivate(set) var standardError: Output
fileprivate let context: Context
struct Invocation {
let standardInput: FileDescriptor
let standardOutput: FileDescriptor
let standardError: FileDescriptor
let context: Context
/**
Convenience for builtin invocations
*/
func builtin<T>(
_ command: (Builtin.Channel) async throws -> T
) async throws -> T {
try await Builtin.withChannel(
input: standardInput,
output: standardOutput,
in: context,
command)
}
}
static var current: Shell { taskLocal }
static var scriptForLogging: Script { hostScript }
static func invoke<T>(
_ command: (Shell, Invocation) async throws -> T
) async throws -> T {
let shell: Shell = .taskLocal
return try await shell.standardInput.withFileDescriptor(in: shell.context) { input in
try await shell.standardOutput.withFileDescriptor(in: shell.context) { output in
try await shell.standardError.withFileDescriptor(in: shell.context) { error in
try await command(
shell,
Invocation(
standardInput: input,
standardOutput: output,
standardError: error,
context: shell.context))
}
}
}
}
@TaskLocal
fileprivate static var taskLocal: Shell!
@TaskLocal
fileprivate static var hostScript: Script!
}
// MARK: - Subshell
public func subshell<T>(
pushing path: FilePath? = nil,
updatingEnvironmentWith environmentUpdates: [String: String?] = [:],
standardInput: Input? = nil,
standardOutput: Output? = nil,
standardError: Output? = nil,
operation: () async throws -> T
) async throws -> T {
var shell: Shell = .current
if let path = path {
shell.workingDirectory.push(path)
}
for (name, value) in environmentUpdates {
shell.environment.setValue(value, forVariableNamed: name)
}
if let standardInput = standardInput {
shell.standardInput = standardInput
}
if let standardOutput = standardOutput {
shell.standardOutput = standardOutput
}
if let standardError = standardError {
shell.standardError = standardError
}
return try await Shell.$taskLocal.withValue(shell, operation: operation)
}
@_disfavoredOverload
public func subshell<T>(
pushing path: FilePath? = nil,
updatingEnvironmentWith environmentUpdates: [String: String?] = [:],
standardInput: Input? = nil,
standardOutput: Output? = nil,
standardError: Output? = nil,
operation: @escaping () async throws -> T
) -> Shell.PipableCommand<T> {
Shell.PipableCommand {
try await subshell(
pushing: path,
updatingEnvironmentWith: environmentUpdates,
standardInput: standardInput,
standardOutput: standardOutput,
standardError: standardError,
operation: operation)
}
}
// MARK: - Shell State
/**
The current working directory of the current `Script`.
*/
public var workingDirectory: FilePath {
Shell.current.workingDirectory
}
public var environment: Environment {
Shell.current.environment
}