forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-sources-root-layout.py
More file actions
executable file
·104 lines (83 loc) · 3.18 KB
/
Copy pathcheck-sources-root-layout.py
File metadata and controls
executable file
·104 lines (83 loc) · 3.18 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
#!/usr/bin/env python3
"""Ratchet on loose Swift files directly under Desktop/Sources/.
Issue #8848: the desktop app had ~100+ `.swift` files sitting in the
`Sources/` root instead of feature directories. New code should land in a
feature folder (e.g. `Onboarding/`, `MainWindow/`) so module boundaries stay
obvious and future SPM carve-outs stay mechanical.
This gate counts `*.swift` files at `desktop/macos/Desktop/Sources/*.swift`
(max depth 1) and FAILS if that count rises above the pinned baseline. The
baseline may only shrink — after organizing files into directories, lower
BASELINE in the same commit.
Wiring (see also `.github/workflows/repo-checks.yml`):
- CI: gated step in the Repo Checks workflow when desktop Swift sources change.
- Manually: python3 desktop/macos/scripts/check-sources-root-layout.py
- Show offenders: ... --print
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
# Pinned baseline — loose root-level Swift files. MAY ONLY DECREASE.
BASELINE = 77
SOURCES_ROOT = "desktop/macos/Desktop/Sources"
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
"--root",
default=None,
help="Repository root (default: inferred from this script's location).",
)
parser.add_argument(
"--print",
dest="print_offenders",
action="store_true",
help="Print the current count and every offending path, then exit 0.",
)
return parser.parse_args()
def repo_root(explicit: str | None) -> Path:
if explicit:
return Path(explicit).resolve()
return Path(__file__).resolve().parents[3]
def scan(root: Path) -> list[str]:
sources = root / SOURCES_ROOT
return sorted(
path.relative_to(root).as_posix()
for path in sources.glob("*.swift")
if path.is_file()
)
def main() -> int:
args = parse_args()
root = repo_root(args.root)
offenders = scan(root)
count = len(offenders)
if args.print_offenders:
for relative in offenders:
print(relative)
print(f"\nloose Sources-root Swift files: {count} (baseline {BASELINE})")
return 0
if count > BASELINE:
print(
f"FAIL: loose Sources-root Swift files rose to {count} (baseline {BASELINE}).",
file=sys.stderr,
)
print(
"Place new desktop Swift sources in a feature directory under "
f"{SOURCES_ROOT}/ instead of the root. See desktop/macos/AGENTS.md.",
file=sys.stderr,
)
print(
"Offenders: python3 desktop/macos/scripts/check-sources-root-layout.py --print",
file=sys.stderr,
)
return 1
if count < BASELINE:
print(
f"NOTE: loose Sources-root Swift files dropped to {count} "
f"(baseline {BASELINE}). Lower BASELINE to {count} in "
"check-sources-root-layout.py to ratchet the gain."
)
return 0
print(f"OK: loose Sources-root Swift files at baseline ({count}).")
return 0
if __name__ == "__main__":
sys.exit(main())