#!/usr/bin/env python3 """ static/build.py --------------- Compile the CollabCode SCSS design system to CSS. Usage: python static/build.py # compile once → static/styles/main.css python static/build.py --watch # recompile on file-system changes python static/build.py --minify # compile minified (for production) The compiled main.css is committed alongside the SCSS source so that app.py can fall back to the pre-compiled file if libsass is not installed. """ import sys import time import argparse from pathlib import Path STYLES_DIR = Path(__file__).parent / "styles" ENTRY_POINT = STYLES_DIR / "main.scss" OUTPUT_FILE = STYLES_DIR / "main.css" def compile_scss(minify: bool = False) -> str: # Prefer Dart Sass CLI when available (supports @use and newer features). import shutil, subprocess sass_cli = shutil.which("sass") output_style = "compressed" if minify else "expanded" if sass_cli: style_flag = "--style=compressed" if minify else "--style=expanded" cmd = [sass_cli, "--no-source-map", style_flag, str(ENTRY_POINT), str(OUTPUT_FILE)] subprocess.check_call(cmd) return OUTPUT_FILE.read_text(encoding="utf-8") # Fallback to the Python `sass` package (libsass) if Dart Sass CLI isn't available. import sass # type: ignore output_style = "compressed" if minify else "expanded" return sass.compile( filename=str(ENTRY_POINT), output_style=output_style, include_paths=[str(STYLES_DIR)], ) def build(minify: bool = False) -> None: print(f"[CollabCode] Compiling {ENTRY_POINT.name} …", end=" ", flush=True) t0 = time.perf_counter() try: css = compile_scss(minify=minify) except Exception as e: print(f"\n[CollabCode] SCSS compile error:\n{e}") sys.exit(1) OUTPUT_FILE.write_text(css, encoding="utf-8") ms = (time.perf_counter() - t0) * 1000 size_kb = len(css.encode()) / 1024 print(f"done ({ms:.0f} ms, {size_kb:.1f} KB) → {OUTPUT_FILE}") def watch() -> None: """Re-compile whenever any .scss file in STYLES_DIR changes.""" print(f"[CollabCode] Watching {STYLES_DIR} for changes (Ctrl+C to stop)") mtimes: dict[Path, float] = {} def get_mtimes() -> dict[Path, float]: return {p: p.stat().st_mtime for p in STYLES_DIR.glob("**/*.scss")} mtimes = get_mtimes() build() try: while True: time.sleep(0.5) new_mtimes = get_mtimes() changed = [p for p, t in new_mtimes.items() if mtimes.get(p) != t] if changed: print(f"[CollabCode] Changed: {', '.join(p.name for p in changed)}") build() mtimes = new_mtimes except KeyboardInterrupt: print("\n[CollabCode] Watch stopped.") if __name__ == "__main__": parser = argparse.ArgumentParser(description="CollabCode SCSS build tool") parser.add_argument("--watch", action="store_true", help="Watch for changes") parser.add_argument("--minify", action="store_true", help="Produce minified output") args = parser.parse_args() if args.watch: watch() else: build(minify=args.minify)