|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# [MISE] description="Verify standalone example POMs won't break spotless" |
| 4 | + |
| 5 | +"""Check that standalone example modules don't break 'mise run format'. |
| 6 | +
|
| 7 | +Example modules are intentionally standalone (no <parent> from the project) |
| 8 | +so users can copy them. But they're included in the Maven reactor via the |
| 9 | +examples-and-integration-tests profile. If 'mise run format' doesn't |
| 10 | +exclude them, spotless:apply fails because the plugin isn't declared. |
| 11 | +
|
| 12 | +This lint verifies that every standalone example POM is excluded from |
| 13 | +the format task in mise.toml. |
| 14 | +""" |
| 15 | + |
| 16 | +import re |
| 17 | +import sys |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +ROOT = Path(__file__).resolve().parents[3] |
| 21 | +EXAMPLES_DIR = ROOT / "examples" |
| 22 | + |
| 23 | + |
| 24 | +def find_standalone_example_poms() -> list[Path]: |
| 25 | + """Find example pom.xml files that don't inherit from the project parent.""" |
| 26 | + standalone = [] |
| 27 | + for pom in sorted(EXAMPLES_DIR.rglob("pom.xml")): |
| 28 | + if "target" in pom.parts: |
| 29 | + continue |
| 30 | + text = pom.read_text(encoding="utf-8") |
| 31 | + # Check if this POM has a <parent> with the project's groupId/artifactId |
| 32 | + has_project_parent = bool( |
| 33 | + re.search( |
| 34 | + r"<parent>\s*<groupId>io\.prometheus</groupId>\s*" |
| 35 | + r"<artifactId>client_java</artifactId>", |
| 36 | + text, |
| 37 | + ) |
| 38 | + ) |
| 39 | + if not has_project_parent: |
| 40 | + standalone.append(pom) |
| 41 | + return standalone |
| 42 | + |
| 43 | + |
| 44 | +def format_task_excludes_examples() -> bool: |
| 45 | + """Check that the format task in mise.toml excludes standalone examples.""" |
| 46 | + mise_toml = ROOT / "mise.toml" |
| 47 | + text = mise_toml.read_text(encoding="utf-8") |
| 48 | + # Look for the format task run command |
| 49 | + match = re.search( |
| 50 | + r'\[tasks\.format\].*?run\s*=\s*"([^"]*)"', text, re.DOTALL |
| 51 | + ) |
| 52 | + if not match: |
| 53 | + return False |
| 54 | + run_cmd = match.group(1) |
| 55 | + # The command should deactivate the examples-and-integration-tests profile |
| 56 | + return "!examples-and-integration-tests" in run_cmd |
| 57 | + |
| 58 | + |
| 59 | +def main() -> int: |
| 60 | + standalone = find_standalone_example_poms() |
| 61 | + if not standalone: |
| 62 | + return 0 |
| 63 | + |
| 64 | + if format_task_excludes_examples(): |
| 65 | + return 0 |
| 66 | + |
| 67 | + print("ERROR: Standalone example POMs found but 'mise run format'") |
| 68 | + print("does not exclude the examples-and-integration-tests profile.") |
| 69 | + print() |
| 70 | + print("Standalone example POMs (no project parent):") |
| 71 | + for pom in standalone: |
| 72 | + print(f" {pom.relative_to(ROOT)}") |
| 73 | + print() |
| 74 | + print("Fix: ensure the format task in mise.toml deactivates the") |
| 75 | + print("examples-and-integration-tests profile, e.g.:") |
| 76 | + print(" -P '!examples-and-integration-tests'") |
| 77 | + return 1 |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + sys.exit(main()) |
0 commit comments