/
Executor.swift
46 lines (39 loc) · 1.29 KB
/
Executor.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
import Foundation
final class Executor {
let dryRun: Bool
// MARK: - Initialization
init(dryRun: Bool) {
self.dryRun = dryRun
}
// MARK: - Internal
@discardableResult
func execute(_ command: [String], workingDir: URL? = nil) -> Int32 {
let command = command.joined(separator: " ")
return execute(command, workingDir: workingDir)
}
@discardableResult
func execute(_ command: String, workingDir: URL? = nil) -> Int32 {
console.info(command)
guard !self.dryRun else { return 0 }
let task = createTask(forCommand: command, workingDir: workingDir)
return run(task: task)
}
}
// MARK: - Private
extension Executor {
private func createTask(forCommand command: String, workingDir: URL?) -> Process {
let task = Process()
task.executableURL = URL(fileURLWithPath: "/usr/bin/env")
task.arguments = command.split(separator: " ").map { String($0) }
if let workingDir = workingDir {
task.currentDirectoryURL = workingDir
task.currentDirectoryPath = workingDir.path
}
return task
}
private func run(task: Process) -> Int32 {
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
}