swarm repositories / source
aboutsummaryrefslogtreecommitdiff
path: root/check.py
blob: 23b558e319afd02bb9e0bcd41274ebd65f02997d (plain)
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
#!/usr/bin/env python3
from __future__ import annotations

import argparse
import subprocess
import tomllib
from pathlib import Path


ROOT = Path(__file__).resolve().parent
WORKSPACE_MANIFEST = ROOT / "Cargo.toml"


def load_commands() -> dict[str, list[str]]:
    workspace = tomllib.loads(WORKSPACE_MANIFEST.read_text(encoding="utf-8"))
    metadata = workspace["workspace"]["metadata"]["rust-starter"]
    commands: dict[str, list[str]] = {}
    for key in (
        "format_command",
        "clippy_command",
        "test_command",
        "doc_command",
        "fix_command",
    ):
        value = metadata.get(key)
        if isinstance(value, list) and value and all(
            isinstance(part, str) for part in value
        ):
            commands[key] = value
    return commands


def run(name: str, argv: list[str]) -> None:
    print(f"[check] {name}: {' '.join(argv)}", flush=True)
    proc = subprocess.run(argv, cwd=ROOT, check=False)
    if proc.returncode != 0:
        raise SystemExit(proc.returncode)


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Thin Rust starter check runner")
    parser.add_argument(
        "mode",
        nargs="?",
        choices=("check", "deep", "fix"),
        default="check",
        help="Run the fast gate, include docs for the deep gate, or run the fix command.",
    )
    return parser.parse_args()


def main() -> None:
    commands = load_commands()
    args = parse_args()

    if args.mode == "fix":
        run("fix", commands["fix_command"])
        return

    run("fmt", commands["format_command"])
    run("clippy", commands["clippy_command"])
    run("test", commands["test_command"])

    if args.mode == "deep" and "doc_command" in commands:
        run("doc", commands["doc_command"])


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        raise SystemExit(130)