SPB Git

Repository Public

sync-logos.py 3.5 KB 59 lines python

1#!/usr/bin/env python32# Author: Simon-Pierre Boucher — contact@spboucher.ai3# File: Scripts/sync-logos.py4# Description: Generates the native logo assets (Assets.xcassets/Logos/<slug>.imageset, vector, template / original) and LogoRegistry.json from the canonical provider-logo registry shared by web, macOS and iOS.5#6# Usage: Scripts/sync-logos.py [path/to/_spchat-nextgen-shared]7#   Source: <shared>/logos/*.svg + <shared>/logos.generated.json (artwork from @lobehub/icons-static-svg, MIT collection;8#   each mark belongs to its owner). Monochrome marks use `currentColor` and are imported as template images; the9#   `<slug>-color.svg` variants (registry `color: true`) are imported as original images. No logo is drawn by hand and10#   nothing is fetched at runtime: a maker without an entry gets a neutral monogram in the app.11import json, re, shutil, sys12from pathlib import Path1314ROOT = Path(__file__).resolve().parent.parent15SHARED = Path(sys.argv[1]) if len(sys.argv) > 1 else ROOT.parent / "_spchat-nextgen-shared"16SRC = SHARED / "logos"17REGISTRY = SHARED / "logos.generated.json"18ASSETS = ROOT / "SPChat" / "Resources" / "Assets.xcassets" / "Logos"19OUT_JSON = ROOT / "SPChat" / "Resources" / "LogoRegistry.json"2021def main() -> int:22    if not SRC.is_dir() or not REGISTRY.is_file():23        print(f"ERROR: {SRC} or {REGISTRY} not found", file=sys.stderr); return 124    registry = json.loads(REGISTRY.read_text())25    makers = registry["makers"]26    if ASSETS.exists(): shutil.rmtree(ASSETS)27    ASSETS.mkdir(parents=True)28    (ASSETS / "Contents.json").write_text(json.dumps({"info": {"author": "xcode", "version": 1}, "properties": {"provides-namespace": True}}, indent=2) + "\n")29    mono = color = 0; missing = []30    for slug in sorted(makers):31        if not re.fullmatch(r"[a-z0-9-]+", slug): print(f"ERROR: unexpected slug {slug!r}", file=sys.stderr); return 132        for variant, intent in (("", "template"), ("-color", "original")):33            if variant and not makers[slug].get("color"): continue34            svg = SRC / f"{slug}{variant}.svg"35            if not svg.is_file(): missing.append(svg.name); continue36            text = svg.read_text()37            # template images only keep the alpha channel: `currentColor` is resolved to an opaque colour for the asset compiler38            if not variant: text = text.replace("currentColor", "#000000")39            imageset = ASSETS / f"{slug}{variant}.imageset"; imageset.mkdir()40            (imageset / svg.name).write_text(text)41            (imageset / "Contents.json").write_text(json.dumps({42                "images": [{"filename": svg.name, "idiom": "universal"}],43                "info": {"author": "xcode", "version": 1},44                "properties": {"preserves-vector-representation": True, "template-rendering-intent": intent},45            }, indent=2) + "\n")46            if variant: color += 147            else: mono += 148    OUT_JSON.write_text(json.dumps({49        "source": registry.get("source"), "license": registry.get("license"), "generatedAt": registry.get("generatedAt"),50        "makers": {s: {"name": m["name"], "color": bool(m.get("color"))} for s, m in sorted(makers.items())},51        "aliases": dict(sorted(registry.get("aliases", {}).items())),52    }, indent=1, ensure_ascii=False) + "\n")53    print(f"{mono} monochrome + {color} colour logos → {ASSETS.relative_to(ROOT)} ; registry → {OUT_JSON.relative_to(ROOT)}")54    if missing: print("missing source files: " + ", ".join(missing), file=sys.stderr); return 155    return 05657if __name__ == "__main__":58    sys.exit(main())59
Files main
Menu

Theme