/
SwiftRunTool.swift
148 lines (118 loc) · 4.41 KB
/
SwiftRunTool.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
/*
This source file is part of the Swift.org open source project
Copyright 2015 - 2016 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import class Foundation.ProcessInfo
import Basic
import Build
import Utility
import PackageGraph
import PackageModel
import func POSIX.chdir
import func POSIX.getcwd
private enum RunError: Swift.Error {
case noExecutableFound
case executableNotFound(String)
case multipleExecutables
}
extension RunError: CustomStringConvertible {
var description: String {
switch self {
case .noExecutableFound:
return "no executable product found"
case .executableNotFound(let executable):
return "could not find executable '\(executable)'"
case .multipleExecutables:
return "multiple products defined"
}
}
}
public class RunToolOptions: ToolOptions {
/// Returns the mode in with the tool command should run.
var mode: RunMode {
// If we got version option, just print the version and exit.
if shouldPrintVersion {
return .version
}
return .run
}
/// If the executable product should be built before running.
var shouldBuild = true
/// The custom working directory the executable should be run in.
var workingDirectory: AbsolutePath?
/// The executable product to run.
var executable: String?
/// The arguments to pass to the executable.
var arguments: [String] = []
}
public enum RunMode {
case version
case run
}
/// swift-run tool namespace
public class SwiftRunTool: SwiftTool<RunToolOptions> {
public convenience init(args: [String]) {
self.init(
toolName: "run",
usage: "[options] [executable <arguments>]",
overview: "Build and run an executable product",
args: args
)
}
override func runImpl() throws {
switch options.mode {
case .version:
print(Versioning.currentVersion.completeDisplayString)
case .run:
let plan = try buildPlan()
if options.shouldBuild {
try build(plan: plan, includingTests: false)
}
let product = try findExecutable(in: plan)
let path = plan.buildParameters.buildPath.appending(component: product.name)
try run(at: path)
}
}
/// Find executable product based on options.
private func findExecutable(in plan: BuildPlan) throws -> ResolvedProduct {
let executableProducts = plan.graph.products.filter({ $0.type == .executable })
guard executableProducts.count > 0 else {
throw RunError.noExecutableFound
}
if let executable = options.executable {
guard let executableProduct = executableProducts.first(where: { $0.name == executable }) else {
throw RunError.executableNotFound(executable)
}
return executableProduct
} else {
guard executableProducts.count == 1 else {
throw RunError.multipleExecutables
}
return executableProducts.first!
}
}
/// Executes and returns execution status. Prints output on standard streams.
private func run(at path: AbsolutePath) throws {
if let workingDirectory = options.workingDirectory {
try POSIX.chdir(workingDirectory.asString)
}
let relativePath = path.relative(to: currentWorkingDirectory)
try exec(path: path.asString, args: [relativePath.asString] + options.arguments)
}
override class func defineArguments(parser: ArgumentParser, binder: ArgumentBinder<RunToolOptions>) {
binder.bind(
option: parser.add(option: "--skip-build", kind: Bool.self,
usage: "Skip building the executable product"),
to: { $0.shouldBuild = !$1 })
binder.bindArray(
positional: parser.add(positional: "executable", kind: [String].self, optional: true, strategy: .remaining,
usage: "The executable to run"),
to: {
$0.executable = $1.first!
$0.arguments = Array($1.dropFirst())
})
}
}