File: format.py

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (55 lines) | stat: -rwxr-xr-x 1,601 bytes parent folder | download
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()