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

import subprocess
import tomllib
from pathlib import Path

ROOT = Path(__file__).resolve().parent
WORKSPACE_MANIFEST = ROOT / "Cargo.toml"
DEFAULT_CLIPPY = ["cargo", "clippy", "--workspace", "--all-targets", "--all-features"]


def run(argv: list[str]) -> None:
    print("+", " ".join(argv), flush=True)
    proc = subprocess.run(argv, cwd=ROOT)
    if proc.returncode != 0:
        raise SystemExit(proc.returncode)


def normalized_clippy_command() -> list[str]:
    workspace = tomllib.loads(WORKSPACE_MANIFEST.read_text(encoding="utf-8"))
    metadata = (
        workspace.get("workspace", {})
        .get("metadata", {})
        .get("adequate-rust-mcp", {})
    )
    configured = metadata.get("clippy_command")
    if not (
        isinstance(configured, list)
        and configured
        and all(isinstance(part, str) for part in configured)
    ):
        return DEFAULT_CLIPPY

    # Human check output does not need machine JSON framing.
    command: list[str] = []
    skip_next = False
    for index, part in enumerate(configured):
        if skip_next:
            skip_next = False
            continue
        if part.startswith("--message-format="):
            continue
        if part == "--message-format" and index + 1 < len(configured):
            skip_next = True
            continue
        command.append(part)
    return command or DEFAULT_CLIPPY


def main() -> None:
    run(["cargo", "fmt", "--all", "--check"])
    run(normalized_clippy_command())
    run(["cargo", "test", "--workspace", "--all-targets", "--all-features"])


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