Skip to content

Commit

Permalink
Stop using shell just to run commands
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kateinoigakukun committed May 8, 2024
1 parent 8508bf2 commit beec492
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Sources/WebIDLToSwift/IDLParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ enum IDLParser {
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = FileHandle.standardError
task.arguments = ["-c", "npm start --silent \(modules.joined(separator: " "))"]
task.launchPath = "/bin/zsh"
task.arguments = ["start", "--silent"] + modules
task.launchPath = findExecutable("npm")
task.currentDirectoryURL = URL(fileURLWithPath: #file)
.deletingLastPathComponent()
.appendingPathComponent("../../parse-idl")
Expand Down
26 changes: 20 additions & 6 deletions Sources/WebIDLToSwift/Shell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
enum Shell {
static func format(source: String) {
print("Formatting generated Swift files...")
run(script: "swiftformat --swiftversion 5.5 \(source)")
run(executable: "swiftformat", arguments: ["--swiftversion", "5.5", source])
}

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

private static func run(script: String) {
private static func run(executable: String, arguments: [String]) {
// print("*** running script: \(script)")
let task = Process()
task.standardError = FileHandle.standardError
task.arguments = ["-c", script]
task.launchPath = "/bin/zsh"
task.arguments = arguments
task.launchPath = findExecutable(executable)
task.currentDirectoryURL = projectRoot
task.launch()
task.waitUntilExit()
if task.terminationStatus != 0 {
print("Error: \(script) failed with exit code \(task.terminationStatus)")
print("Error: \(([executable] + arguments).map { "\"\($0)\"" }.joined(separator: " ")) failed with exit code \(task.terminationStatus)")
exit(task.terminationStatus)
}
}
}

func findExecutable(_ name: String) -> String? {
guard let path = ProcessInfo.processInfo.environment["PATH"] else {
return nil
}
let paths = path.split(separator: ":")
for p in paths {
let fullPath = URL(fileURLWithPath: String(p)).appendingPathComponent(name).path
if FileManager.default.isExecutableFile(atPath: fullPath) {
return fullPath
}
}
return nil
}

0 comments on commit beec492

Please sign in to comment.