Files
mtg-builder/scripts/update_docs.py
2026-02-20 00:13:44 +01:00

103 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""
Generate README.md and AGENTS.md from templates.
Usage:
python scripts/update_docs.py
"""
import json
from pathlib import Path
SCRIPT_DIR = Path(__file__).parent
PROJECT_ROOT = SCRIPT_DIR.parent
TEMPLATES_DIR = PROJECT_ROOT / "docs" / "templates"
DECKS_DIR = PROJECT_ROOT / "decks"
COLLECTION_DIR = PROJECT_ROOT / "collection_hydrated"
def load_decks() -> list[dict]:
"""Load all deck JSON files."""
decks = []
if DECKS_DIR.exists():
for path in sorted(DECKS_DIR.glob("*.json")):
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
data["filename"] = path.stem
decks.append(data)
return decks
def load_collection_stats() -> dict:
"""Get collection statistics."""
stats = {
"total_cards": 0,
"unique_cards": 0,
"by_type": {}
}
deck_file = COLLECTION_DIR / "deck.json"
if deck_file.exists():
with open(deck_file, "r", encoding="utf-8") as f:
cards = json.load(f)
stats["unique_cards"] = len(cards)
stats["total_cards"] = sum(c.get("count", 1) for c in cards)
for category in ["creatures", "instants", "sorceries", "artifacts", "enchantments", "lands", "planeswalkers"]:
cat_file = COLLECTION_DIR / f"{category}.json"
if cat_file.exists():
with open(cat_file, "r", encoding="utf-8") as f:
cards = json.load(f)
stats["by_type"][category] = len(cards)
return stats
def generate_deck_table(decks: list[dict]) -> str:
"""Generate markdown table of decks."""
if not decks:
return "*No decks defined yet.*"
lines = ["| Deck | Colors | Commander | Archetype |", "|------|--------|-----------|-----------|"]
for deck in decks:
name = deck.get("name", deck.get("filename", "Unknown"))
colors = deck.get("colors", [])
color_str = "".join(colors) if colors else "Colorless"
commander = deck.get("commander", "Unknown")
archetype = deck.get("archetype", "-")
lines.append(f"| {name} | {color_str} | {commander} | {archetype} |")
return "\n".join(lines)
def generate_collection_stats(stats: dict) -> str:
"""Generate collection statistics section."""
lines = [f"- **Total cards:** {stats['total_cards']}", f"- **Unique cards:** {stats['unique_cards']}"]
if stats["by_type"]:
lines.append("- **By type:**")
for category, count in sorted(stats["by_type"].items()):
lines.append(f" - {category.title()}: {count}")
return "\n".join(lines)
def generate_docs() -> None:
"""Generate README.md and AGENTS.md from templates."""
decks = load_decks()
stats = load_collection_stats()
deck_table = generate_deck_table(decks)
collection_stats = generate_collection_stats(stats)
for template_name in ["README.template.md", "AGENTS.template.md"]:
template_path = TEMPLATES_DIR / template_name
if not template_path.exists():
print(f"Template not found: {template_path}")
continue
with open(template_path, "r", encoding="utf-8") as f:
content = f.read()
content = content.replace("{{DECK_TABLE}}", deck_table)
content = content.replace("{{COLLECTION_STATS}}", collection_stats)
output_name = template_name.replace(".template", "")
output_path = PROJECT_ROOT / output_name
with open(output_path, "w", encoding="utf-8") as f:
f.write(content)
print(f"Generated: {output_path}")
if __name__ == "__main__":
generate_docs()