Skip to content

Commit beec492

Browse files
Stop using shell just to run commands
There is no need to use shell here, and we should not assume that user's zshrc configures the PATH correctly. Instead, we should use the `Process` API to run commands directly, and assume that the PATH in the process spawn environment is correctly configured.
1 parent 8508bf2 commit beec492

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

Diff for: Sources/WebIDLToSwift/IDLParser.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ enum IDLParser {
88
let pipe = Pipe()
99
task.standardOutput = pipe
1010
task.standardError = FileHandle.standardError
11-
task.arguments = ["-c", "npm start --silent \(modules.joined(separator: " "))"]
12-
task.launchPath = "/bin/zsh"
11+
task.arguments = ["start", "--silent"] + modules
12+
task.launchPath = findExecutable("npm")
1313
task.currentDirectoryURL = URL(fileURLWithPath: #file)
1414
.deletingLastPathComponent()
1515
.appendingPathComponent("../../parse-idl")

Diff for: Sources/WebIDLToSwift/Shell.swift

+20-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Foundation
33
enum Shell {
44
static func format(source: String) {
55
print("Formatting generated Swift files...")
6-
run(script: "swiftformat --swiftversion 5.5 \(source)")
6+
run(executable: "swiftformat", arguments: ["--swiftversion", "5.5", source])
77
}
88

99
private static let projectRoot = URL(fileURLWithPath: #file)
@@ -18,22 +18,36 @@ enum Shell {
1818
let patchFile = patchFolder.appendingPathComponent(module.swiftModule).appendingPathExtension("patch").path
1919
if FileManager.default.fileExists(atPath: patchFile) {
2020
print("Patching generated Swift files...")
21-
run(script: "git apply \(patchFile)")
21+
run(executable: "git", arguments: ["apply", patchFile])
2222
}
2323
}
2424

25-
private static func run(script: String) {
25+
private static func run(executable: String, arguments: [String]) {
2626
// print("*** running script: \(script)")
2727
let task = Process()
2828
task.standardError = FileHandle.standardError
29-
task.arguments = ["-c", script]
30-
task.launchPath = "/bin/zsh"
29+
task.arguments = arguments
30+
task.launchPath = findExecutable(executable)
3131
task.currentDirectoryURL = projectRoot
3232
task.launch()
3333
task.waitUntilExit()
3434
if task.terminationStatus != 0 {
35-
print("Error: \(script) failed with exit code \(task.terminationStatus)")
35+
print("Error: \(([executable] + arguments).map { "\"\($0)\"" }.joined(separator: " ")) failed with exit code \(task.terminationStatus)")
3636
exit(task.terminationStatus)
3737
}
3838
}
3939
}
40+
41+
func findExecutable(_ name: String) -> String? {
42+
guard let path = ProcessInfo.processInfo.environment["PATH"] else {
43+
return nil
44+
}
45+
let paths = path.split(separator: ":")
46+
for p in paths {
47+
let fullPath = URL(fileURLWithPath: String(p)).appendingPathComponent(name).path
48+
if FileManager.default.isExecutableFile(atPath: fullPath) {
49+
return fullPath
50+
}
51+
}
52+
return nil
53+
}

0 commit comments

Comments
 (0)