Skip to content

Commit 022add4

Browse files
Add SWIFT_FORMAT_PATH environment variable to override swift-format path
1 parent 177ed2a commit 022add4

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

Diff for: Utilities/format.swift

+14-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ import func Foundation.exit
1010
let projectRoot = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent()
1111

1212
/// Returns the path to the executable if it is found in the PATH environment variable.
13-
func which(_ executable: String) -> String? {
13+
func which(_ executable: String) -> URL? {
14+
do {
15+
// Check overriding environment variable
16+
let envVariable = executable.uppercased().replacingOccurrences(of: "-", with: "_") + "_PATH"
17+
if let path = ProcessInfo.processInfo.environment[envVariable] {
18+
if FileManager.default.isExecutableFile(atPath: path) {
19+
return URL(fileURLWithPath: path)
20+
}
21+
}
22+
}
1423
let pathSeparator: Character
1524
#if os(Windows)
1625
pathSeparator = ";"
@@ -21,7 +30,7 @@ func which(_ executable: String) -> String? {
2130
for path in paths {
2231
let url = URL(fileURLWithPath: String(path)).appendingPathComponent(executable)
2332
if FileManager.default.isExecutableFile(atPath: url.path) {
24-
return url.path
33+
return url
2534
}
2635
}
2736
return nil
@@ -33,8 +42,9 @@ func swiftFormat(_ arguments: [String]) throws {
3342
print("swift-format not found in PATH")
3443
exit(1)
3544
}
45+
print("[Utilities/format.swift] Running \(swiftFormat.path)")
3646
let task = Process()
37-
task.executableURL = URL(fileURLWithPath: swiftFormat)
47+
task.executableURL = swiftFormat
3848
task.arguments = arguments
3949
task.currentDirectoryURL = projectRoot
4050
try task.run()
@@ -43,6 +53,7 @@ func swiftFormat(_ arguments: [String]) throws {
4353
print("swift-format failed with status \(task.terminationStatus)")
4454
exit(1)
4555
}
56+
print("[Utilities/format.swift] Done")
4657
}
4758

4859
/// Patterns to exclude from formatting.

0 commit comments

Comments
 (0)