generate-version-metadata.py

  1#!/usr/bin/env python3
  2"""Generate version metadata for GitHub Pages.
  3
  4Generates release.json, commits.json, and index.html.
  5"""
  6
  7import json
  8import subprocess
  9import sys
 10from pathlib import Path
 11
 12
 13def generate_release_json(output_dir: Path) -> None:
 14    """Generate release.json with latest release information."""
 15    # Get latest tag - fail if none exists
 16    result = subprocess.run(
 17        ["git", "describe", "--tags", "--abbrev=0"],
 18        capture_output=True,
 19        text=True,
 20    )
 21    if result.returncode != 0:
 22        print("ERROR: No tags found. Run this after creating a release.", file=sys.stderr)
 23        sys.exit(1)
 24
 25    latest_tag = result.stdout.strip()
 26    latest_commit = subprocess.check_output(
 27        ["git", "rev-list", "-n", "1", latest_tag], text=True
 28    ).strip()
 29    latest_commit_short = latest_commit[:7]
 30    latest_commit_time = subprocess.check_output(
 31        ["git", "show", "-s", "--format=%cI", latest_commit], text=True
 32    ).strip()
 33    published_at = subprocess.check_output(
 34        ["git", "show", "-s", "--format=%cI", latest_tag], text=True
 35    ).strip()
 36
 37    version = latest_tag[1:] if latest_tag.startswith("v") else latest_tag
 38
 39    release_info = {
 40        "tag_name": latest_tag,
 41        "version": version,
 42        "commit": latest_commit_short,
 43        "commit_full": latest_commit,
 44        "commit_time": latest_commit_time,
 45        "published_at": published_at,
 46        "download_urls": {
 47            "darwin_amd64": f"https://github.com/boldsoftware/shelley/releases/download/{latest_tag}/shelley_darwin_amd64",
 48            "darwin_arm64": f"https://github.com/boldsoftware/shelley/releases/download/{latest_tag}/shelley_darwin_arm64",
 49            "linux_amd64": f"https://github.com/boldsoftware/shelley/releases/download/{latest_tag}/shelley_linux_amd64",
 50            "linux_arm64": f"https://github.com/boldsoftware/shelley/releases/download/{latest_tag}/shelley_linux_arm64",
 51        },
 52        "checksums_url": f"https://github.com/boldsoftware/shelley/releases/download/{latest_tag}/checksums.txt",
 53    }
 54
 55    output_path = output_dir / "release.json"
 56    with open(output_path, "w") as f:
 57        json.dump(release_info, f, indent=2)
 58    print(f"Generated {output_path}")
 59
 60
 61def generate_commits_json(output_dir: Path, count: int = 500) -> None:
 62    """Generate commits.json with recent commits."""
 63    output = subprocess.check_output(
 64        ["git", "log", f"--pretty=format:%h%x00%s", f"-{count}", "HEAD"],
 65        text=True,
 66    )
 67
 68    commits = []
 69    for line in output.strip().split("\n"):
 70        if "\x00" in line:
 71            sha, subject = line.split("\x00", 1)
 72            commits.append({"sha": sha, "subject": subject})
 73
 74    output_path = output_dir / "commits.json"
 75    with open(output_path, "w") as f:
 76        json.dump(commits, f, indent=2)
 77    print(f"Generated {output_path} with {len(commits)} commits")
 78
 79
 80def generate_index_html(output_dir: Path) -> None:
 81    """Generate index.html."""
 82    html = """<!DOCTYPE html>
 83<html>
 84<head><title>Shelley</title></head>
 85<body>
 86<p><a href="https://github.com/boldsoftware/shelley">github.com/boldsoftware/shelley</a></p>
 87<ul>
 88<li><a href="release.json">release.json</a></li>
 89<li><a href="commits.json">commits.json</a></li>
 90</ul>
 91</body>
 92</html>
 93"""
 94    output_path = output_dir / "index.html"
 95    with open(output_path, "w") as f:
 96        f.write(html)
 97    print(f"Generated {output_path}")
 98
 99
100def main() -> None:
101    output_dir = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("_site")
102    output_dir.mkdir(parents=True, exist_ok=True)
103
104    generate_release_json(output_dir)
105    generate_commits_json(output_dir)
106    generate_index_html(output_dir)
107
108
109if __name__ == "__main__":
110    main()