File size: 3,221 Bytes
795a91a ba2c0b3 795a91a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | #!/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)
|