| 1 | #!/usr/bin/env python3 |
| 2 | """check-crates-reference.py — keep docs/reference/crates.md equal to the workspace. |
| 3 | |
| 4 | The page is one table row per `crates/*/Cargo.toml`: package name, path and |
| 5 | the manifest's `description`. It fails when the committed page differs, so a |
| 6 | new crate, a removed crate or a changed description shows up in review. |
| 7 | |
| 8 | python3 scripts/docs/check-crates-reference.py # check |
| 9 | python3 scripts/docs/check-crates-reference.py --write # regenerate |
| 10 | """ |
| 11 | from __future__ import annotations |
| 12 | |
| 13 | import sys |
| 14 | import tomllib |
| 15 | from pathlib import Path |
| 16 | |
| 17 | REPO_ROOT = Path(__file__).resolve().parents[2] |
| 18 | PAGE = REPO_ROOT / "docs" / "reference" / "crates.md" |
| 19 | |
| 20 | HEADER = """# Workspace crates |
| 21 | |
| 22 | Generated from each `crates/*/Cargo.toml` by |
| 23 | `python3 scripts/docs/check-crates-reference.py --write`. Do not edit the |
| 24 | table by hand; change the crate's `description` and regenerate. How the crates |
| 25 | fit together is in [Architecture](../ARCHITECTURE.md). |
| 26 | |
| 27 | | Crate | Path | What it is | |
| 28 | | --- | --- | --- | |
| 29 | """ |
| 30 | |
| 31 | |
| 32 | def render() -> str: |
| 33 | rows = [] |
| 34 | for manifest in sorted((REPO_ROOT / "crates").glob("*/Cargo.toml")): |
| 35 | package = tomllib.loads(manifest.read_text(encoding="utf8")).get("package", {}) |
| 36 | name = package.get("name") |
| 37 | description = package.get("description") |
| 38 | if not isinstance(name, str) or not isinstance(description, str): |
| 39 | raise SystemExit(f"{manifest.relative_to(REPO_ROOT)}: package name and description are required") |
| 40 | text = " ".join(description.split()).replace("|", "\\|") |
| 41 | rows.append(f"| `{name}` | `crates/{manifest.parent.name}` | {text} |") |
| 42 | return HEADER + "\n".join(rows) + "\n" |
| 43 | |
| 44 | |
| 45 | def main() -> int: |
| 46 | expected = render() |
| 47 | if "--write" in sys.argv[1:]: |
| 48 | PAGE.parent.mkdir(parents=True, exist_ok=True) |
| 49 | PAGE.write_text(expected, encoding="utf8") |
| 50 | print(f"check-crates-reference: wrote {PAGE.relative_to(REPO_ROOT)}") |
| 51 | return 0 |
| 52 | current = PAGE.read_text(encoding="utf8") if PAGE.exists() else "" |
| 53 | if current != expected: |
| 54 | print( |
| 55 | "check-crates-reference: docs/reference/crates.md is out of date; run " |
| 56 | "python3 scripts/docs/check-crates-reference.py --write" |
| 57 | ) |
| 58 | return 1 |
| 59 | print(f"check-crates-reference: {expected.count(chr(10)) - HEADER.count(chr(10))} crates, page is current") |
| 60 | return 0 |
| 61 | |
| 62 | |
| 63 | if __name__ == "__main__": |
| 64 | sys.exit(main()) |
| 65 |