|
| 1 | +#!/usr/bin/env swift |
| 2 | + |
| 3 | +import class Foundation.FileManager |
| 4 | +import class Foundation.Process |
| 5 | +import class Foundation.ProcessInfo |
| 6 | +import struct Foundation.URL |
| 7 | +import func Foundation.exit |
| 8 | + |
| 9 | +/// The root directory of the project. |
| 10 | +let projectRoot = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent() |
| 11 | + |
| 12 | +/// Returns the path to the executable if it is found in the PATH environment variable. |
| 13 | +func which(_ executable: String) -> String? { |
| 14 | + let pathSeparator: Character |
| 15 | + #if os(Windows) |
| 16 | + pathSeparator = ";" |
| 17 | + #else |
| 18 | + pathSeparator = ":" |
| 19 | + #endif |
| 20 | + let paths = ProcessInfo.processInfo.environment["PATH"]!.split(separator: pathSeparator) |
| 21 | + for path in paths { |
| 22 | + let url = URL(fileURLWithPath: String(path)).appendingPathComponent(executable) |
| 23 | + if FileManager.default.isExecutableFile(atPath: url.path) { |
| 24 | + return url.path |
| 25 | + } |
| 26 | + } |
| 27 | + return nil |
| 28 | +} |
| 29 | + |
| 30 | +/// Runs the `swift-format` command with the given arguments in the project root. |
| 31 | +func swiftFormat(_ arguments: [String]) throws { |
| 32 | + guard let swiftFormat = which("swift-format") else { |
| 33 | + print("swift-format not found in PATH") |
| 34 | + exit(1) |
| 35 | + } |
| 36 | + let task = Process() |
| 37 | + task.executableURL = URL(fileURLWithPath: swiftFormat) |
| 38 | + task.arguments = arguments |
| 39 | + task.currentDirectoryURL = projectRoot |
| 40 | + try task.run() |
| 41 | + task.waitUntilExit() |
| 42 | + if task.terminationStatus != 0 { |
| 43 | + print("swift-format failed with status \(task.terminationStatus)") |
| 44 | + exit(1) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/// Patterns to exclude from formatting. |
| 49 | +let excluded: Set<String> = [ |
| 50 | + ".git", |
| 51 | + ".build", |
| 52 | + ".index-build", |
| 53 | + "node_modules", |
| 54 | + "__Snapshots__", |
| 55 | + // Exclude the script itself to avoid changing its file mode. |
| 56 | + URL(fileURLWithPath: #filePath).lastPathComponent, |
| 57 | +] |
| 58 | + |
| 59 | +/// Returns a list of directories to format. |
| 60 | +func filesToFormat() -> [String] { |
| 61 | + var files: [String] = [] |
| 62 | + let fileManager = FileManager.default |
| 63 | + let enumerator = fileManager.enumerator( |
| 64 | + at: projectRoot, includingPropertiesForKeys: nil |
| 65 | + )! |
| 66 | + for case let fileURL as URL in enumerator { |
| 67 | + if excluded.contains(fileURL.lastPathComponent) { |
| 68 | + if fileURL.hasDirectoryPath { |
| 69 | + enumerator.skipDescendants() |
| 70 | + } |
| 71 | + continue |
| 72 | + } |
| 73 | + guard fileURL.pathExtension == "swift" else { continue } |
| 74 | + files.append(fileURL.path) |
| 75 | + } |
| 76 | + return files |
| 77 | +} |
| 78 | + |
| 79 | +// Format the files in the project. |
| 80 | +try swiftFormat(["format", "--parallel", "--in-place", "--recursive"] + filesToFormat()) |
0 commit comments