diff tools/build.py @ 27:75deda08db90 0.9.1

game debug functions separated into own file; release build option, distribution script
author ark
date 2026-10-20 17:19 +0200
parents 3f2811aebb3d
children 85d23328a215
line wrap: on
line diff
--- a/tools/build.py	Tue Oct 20 16:06:50 2026 +0200
+++ b/tools/build.py	Tue Oct 20 17:19:40 2026 +0200
@@ -3,12 +3,14 @@
 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]
+usage: python3 tools/build.py [--force] [--watch] [--release]
 
 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.
+--release leaves the debug code and switches out: www/js/debug.js is
+written empty and config.js has no [debug] section.
 
 requires python 3.11 or later and pillow.
 """
@@ -397,13 +399,15 @@
     return out
 
 
-def step_config(cfg, force, assets_changed):
+def step_config(cfg, force, assets_changed, release):
     src = CONFIG / "game.toml"
     dst = DATA / "config.js"
-    if not stale([src], [dst], force) and not assets_changed:
+    # a switch between release and debug builds rewrites the file
+    was_release = dst.exists() and '"debug":' not in dst.read_text()
+    if not stale([src], [dst], force) and not assets_changed and was_release == release:
         print("config: up to date")
         return
-    out = {k: v for k, v in cfg.items() if k != "map"}
+    out = {k: v for k, v in cfg.items() if k != "map" and not (release and k == "debug")}
     out["tower"] = {tid: {k: tower_levels(v) if k in TOWER_BY_LEVEL else v
                           for k, v in tw.items()}
                     for tid, tw in cfg["tower"].items()}
@@ -504,22 +508,31 @@
 
 # 9. game files
 
+# debug switches and drawing, left out of a release build
+DEBUG_JS = Path("js") / "debug.js"
+
 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):
+def step_game(force, release):
     """
     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.
+    www/data/ are left alone, as the other steps write them. a release
+    build writes js/debug.js empty.
     """
     files = game_files()
     copied = 0
     for rel in files:
         src, dst = GAME / rel, WWW / rel
+        if release and rel == DEBUG_JS:
+            if force or not dst.exists() or dst.stat().st_size:
+                write_text(dst, "")
+                copied += 1
+            continue
         a = src.stat()
         if not force and dst.exists():
             b = dst.stat()
@@ -545,7 +558,7 @@
         print("game files: up to date")
 
 
-def build(force):
+def build(force, release):
     warnings.clear()
     try:
         cfg = tomllib.loads((CONFIG / "game.toml").read_text())
@@ -558,15 +571,15 @@
     step_maps(force)
     maps, masks_changed = step_masks(cfg, force)
     step_sounds(cfg, force)
-    step_config(cfg, force, changed)
+    step_config(cfg, force, changed, release)
     step_checks(cfg, maps)
     step_previews(cfg, maps, force, changed or masks_changed)
-    step_game(force)
+    step_game(force, release)
     print(f"\n{len(warnings)} warning(s)")
     return True
 
 
-def watch():
+def watch(release):
     def snapshot():
         files = (list(ART.iterdir()) + list(SOUNDS.glob("*.mp3")) + list(DIGITS.glob("*.png"))
                  + list(CONFIG.iterdir()) + [GAME / p for p in game_files()] + TOOLS)
@@ -579,7 +592,7 @@
             seen = now
             print(time.strftime("\n== build %H:%M:%S"))
             try:
-                build(False)
+                build(False, release)
             except Exception as e:  # keep watching after a failed build
                 print("build failed:", e)
         time.sleep(1)
@@ -587,15 +600,16 @@
 
 def main():
     args = sys.argv[1:]
+    release = "--release" in args
     if "--watch" in args:
         # show each line at once, also when the output goes to a file
         sys.stdout.reconfigure(line_buffering=True)
         try:
-            watch()
+            watch(release)
         except KeyboardInterrupt:
             pass
         return 0
-    return 0 if build("--force" in args) else 1
+    return 0 if build("--force" in args, release) else 1
 
 
 if __name__ == "__main__":