|
| 1 | +"""Build the GitHub Pages bundle with inlined assets.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import json |
| 5 | +import shutil |
| 6 | +import time |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +ROOT = Path(__file__).resolve().parents[1] |
| 10 | +DIST = ROOT / "dist" |
| 11 | +CI_REPORTS = ROOT / "ci_reports" |
| 12 | + |
| 13 | +CSS_START = "<!-- build:css -->" |
| 14 | +CSS_END = "<!-- endbuild css -->" |
| 15 | +JS_START = "<!-- build:js -->" |
| 16 | +JS_END = "<!-- endbuild js -->" |
| 17 | + |
| 18 | + |
| 19 | +def replace_block(source: str, start_marker: str, end_marker: str, replacement: str) -> str: |
| 20 | + """Replace the block delimited by the provided markers. |
| 21 | +
|
| 22 | + Raises a ValueError when the markers are missing so the workflow fails fast |
| 23 | + instead of silently producing a broken bundle. |
| 24 | + """ |
| 25 | + |
| 26 | + try: |
| 27 | + start_index = source.index(start_marker) + len(start_marker) |
| 28 | + end_index = source.index(end_marker) |
| 29 | + except ValueError as exc: # pragma: no cover - defensive guard |
| 30 | + raise ValueError( |
| 31 | + "Build markers missing in index.html. Ensure the start and end " |
| 32 | + f"markers {start_marker!r} and {end_marker!r} exist." |
| 33 | + ) from exc |
| 34 | + |
| 35 | + before = source[:start_index] |
| 36 | + after = source[end_index:] |
| 37 | + return f"{before}\n{replacement}\n{after}" |
| 38 | + |
| 39 | + |
| 40 | +def build() -> None: |
| 41 | + start = time.perf_counter() |
| 42 | + |
| 43 | + if DIST.exists(): |
| 44 | + shutil.rmtree(DIST) |
| 45 | + DIST.mkdir(parents=True, exist_ok=True) |
| 46 | + |
| 47 | + index_html = (ROOT / "index.html").read_text(encoding="utf-8") |
| 48 | + css = (ROOT / "style.css").read_text(encoding="utf-8") |
| 49 | + javascript = (ROOT / "app.js").read_text(encoding="utf-8") |
| 50 | + |
| 51 | + inlined_index = replace_block( |
| 52 | + index_html, |
| 53 | + CSS_START, |
| 54 | + CSS_END, |
| 55 | + f"<style>\n{css}\n</style>", |
| 56 | + ) |
| 57 | + inlined_index = replace_block( |
| 58 | + inlined_index, |
| 59 | + JS_START, |
| 60 | + JS_END, |
| 61 | + f"<script>\n{javascript}\n</script>", |
| 62 | + ) |
| 63 | + |
| 64 | + (DIST / "index.html").write_text(inlined_index, encoding="utf-8") |
| 65 | + |
| 66 | + # Keep the original assets alongside the inlined bundle for local debugging |
| 67 | + # and to avoid breaking any bookmarked resources. |
| 68 | + shutil.copy2(ROOT / "style.css", DIST / "style.css") |
| 69 | + shutil.copy2(ROOT / "app.js", DIST / "app.js") |
| 70 | + shutil.copy2(ROOT / "ai-instruct.txt", DIST / "ai-instruct.txt") |
| 71 | + |
| 72 | + duration = time.perf_counter() - start |
| 73 | + CI_REPORTS.mkdir(parents=True, exist_ok=True) |
| 74 | + (CI_REPORTS / "build_status.json").write_text( |
| 75 | + json.dumps( |
| 76 | + { |
| 77 | + "status": "succeeded", |
| 78 | + "artifact": "github-pages", |
| 79 | + "duration": duration, |
| 80 | + }, |
| 81 | + indent=2, |
| 82 | + ), |
| 83 | + encoding="utf-8", |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + build() |
0 commit comments