|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
3 | 3 | import subprocess |
4 | | -import json |
| 4 | +import os |
5 | 5 |
|
6 | | - |
7 | | -def all_swiftpm_targets(): |
8 | | - # List up all SwiftPM targets by "swift package dump-package" |
9 | | - output = subprocess.run( |
10 | | - ["swift", "package", "dump-package"], stdout=subprocess.PIPE) |
11 | | - package = json.loads(output.stdout) |
12 | | - targets = [] |
13 | | - for target in package["targets"]: |
14 | | - if target["type"] == "plugin": |
15 | | - continue |
16 | | - targets.append(target["name"]) |
17 | | - |
18 | | - return targets |
| 6 | +SOURCE_ROOT = os.path.relpath(os.path.join(os.path.dirname(__file__), "..")) |
19 | 7 |
|
20 | 8 |
|
21 | 9 | def run(arguments): |
22 | 10 | print("Running: " + " ".join(arguments)) |
23 | 11 | subprocess.run(arguments, check=True) |
24 | 12 |
|
25 | 13 |
|
| 14 | +def build_swift_format(): |
| 15 | + # Build swift-format |
| 16 | + package_path = os.path.join(SOURCE_ROOT, "Vendor", "swift-format") |
| 17 | + bin_path = os.path.join(package_path, ".build", "release", "swift-format") |
| 18 | + if os.path.exists(bin_path): |
| 19 | + return bin_path |
| 20 | + |
| 21 | + run(["./Vendor/checkout-dependency", "swift-format"]) |
| 22 | + |
| 23 | + run([ |
| 24 | + "swift", "build", "-c", "release", |
| 25 | + "--package-path", package_path]) |
| 26 | + return bin_path |
| 27 | + |
| 28 | + |
26 | 29 | def main(): |
27 | | - targets = all_swiftpm_targets() |
| 30 | + targets = [] |
| 31 | + for targets_dir in ["Sources", "Tests"]: |
| 32 | + targets_path = os.path.join(SOURCE_ROOT, targets_dir) |
| 33 | + for target in os.listdir(targets_path): |
| 34 | + if not os.path.isdir(os.path.join(targets_path, target)): |
| 35 | + continue |
| 36 | + targets.append(os.path.join(targets_dir, target)) |
| 37 | + |
28 | 38 | # NOTE: SystemExtras is not included in the list of targets because it |
29 | 39 | # follows swift-system style conventions, which is different from |
30 | 40 | # swift-format. |
31 | | - targets.remove("SystemExtras") |
| 41 | + targets.remove(os.path.join("Sources", "SystemExtras")) |
32 | 42 |
|
33 | | - arguments = ["swift", "package", |
34 | | - "--allow-writing-to-package-directory", "format-source-code"] |
35 | | - for target in targets: |
36 | | - arguments.append("--target") |
37 | | - arguments.append(target) |
| 43 | + swift_format = build_swift_format() |
38 | 44 |
|
| 45 | + arguments = [ |
| 46 | + swift_format, "format", "--in-place", "--recursive", "--parallel" |
| 47 | + ] |
| 48 | + for target in targets: |
| 49 | + arguments.append(os.path.join(SOURCE_ROOT, target)) |
| 50 | + arguments.append(os.path.join(SOURCE_ROOT, "Package.swift")) |
39 | 51 | run(arguments) |
40 | | - run(["swift", "format", "--in-place", "Package.swift"]) |
41 | 52 |
|
42 | 53 |
|
43 | 54 | if __name__ == "__main__": |
|
0 commit comments