|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from common import configure_stdout, emit_json, emit_output |
| 7 | +from scan_project import build_summary |
| 8 | + |
| 9 | + |
| 10 | +def build_parser() -> argparse.ArgumentParser: |
| 11 | + parser = argparse.ArgumentParser(description="Explain what the repository scanner detected and why") |
| 12 | + parser.add_argument("--target", default=".", help="Target project directory") |
| 13 | + parser.add_argument("--output", default="-", help="Output path, or - for stdout") |
| 14 | + parser.add_argument("--format", choices=["text", "markdown", "json"], default="text", help="Output format") |
| 15 | + return parser |
| 16 | + |
| 17 | + |
| 18 | +def build_payload(summary: dict[str, object]) -> dict[str, object]: |
| 19 | + confidence = summary.get("confidence_notes", {}) |
| 20 | + return { |
| 21 | + "project_name": summary.get("project_name"), |
| 22 | + "primary_language": { |
| 23 | + "value": summary.get("primary_language"), |
| 24 | + **confidence.get("primary_language", {}), |
| 25 | + }, |
| 26 | + "entrypoints": summary.get("entrypoints", []), |
| 27 | + "tests": { |
| 28 | + **summary.get("tests", {}), |
| 29 | + **({"confidence": confidence.get("tests", {}).get("confidence"), "reason": confidence.get("tests", {}).get("reason")} if confidence.get("tests") else {}), |
| 30 | + }, |
| 31 | + "commands": { |
| 32 | + "items": summary.get("commands", []), |
| 33 | + **({"confidence": confidence.get("commands", {}).get("confidence"), "reason": confidence.get("commands", {}).get("reason")} if confidence.get("commands") else {}), |
| 34 | + }, |
| 35 | + "areas": { |
| 36 | + "items": summary.get("area_hints", []), |
| 37 | + **({"confidence": confidence.get("areas", {}).get("confidence"), "reason": confidence.get("areas", {}).get("reason")} if confidence.get("areas") else {}), |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | +def render_text(summary: dict[str, object]) -> str: |
| 43 | + confidence = summary.get("confidence_notes", {}) |
| 44 | + entrypoints = summary.get("entrypoints", []) |
| 45 | + commands = summary.get("commands", []) |
| 46 | + area_hints = summary.get("area_hints", []) |
| 47 | + tests = summary.get("tests", {}) |
| 48 | + |
| 49 | + lines = [ |
| 50 | + f"Project: {summary.get('project_name')}", |
| 51 | + f"Primary language: {summary.get('primary_language') or 'unknown'}", |
| 52 | + f" Confidence: {confidence.get('primary_language', {}).get('confidence', 'unknown')}", |
| 53 | + f" Reason: {confidence.get('primary_language', {}).get('reason', 'Needs review')}", |
| 54 | + "", |
| 55 | + "Entrypoints:", |
| 56 | + ] |
| 57 | + if entrypoints: |
| 58 | + for item in entrypoints[:8]: |
| 59 | + reasons = ", ".join(item.get("reasons", [])) or "Needs review" |
| 60 | + lines.append(f"- {item['path']} [{item.get('confidence', 'unknown')}]") |
| 61 | + lines.append(f" Reasons: {reasons}") |
| 62 | + else: |
| 63 | + lines.append("- none detected") |
| 64 | + |
| 65 | + lines.extend( |
| 66 | + [ |
| 67 | + "", |
| 68 | + f"Tests: {tests.get('count', 0) if isinstance(tests, dict) else 0} detected", |
| 69 | + f" Confidence: {confidence.get('tests', {}).get('confidence', 'unknown')}", |
| 70 | + f" Reason: {confidence.get('tests', {}).get('reason', 'Needs review')}", |
| 71 | + "", |
| 72 | + f"Commands: {len(commands)} detected", |
| 73 | + f" Confidence: {confidence.get('commands', {}).get('confidence', 'unknown')}", |
| 74 | + f" Reason: {confidence.get('commands', {}).get('reason', 'Needs review')}", |
| 75 | + "", |
| 76 | + "Area hints:", |
| 77 | + ] |
| 78 | + ) |
| 79 | + if area_hints: |
| 80 | + for item in area_hints: |
| 81 | + lines.append(f"- {item['area']} [{item.get('confidence', 'unknown')}] -> {', '.join(item.get('matches', []))}") |
| 82 | + else: |
| 83 | + lines.append("- none detected") |
| 84 | + return "\n".join(lines) |
| 85 | + |
| 86 | + |
| 87 | +def render_markdown(summary: dict[str, object]) -> str: |
| 88 | + payload = build_payload(summary) |
| 89 | + entrypoints = payload["entrypoints"] |
| 90 | + tests = payload["tests"] |
| 91 | + commands = payload["commands"] |
| 92 | + areas = payload["areas"] |
| 93 | + |
| 94 | + lines = [ |
| 95 | + "# Scan Explanation", |
| 96 | + "", |
| 97 | + f"- Project: {payload['project_name']}", |
| 98 | + f"- Primary language: {payload['primary_language'].get('value') or 'unknown'}", |
| 99 | + f"- Language confidence: {payload['primary_language'].get('confidence', 'unknown')}", |
| 100 | + f"- Language reason: {payload['primary_language'].get('reason', 'Needs review')}", |
| 101 | + "", |
| 102 | + "## Entrypoints", |
| 103 | + ] |
| 104 | + if entrypoints: |
| 105 | + for item in entrypoints[:8]: |
| 106 | + lines.append(f"- `{item['path']}`") |
| 107 | + lines.append(f" - confidence: {item.get('confidence', 'unknown')}") |
| 108 | + lines.append(f" - reasons: {', '.join(item.get('reasons', [])) or 'Needs review'}") |
| 109 | + else: |
| 110 | + lines.append("- none detected") |
| 111 | + |
| 112 | + lines.extend( |
| 113 | + [ |
| 114 | + "", |
| 115 | + "## Tests", |
| 116 | + f"- count: {tests.get('count', 0)}", |
| 117 | + f"- confidence: {tests.get('confidence', 'unknown')}", |
| 118 | + f"- reason: {tests.get('reason', 'Needs review')}", |
| 119 | + "", |
| 120 | + "## Commands", |
| 121 | + f"- count: {len(commands.get('items', []))}", |
| 122 | + f"- confidence: {commands.get('confidence', 'unknown')}", |
| 123 | + f"- reason: {commands.get('reason', 'Needs review')}", |
| 124 | + "", |
| 125 | + "## Areas", |
| 126 | + f"- confidence: {areas.get('confidence', 'unknown')}", |
| 127 | + f"- reason: {areas.get('reason', 'Needs review')}", |
| 128 | + ] |
| 129 | + ) |
| 130 | + if areas.get("items"): |
| 131 | + for item in areas["items"]: |
| 132 | + lines.append(f"- {item['area']}: {', '.join(item.get('matches', []))}") |
| 133 | + else: |
| 134 | + lines.append("- none detected") |
| 135 | + return "\n".join(lines) |
| 136 | + |
| 137 | + |
| 138 | +def main() -> int: |
| 139 | + configure_stdout() |
| 140 | + parser = build_parser() |
| 141 | + args = parser.parse_args() |
| 142 | + summary = build_summary(Path(args.target).resolve()) |
| 143 | + output = args.output |
| 144 | + if args.format == "json": |
| 145 | + emit_json(output, build_payload(summary)) |
| 146 | + elif args.format == "markdown": |
| 147 | + emit_output(output, render_markdown(summary)) |
| 148 | + else: |
| 149 | + emit_output(output, render_text(summary)) |
| 150 | + return 0 |
| 151 | + |
| 152 | + |
| 153 | +if __name__ == "__main__": |
| 154 | + raise SystemExit(main()) |
0 commit comments