comparison 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
comparison
equal deleted inserted replaced
26:28e1fc486be5 27:75deda08db90
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 """ 2 """
3 build the runnable game into www/: the game data from art/ and config/ into 3 build the runnable game into www/: the game data from art/ and config/ into
4 www/assets/ and www/data/, and a copy of the hand-written files in game/. 4 www/assets/ and www/data/, and a copy of the hand-written files in game/.
5 5
6 usage: python3 tools/build.py [--force] [--watch] 6 usage: python3 tools/build.py [--force] [--watch] [--release]
7 7
8 steps: sprites, tower levels, maps, masks, sounds, config, checks, previews, 8 steps: sprites, tower levels, maps, masks, sounds, config, checks, previews,
9 game files. a step is skipped when all its outputs are newer than its inputs 9 game files. a step is skipped when all its outputs are newer than its inputs
10 and the build scripts; --force runs every step. --watch reruns the build 10 and the build scripts; --force runs every step. --watch reruns the build
11 whenever a file in art/, art/sounds/, art/digits/, config/ or game/ changes. 11 whenever a file in art/, art/sounds/, art/digits/, config/ or game/ changes.
12 --release leaves the debug code and switches out: www/js/debug.js is
13 written empty and config.js has no [debug] section.
12 14
13 requires python 3.11 or later and pillow. 15 requires python 3.11 or later and pillow.
14 """ 16 """
15 17
16 import base64 18 import base64
395 with Image.open(p) as im: 397 with Image.open(p) as im:
396 out[p.stem] = list(im.size) 398 out[p.stem] = list(im.size)
397 return out 399 return out
398 400
399 401
400 def step_config(cfg, force, assets_changed): 402 def step_config(cfg, force, assets_changed, release):
401 src = CONFIG / "game.toml" 403 src = CONFIG / "game.toml"
402 dst = DATA / "config.js" 404 dst = DATA / "config.js"
403 if not stale([src], [dst], force) and not assets_changed: 405 # a switch between release and debug builds rewrites the file
406 was_release = dst.exists() and '"debug":' not in dst.read_text()
407 if not stale([src], [dst], force) and not assets_changed and was_release == release:
404 print("config: up to date") 408 print("config: up to date")
405 return 409 return
406 out = {k: v for k, v in cfg.items() if k != "map"} 410 out = {k: v for k, v in cfg.items() if k != "map" and not (release and k == "debug")}
407 out["tower"] = {tid: {k: tower_levels(v) if k in TOWER_BY_LEVEL else v 411 out["tower"] = {tid: {k: tower_levels(v) if k in TOWER_BY_LEVEL else v
408 for k, v in tw.items()} 412 for k, v in tw.items()}
409 for tid, tw in cfg["tower"].items()} 413 for tid, tw in cfg["tower"].items()}
410 out["maps"] = [] 414 out["maps"] = []
411 for m in range(MAPS): 415 for m in range(MAPS):
502 print(f"previews: wrote {PREVIEW.relative_to(ROOT)}/") 506 print(f"previews: wrote {PREVIEW.relative_to(ROOT)}/")
503 507
504 508
505 # 9. game files 509 # 9. game files
506 510
511 # debug switches and drawing, left out of a release build
512 DEBUG_JS = Path("js") / "debug.js"
513
507 def game_files(): 514 def game_files():
508 """the hand-written files in game/, as paths relative to it.""" 515 """the hand-written files in game/, as paths relative to it."""
509 return sorted(p.relative_to(GAME) for p in GAME.rglob("*") 516 return sorted(p.relative_to(GAME) for p in GAME.rglob("*")
510 if p.is_file() and p.name != ".DS_Store") 517 if p.is_file() and p.name != ".DS_Store")
511 518
512 519
513 def step_game(force): 520 def step_game(force, release):
514 """ 521 """
515 copy the files in game/ to www/ when they differ in time or size, and 522 copy the files in game/ to www/ when they differ in time or size, and
516 remove files from www/ that are no longer in game/. www/assets/ and 523 remove files from www/ that are no longer in game/. www/assets/ and
517 www/data/ are left alone, as the other steps write them. 524 www/data/ are left alone, as the other steps write them. a release
525 build writes js/debug.js empty.
518 """ 526 """
519 files = game_files() 527 files = game_files()
520 copied = 0 528 copied = 0
521 for rel in files: 529 for rel in files:
522 src, dst = GAME / rel, WWW / rel 530 src, dst = GAME / rel, WWW / rel
531 if release and rel == DEBUG_JS:
532 if force or not dst.exists() or dst.stat().st_size:
533 write_text(dst, "")
534 copied += 1
535 continue
523 a = src.stat() 536 a = src.stat()
524 if not force and dst.exists(): 537 if not force and dst.exists():
525 b = dst.stat() 538 b = dst.stat()
526 if b.st_mtime == a.st_mtime and b.st_size == a.st_size: 539 if b.st_mtime == a.st_mtime and b.st_size == a.st_size:
527 continue 540 continue
543 print(f"game files: copied {copied}, removed {removed}") 556 print(f"game files: copied {copied}, removed {removed}")
544 else: 557 else:
545 print("game files: up to date") 558 print("game files: up to date")
546 559
547 560
548 def build(force): 561 def build(force, release):
549 warnings.clear() 562 warnings.clear()
550 try: 563 try:
551 cfg = tomllib.loads((CONFIG / "game.toml").read_text()) 564 cfg = tomllib.loads((CONFIG / "game.toml").read_text())
552 check_config(cfg) 565 check_config(cfg)
553 except (tomllib.TOMLDecodeError, ConfigError) as e: 566 except (tomllib.TOMLDecodeError, ConfigError) as e:
556 changed = step_sprites(force) 569 changed = step_sprites(force)
557 changed |= step_towers(force) 570 changed |= step_towers(force)
558 step_maps(force) 571 step_maps(force)
559 maps, masks_changed = step_masks(cfg, force) 572 maps, masks_changed = step_masks(cfg, force)
560 step_sounds(cfg, force) 573 step_sounds(cfg, force)
561 step_config(cfg, force, changed) 574 step_config(cfg, force, changed, release)
562 step_checks(cfg, maps) 575 step_checks(cfg, maps)
563 step_previews(cfg, maps, force, changed or masks_changed) 576 step_previews(cfg, maps, force, changed or masks_changed)
564 step_game(force) 577 step_game(force, release)
565 print(f"\n{len(warnings)} warning(s)") 578 print(f"\n{len(warnings)} warning(s)")
566 return True 579 return True
567 580
568 581
569 def watch(): 582 def watch(release):
570 def snapshot(): 583 def snapshot():
571 files = (list(ART.iterdir()) + list(SOUNDS.glob("*.mp3")) + list(DIGITS.glob("*.png")) 584 files = (list(ART.iterdir()) + list(SOUNDS.glob("*.mp3")) + list(DIGITS.glob("*.png"))
572 + list(CONFIG.iterdir()) + [GAME / p for p in game_files()] + TOOLS) 585 + list(CONFIG.iterdir()) + [GAME / p for p in game_files()] + TOOLS)
573 return {p: p.stat().st_mtime for p in files if p.is_file()} 586 return {p: p.stat().st_mtime for p in files if p.is_file()}
574 587
577 now = snapshot() 590 now = snapshot()
578 if now != seen: 591 if now != seen:
579 seen = now 592 seen = now
580 print(time.strftime("\n== build %H:%M:%S")) 593 print(time.strftime("\n== build %H:%M:%S"))
581 try: 594 try:
582 build(False) 595 build(False, release)
583 except Exception as e: # keep watching after a failed build 596 except Exception as e: # keep watching after a failed build
584 print("build failed:", e) 597 print("build failed:", e)
585 time.sleep(1) 598 time.sleep(1)
586 599
587 600
588 def main(): 601 def main():
589 args = sys.argv[1:] 602 args = sys.argv[1:]
603 release = "--release" in args
590 if "--watch" in args: 604 if "--watch" in args:
591 # show each line at once, also when the output goes to a file 605 # show each line at once, also when the output goes to a file
592 sys.stdout.reconfigure(line_buffering=True) 606 sys.stdout.reconfigure(line_buffering=True)
593 try: 607 try:
594 watch() 608 watch(release)
595 except KeyboardInterrupt: 609 except KeyboardInterrupt:
596 pass 610 pass
597 return 0 611 return 0
598 return 0 if build("--force" in args) else 1 612 return 0 if build("--force" in args, release) else 1
599 613
600 614
601 if __name__ == "__main__": 615 if __name__ == "__main__":
602 sys.exit(main()) 616 sys.exit(main())