Skip to content

Commit feee0ef

Browse files
committed
[swift-syntax-dev-utils] Throw an error in if an executable could not be found
1 parent 7c6d4f7 commit feee0ef

File tree

3 files changed

+50
-23
lines changed

3 files changed

+50
-23
lines changed

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/commands/VerifySourceCode.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,12 @@ struct VerifySourceCode: ParsableCommand, SourceCodeGeneratorCommand {
3636

3737
logSection("Verifing code generated files")
3838

39-
guard let diffExec = Paths.diffExec else {
40-
throw ScriptExectutionError(message: "Didn't find a diff execution path")
41-
}
42-
4339
for module in modules {
4440
let selfGeneratedDir = tempDir.appendingPathComponent(module).appendingPathComponent("generated")
4541
let userGeneratedDir = Paths.sourcesDir.appendingPathComponent(module).appendingPathComponent("generated")
4642

4743
let process = ProcessRunner(
48-
executableURL: diffExec,
44+
executableURL: try Paths.diffExec,
4945
arguments: [
5046
"--recursive",
5147
"--exclude",

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/common/BuildCommand.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,8 @@ extension BuildCommand {
8686
@discardableResult
8787
func invokeXcodeBuild(projectPath: URL, scheme: String) throws -> ProcessResult {
8888
return try withTemporaryDirectory { tempDir in
89-
guard let xcodebuildExec = Paths.xcodebuildExec else {
90-
throw ScriptExectutionError(
91-
message: """
92-
Error: Could not find xcodebuild.
93-
Looking at '\(Paths.xcodebuildExec?.path ?? "N/A")'.
94-
"""
95-
)
96-
}
9789
let processRunner = ProcessRunner(
98-
executableURL: xcodebuildExec,
90+
executableURL: try Paths.xcodebuildExec,
9991
arguments: [
10092
"-project", projectPath.path,
10193
"-scheme", scheme,

SwiftSyntaxDevUtils/Sources/swift-syntax-dev-utils/common/Paths.swift

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,40 @@ enum Paths {
6161
.appendingPathComponent("lit.py")
6262
}
6363

64-
static var python3Exec: URL? {
65-
return lookupExecutable(for: "python3")
64+
static var python3Exec: URL {
65+
get throws {
66+
return try lookupExecutable(for: "python3")
67+
}
6668
}
6769

68-
static var diffExec: URL? {
69-
return lookupExecutable(for: "diff")
70+
static var diffExec: URL {
71+
get throws {
72+
return try lookupExecutable(for: "diff")
73+
}
7074
}
7175

72-
static var xcodebuildExec: URL? {
73-
return lookupExecutable(for: "xcodebuild")
76+
static var gitExec: URL {
77+
get throws {
78+
try lookupExecutable(for: "git")
79+
}
80+
}
81+
82+
static var swiftExec: URL {
83+
get throws {
84+
try lookupExecutable(for: "swift")
85+
}
86+
}
87+
88+
/// The directory in which swift-format should be built.
89+
static var swiftFormatBuildDir: URL {
90+
packageDir
91+
.appendingPathComponent(".swift-format-build")
92+
}
93+
94+
static var xcodebuildExec: URL {
95+
get throws {
96+
return try lookupExecutable(for: "xcodebuild")
97+
}
7498
}
7599

76100
private static var envSearchPaths: [URL] {
@@ -98,13 +122,28 @@ enum Paths {
98122
return ProcessInfo.processInfo.environment[pathArg]
99123
}
100124

101-
private static func lookupExecutable(for filename: String) -> URL? {
102-
return envSearchPaths.map { $0.appendingPathComponent(filename) }
125+
enum ExecutableLookupError: Error, CustomStringConvertible {
126+
case notFound(executableName: String)
127+
128+
var description: String {
129+
switch self {
130+
case .notFound(executableName: let executableName):
131+
return "Executable \(executableName) not found in PATH"
132+
}
133+
}
134+
}
135+
136+
private static func lookupExecutable(for filename: String) throws -> URL {
137+
let executable = envSearchPaths.map { $0.appendingPathComponent(filename) }
103138
.first(where: { $0.isExecutableFile })
139+
guard let executable else {
140+
throw ExecutableLookupError.notFound(executableName: filename)
141+
}
142+
return executable
104143
}
105144
}
106145

107-
fileprivate extension URL {
146+
extension URL {
108147
var isExecutableFile: Bool {
109148
return (self.isFile(path) || self.isSymlink(path)) && FileManager.default.isExecutableFile(atPath: path)
110149
}

0 commit comments

Comments
 (0)