-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathformat.py
executable file
·55 lines (40 loc) · 1.56 KB
/
format.py
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
#!/usr/bin/env python3
import subprocess
import os
SOURCE_ROOT = os.path.relpath(os.path.join(os.path.dirname(__file__), ".."))
def run(arguments):
print("Running: " + " ".join(arguments))
subprocess.run(arguments, check=True)
def build_swift_format():
# Build swift-format
package_path = os.path.join(SOURCE_ROOT, "Vendor", "swift-format")
bin_path = os.path.join(package_path, ".build", "release", "swift-format")
if os.path.exists(bin_path):
return bin_path
run(["./Vendor/checkout-dependency", "swift-format"])
run([
"swift", "build", "-c", "release",
"--package-path", package_path])
return bin_path
def main():
targets = []
for targets_dir in ["Sources", "Tests"]:
targets_path = os.path.join(SOURCE_ROOT, targets_dir)
for target in os.listdir(targets_path):
if not os.path.isdir(os.path.join(targets_path, target)):
continue
targets.append(os.path.join(targets_dir, target))
# NOTE: SystemExtras is not included in the list of targets because it
# follows swift-system style conventions, which is different from
# swift-format.
targets.remove(os.path.join("Sources", "SystemExtras"))
swift_format = build_swift_format()
arguments = [
swift_format, "format", "--in-place", "--recursive", "--parallel"
]
for target in targets:
arguments.append(os.path.join(SOURCE_ROOT, target))
arguments.append(os.path.join(SOURCE_ROOT, "Package.swift"))
run(arguments)
if __name__ == "__main__":
main()