|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Generate a markdown table from a community catalog JSON file. |
| 3 | +
|
| 4 | +Reads a catalog.community.json and replaces content between marker comments |
| 5 | +in a target markdown file. If the markers are not present the table is |
| 6 | +printed to stdout. |
| 7 | +
|
| 8 | +Markers expected in the markdown file: |
| 9 | + <!-- catalog-table-start --> |
| 10 | + ... (old table content replaced) ... |
| 11 | + <!-- catalog-table-end --> |
| 12 | +
|
| 13 | +Usage: |
| 14 | + python .github/scripts/catalog-generate-table.py \ |
| 15 | + --catalog presets/catalog.community.json \ |
| 16 | + --type preset \ |
| 17 | + --target docs/community/presets.md |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +import argparse |
| 23 | +import json |
| 24 | +import re |
| 25 | +import sys |
| 26 | +from pathlib import Path |
| 27 | + |
| 28 | +START_MARKER = "<!-- catalog-table-start -->" |
| 29 | +END_MARKER = "<!-- catalog-table-end -->" |
| 30 | + |
| 31 | + |
| 32 | +# --------------------------------------------------------------------------- |
| 33 | +# Table builders — one per catalog type |
| 34 | +# --------------------------------------------------------------------------- |
| 35 | + |
| 36 | +def _repo_display_name(url: str) -> str: |
| 37 | + """Extract the repository name from a GitHub URL.""" |
| 38 | + # https://github.com/user/spec-kit-foo → spec-kit-foo |
| 39 | + return url.rstrip("/").rsplit("/", 1)[-1] |
| 40 | + |
| 41 | + |
| 42 | +def _provides_str_preset(provides: dict) -> str: |
| 43 | + parts: list[str] = [] |
| 44 | + t = provides.get("templates", 0) |
| 45 | + c = provides.get("commands", 0) |
| 46 | + s = provides.get("scripts", 0) |
| 47 | + if t: |
| 48 | + parts.append(f"{t} template{'s' if t != 1 else ''}") |
| 49 | + if c: |
| 50 | + parts.append(f"{c} command{'s' if c != 1 else ''}") |
| 51 | + if s: |
| 52 | + parts.append(f"{s} script{'s' if s != 1 else ''}") |
| 53 | + return ", ".join(parts) or "—" |
| 54 | + |
| 55 | + |
| 56 | +def _requires_str_preset(requires: dict) -> str: |
| 57 | + exts = requires.get("extensions", []) |
| 58 | + if exts: |
| 59 | + return ", ".join(f"{e} extension" for e in exts) |
| 60 | + return "—" |
| 61 | + |
| 62 | + |
| 63 | +def build_preset_table(catalog: dict) -> str: |
| 64 | + """Build a markdown table for presets.""" |
| 65 | + entries = catalog.get("presets", {}) |
| 66 | + lines: list[str] = [] |
| 67 | + lines.append("| Preset | Purpose | Provides | Requires | URL |") |
| 68 | + lines.append("|--------|---------|----------|----------|-----|") |
| 69 | + |
| 70 | + for _id in sorted(entries): |
| 71 | + e = entries[_id] |
| 72 | + name = e.get("name", _id) |
| 73 | + desc = e.get("description", "") |
| 74 | + provides = _provides_str_preset(e.get("provides", {})) |
| 75 | + requires = _requires_str_preset(e.get("requires", {})) |
| 76 | + repo_url = e.get("repository", "") |
| 77 | + repo_name = _repo_display_name(repo_url) |
| 78 | + lines.append( |
| 79 | + f"| {name} | {desc} | {provides} | {requires} " |
| 80 | + f"| [{repo_name}]({repo_url}) |" |
| 81 | + ) |
| 82 | + |
| 83 | + return "\n".join(lines) |
| 84 | + |
| 85 | + |
| 86 | +def _provides_str_extension(provides: dict) -> str: |
| 87 | + parts: list[str] = [] |
| 88 | + c = provides.get("commands", 0) |
| 89 | + h = provides.get("hooks", 0) |
| 90 | + if c: |
| 91 | + parts.append(f"{c} command{'s' if c != 1 else ''}") |
| 92 | + if h: |
| 93 | + parts.append(f"{h} hook{'s' if h != 1 else ''}") |
| 94 | + return ", ".join(parts) or "—" |
| 95 | + |
| 96 | + |
| 97 | +def build_extension_table(catalog: dict) -> str: |
| 98 | + """Build a markdown table for extensions.""" |
| 99 | + entries = catalog.get("extensions", {}) |
| 100 | + lines: list[str] = [] |
| 101 | + lines.append("| Extension | Purpose | Provides | URL |") |
| 102 | + lines.append("|-----------|---------|----------|-----|") |
| 103 | + |
| 104 | + for _id in sorted(entries): |
| 105 | + e = entries[_id] |
| 106 | + name = e.get("name", _id) |
| 107 | + desc = e.get("description", "") |
| 108 | + provides = _provides_str_extension(e.get("provides", {})) |
| 109 | + repo_url = e.get("repository", "") |
| 110 | + repo_name = _repo_display_name(repo_url) |
| 111 | + lines.append( |
| 112 | + f"| {name} | {desc} | {provides} " |
| 113 | + f"| [{repo_name}]({repo_url}) |" |
| 114 | + ) |
| 115 | + |
| 116 | + return "\n".join(lines) |
| 117 | + |
| 118 | + |
| 119 | +BUILDERS = { |
| 120 | + "preset": build_preset_table, |
| 121 | + "extension": build_extension_table, |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | +# --------------------------------------------------------------------------- |
| 126 | +# File updater |
| 127 | +# --------------------------------------------------------------------------- |
| 128 | + |
| 129 | +def update_file(path: Path, table: str) -> bool: |
| 130 | + """Replace content between markers in *path*. Returns True if updated.""" |
| 131 | + content = path.read_text() |
| 132 | + |
| 133 | + pattern = re.compile( |
| 134 | + rf"({re.escape(START_MARKER)})\n.*?\n({re.escape(END_MARKER)})", |
| 135 | + re.DOTALL, |
| 136 | + ) |
| 137 | + |
| 138 | + if not pattern.search(content): |
| 139 | + return False |
| 140 | + |
| 141 | + new_content = pattern.sub(rf"\1\n{table}\n\2", content) |
| 142 | + |
| 143 | + if new_content != content: |
| 144 | + path.write_text(new_content) |
| 145 | + return True |
| 146 | + return False |
| 147 | + |
| 148 | + |
| 149 | +# --------------------------------------------------------------------------- |
| 150 | +# Main |
| 151 | +# --------------------------------------------------------------------------- |
| 152 | + |
| 153 | +def main() -> None: |
| 154 | + parser = argparse.ArgumentParser(description=__doc__) |
| 155 | + parser.add_argument( |
| 156 | + "--catalog", required=True, |
| 157 | + help="Path to catalog.community.json", |
| 158 | + ) |
| 159 | + parser.add_argument( |
| 160 | + "--type", required=True, choices=list(BUILDERS), |
| 161 | + help="Catalog type", |
| 162 | + ) |
| 163 | + parser.add_argument( |
| 164 | + "--target", |
| 165 | + help="Markdown file to update (must contain marker comments)", |
| 166 | + ) |
| 167 | + args = parser.parse_args() |
| 168 | + |
| 169 | + with open(args.catalog) as f: |
| 170 | + catalog = json.load(f) |
| 171 | + |
| 172 | + builder = BUILDERS[args.type] |
| 173 | + table = builder(catalog) |
| 174 | + |
| 175 | + if args.target: |
| 176 | + target = Path(args.target) |
| 177 | + if not target.exists(): |
| 178 | + print(f"Error: target file not found: {target}", file=sys.stderr) |
| 179 | + sys.exit(1) |
| 180 | + if update_file(target, table): |
| 181 | + print(f"Updated {target}") |
| 182 | + else: |
| 183 | + print( |
| 184 | + f"Warning: markers {START_MARKER} / {END_MARKER} not found " |
| 185 | + f"in {target}. Printing table to stdout.", |
| 186 | + file=sys.stderr, |
| 187 | + ) |
| 188 | + print(table) |
| 189 | + else: |
| 190 | + print(table) |
| 191 | + |
| 192 | + |
| 193 | +if __name__ == "__main__": |
| 194 | + main() |
0 commit comments