diff tools/build.py @ 17:fe680946164b

reorganised repo
author ark
date 2026-10-20 00:12 +0200
parents 30bd10284fd8
children 3a831478eda4
line wrap: on
line diff
--- a/tools/build.py	Mon Oct 19 23:47:22 2026 +0200
+++ b/tools/build.py	Tue Oct 20 00:12:36 2026 +0200
@@ -1,14 +1,14 @@
 #!/usr/bin/env python3
 """
-build the game data from art/ and config/ into game/assets/ and game/data/.
+build the runnable game into www/: the game data from art/ and config/ into
+www/assets/ and www/data/, and a copy of the hand-written files in game/.
 
 usage: python3 tools/build.py [--force] [--watch]
 
-steps: sprites, tower levels, maps, masks, sounds, config, checks, previews.
-a step
-is skipped when all its outputs are newer than its inputs and the build
-scripts; --force runs every step. --watch reruns the build whenever a file
-in art/, art/sounds/, art/digits/ or config/ changes.
+steps: sprites, tower levels, maps, masks, sounds, config, checks, previews,
+game files. a step is skipped when all its outputs are newer than its inputs
+and the build scripts; --force runs every step. --watch reruns the build
+whenever a file in art/, art/sounds/, art/digits/, config/ or game/ changes.
 
 requires python 3.11 or later and pillow.
 """
@@ -17,6 +17,7 @@
 import json
 import math
 import re
+import shutil
 import sys
 import time
 import tomllib
@@ -36,8 +37,10 @@
 SOUNDS = ART / "sounds"
 DIGITS = ART / "digits"
 CONFIG = ROOT / "config"
-ASSETS = ROOT / "game" / "assets"
-DATA = ROOT / "game" / "data"
+GAME = ROOT / "game"
+WWW = ROOT / "www"
+ASSETS = WWW / "assets"
+DATA = WWW / "data"
 TEMP = ROOT / "temp"
 PREVIEW = TEMP / "preview"
 # route data of the last mask step, read when the step is skipped
@@ -289,7 +292,7 @@
 
 def step_sounds(cfg, force):
     """
-    embed art/sounds/*.mp3 as base64 in game/data/sounds.js, grouped by
+    embed art/sounds/*.mp3 as base64 in www/data/sounds.js, grouped by
     event: NAME.mp3 or the variants NAME_1.mp3, NAME_2.mp3 and so on.
     """
     inputs = sorted(SOUNDS.glob("*.mp3"))
@@ -403,7 +406,7 @@
 
 
 def sprite_table():
-    """name -> [width, height] of every image in game/assets/ except maps."""
+    """name -> [width, height] of every image in www/assets/ except maps."""
     out = {}
     for p in sorted(ASSETS.glob("*.png")):
         with Image.open(p) as im:
@@ -430,7 +433,7 @@
     write_text(dst, HEADER.format(src="config/game.toml")
                + "window.MM = window.MM || {};\nMM.config = "
                + json.dumps(out, indent=1) + ";\n")
-    print("config: wrote game/data/config.js")
+    print("config: wrote www/data/config.js")
 
 
 # 7. checks
@@ -520,6 +523,49 @@
     print(f"previews: wrote {PREVIEW.relative_to(ROOT)}/")
 
 
+# 9. game files
+
+def game_files():
+    """the hand-written files in game/, as paths relative to it."""
+    return sorted(p.relative_to(GAME) for p in GAME.rglob("*")
+                  if p.is_file() and p.name != ".DS_Store")
+
+
+def step_game(force):
+    """
+    copy the files in game/ to www/ when they differ in time or size, and
+    remove files from www/ that are no longer in game/. www/assets/ and
+    www/data/ are left alone, as the other steps write them.
+    """
+    files = game_files()
+    copied = 0
+    for rel in files:
+        src, dst = GAME / rel, WWW / rel
+        a = src.stat()
+        if not force and dst.exists():
+            b = dst.stat()
+            if b.st_mtime == a.st_mtime and b.st_size == a.st_size:
+                continue
+        dst.parent.mkdir(parents=True, exist_ok=True)
+        shutil.copy2(src, dst)
+        copied += 1
+    keep = set(files)
+    removed = 0
+    for p in sorted(WWW.rglob("*"), reverse=True):
+        rel = p.relative_to(WWW)
+        if rel.parts[0] in (ASSETS.name, DATA.name) or rel in keep:
+            continue
+        if p.is_file():
+            p.unlink()
+            removed += 1
+        elif not any(p.iterdir()):
+            p.rmdir()
+    if copied or removed:
+        print(f"game files: copied {copied}, removed {removed}")
+    else:
+        print("game files: up to date")
+
+
 def build(force):
     warnings.clear()
     try:
@@ -536,6 +582,7 @@
     step_config(cfg, force, changed)
     step_checks(cfg, maps)
     step_previews(cfg, maps, force, changed or masks_changed)
+    step_game(force)
     print(f"\n{len(warnings)} warning(s)")
     return True
 
@@ -543,7 +590,7 @@
 def watch():
     def snapshot():
         files = (list(ART.iterdir()) + list(SOUNDS.glob("*.mp3")) + list(DIGITS.glob("*.png"))
-                 + list(CONFIG.iterdir()) + TOOLS)
+                 + list(CONFIG.iterdir()) + [GAME / p for p in game_files()] + TOOLS)
         return {p: p.stat().st_mtime for p in files if p.is_file()}
 
     seen = None