-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlint
More file actions
executable file
·105 lines (89 loc) · 2.84 KB
/
lint
File metadata and controls
executable file
·105 lines (89 loc) · 2.84 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
"""Run workspace linting: rustfmt check + clippy.
Usage:
uv run --script lint # full workspace lint
uv run --script lint --fix # auto-fix formatting + clippy
uv run --script lint <file.rs> # single-file rustfmt + per-crate clippy
"""
import os
import subprocess
import sys
from pathlib import Path
SCRIPT_DIR = Path(__file__).parent.resolve()
# Activate rustup toolchain on PATH before any Rust tool invocations
sys.path.insert(0, str(SCRIPT_DIR))
from ci.env import activate # noqa: E402
activate()
def detect_crate(file_path: str) -> str:
"""Detect crate name from file path (e.g. crates/fbuild-build/... -> fbuild-build)."""
normalized = file_path.replace("\\", "/")
if "crates/" in normalized:
after = normalized.split("crates/", 1)[1]
crate_dir = after.split("/", 1)[0]
if crate_dir:
return crate_dir
return ""
def run(args: list[str]) -> int:
"""Run a command and return exit code."""
result = subprocess.run(args, cwd=str(SCRIPT_DIR))
return result.returncode
def main() -> int:
fix = False
file_arg = ""
for arg in sys.argv[1:]:
if arg == "--fix":
fix = True
elif arg.endswith(".rs"):
file_arg = arg
# --fix mode
if fix:
rc = run(["uv", "run", "cargo", "fmt", "--all"])
if rc != 0:
return rc
if not file_arg:
return run(
[
"uv", "run", "cargo", "clippy",
"--workspace", "--all-targets", "--", "-D", "warnings",
]
)
# Single file mode
if file_arg:
file_path = str(Path(file_arg).resolve())
if not os.path.isfile(file_path):
print(f"File not found: {file_path}", file=sys.stderr)
return 1
rc = run(["uv", "run", "rustfmt", file_path])
if rc != 0:
return rc
crate = detect_crate(file_path)
if crate:
return run(
[
"uv", "run", "cargo", "clippy",
"-p", crate, "--all-targets", "--", "-D", "warnings",
]
)
else:
return run(
[
"uv", "run", "cargo", "clippy",
"--workspace", "--all-targets", "--", "-D", "warnings",
]
)
# Full workspace mode
rc = run(["uv", "run", "cargo", "fmt", "--all", "--check"])
if rc != 0:
print(
"Formatting issues found. Run 'uv run --script lint --fix' to auto-fix.",
file=sys.stderr,
)
return 1
return run(
[
"uv", "run", "cargo", "clippy",
"--workspace", "--all-targets", "--", "-D", "warnings",
]
)
if __name__ == "__main__":
sys.exit(main())