Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
executable file
·
101 lines (94 loc) · 3.22 KB

File metadata and controls

executable file
·
101 lines (94 loc) · 3.22 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env swift
import class Foundation.FileManager
import class Foundation.Process
import class Foundation.ProcessInfo
import struct Foundation.URL
import func Foundation.exit
/// The root directory of the project.
let projectRoot = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent()
/// Returns the path to the executable if it is found in the PATH environment variable.
func which(_ executable: String) -> URL? {
do {
// Check overriding environment variable
let envVariable = executable.uppercased().replacingOccurrences(of: "-", with: "_") + "_PATH"
if let path = ProcessInfo.processInfo.environment[envVariable] {
if FileManager.default.isExecutableFile(atPath: path) {
return URL(fileURLWithPath: path)
}
}
}
let pathSeparator: Character
#if os(Windows)
pathSeparator = ";"
#else
pathSeparator = ":"
#endif
let paths = ProcessInfo.processInfo.environment["PATH"]!.split(separator: pathSeparator)
for path in paths {
let url = URL(fileURLWithPath: String(path)).appendingPathComponent(executable)
if FileManager.default.isExecutableFile(atPath: url.path) {
return url
}
}
return nil
}
/// Runs the `swift-format` command with the given arguments in the project root.
func swiftFormat(_ arguments: [String]) throws {
guard let swiftFormat = which("swift-format") else {
print("swift-format not found in PATH")
exit(1)
}
print("[Utilities/format.swift] Running \(swiftFormat.path)")
let task = Process()
task.executableURL = swiftFormat
task.arguments = arguments
task.currentDirectoryURL = projectRoot
try task.run()
task.waitUntilExit()
if task.terminationStatus != 0 {
print("swift-format failed with status \(task.terminationStatus)")
exit(1)
}
print("[Utilities/format.swift] Done")
}
/// Patterns to exclude from formatting.
let excluded: Set<String> = [
".git",
".build",
".index-build",
"node_modules",
"__Snapshots__",
"Generated",
// Exclude the script itself to avoid changing its file mode.
URL(fileURLWithPath: #filePath).lastPathComponent,
]
/// Returns a list of file paths to format.
func filesToFormat() -> [String] {
var files: [String] = []
let fileManager = FileManager.default
let enumerator = fileManager.enumerator(
at: projectRoot, includingPropertiesForKeys: nil
)!
for case let fileURL as URL in enumerator {
if excluded.contains(fileURL.lastPathComponent) {
if fileURL.hasDirectoryPath {
enumerator.skipDescendants()
}
continue
}
guard fileURL.pathExtension == "swift" else { continue }
files.append(fileURL.path)
}
return files
}
let arguments = CommandLine.arguments[1...]
switch arguments.first {
case "lint":
try swiftFormat(["lint", "--parallel", "--recursive"] + filesToFormat())
case "format", nil:
try swiftFormat(["format", "--parallel", "--in-place", "--recursive"] + filesToFormat())
case let subcommand?:
print("Unknown subcommand: \(subcommand)")
print("Usage: format.swift lint|format")
exit(1)
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.