SPB Git

Repository Public

sync-logos.py 4.4 KB 94 lines python

1#!/usr/bin/env python32# SP Chat iOS3# File: Scripts/sync-logos.py4# Description: Generates the native maker-logo assets from the canonical registry shared by the three SP Chat clients5#              (logos/*.svg + logos.generated.json): one vector imageset per logo in Assets.xcassets/Logos (template6#              rendering for the monochrome mark, original rendering for the "-color" variant) and the Swift registry7#              Core/Models/LogoRegistry.generated.swift (makers, colour flags, vendor aliases). No logo is drawn by hand.8#              usage: Scripts/sync-logos.py [path-to-shared-folder]9#10# Author: Simon-Pierre Boucher11# Contact: contact@spboucher.ai12import json13import re14import shutil15import sys16from pathlib import Path1718ROOT = Path(__file__).resolve().parent.parent19SHARED = Path(sys.argv[1]) if len(sys.argv) > 1 else ROOT.parent / "_spchat-nextgen-shared"20OUT = ROOT / "SPChat-iOS" / "Resources" / "Assets.xcassets" / "Logos"21SWIFT = ROOT / "SPChat-iOS" / "Core" / "Models" / "LogoRegistry.generated.swift"222324def normalise(svg: str) -> str:25    """Asset catalogs need an explicit size; `currentColor` becomes black (the template rendering tints it)."""26    svg = svg.replace("currentColor", "#000000")27    head = re.search(r"<svg\b[^>]*>", svg)28    if head and "width=" not in head.group(0):29        box = re.search(r'viewBox="([\d.\s-]+)"', head.group(0))30        w, h = (box.group(1).split()[2:4] if box else ("24", "24"))31        svg = svg.replace("<svg", f'<svg width="{w}" height="{h}"', 1)32    return svg333435def imageset(name: str, source: Path, template: bool) -> None:36    folder = OUT / f"{name}.imageset"37    folder.mkdir(parents=True)38    (folder / f"{name}.svg").write_text(normalise(source.read_text(encoding="utf-8")), encoding="utf-8")39    contents = {40        "images": [{"filename": f"{name}.svg", "idiom": "universal"}],41        "info": {"author": "xcode", "version": 1},42        "properties": {"preserves-vector-representation": True, "template-rendering-intent": "template" if template else "original"},43    }44    (folder / "Contents.json").write_text(json.dumps(contents, indent=2) + "\n", encoding="utf-8")454647def main() -> int:48    registry = json.loads((SHARED / "logos.generated.json").read_text(encoding="utf-8"))49    makers, aliases = registry["makers"], registry.get("aliases", {})50    if OUT.exists():51        shutil.rmtree(OUT)52    OUT.mkdir(parents=True)53    (OUT / "Contents.json").write_text(json.dumps({"info": {"author": "xcode", "version": 1}, "properties": {"provides-namespace": True}}, indent=2) + "\n", encoding="utf-8")54    mono = colour = 055    for svg in sorted((SHARED / "logos").glob("*.svg")):56        is_colour = svg.stem.endswith("-color")57        imageset(svg.stem, svg, template=not is_colour)58        colour += is_colour59        mono += not is_colour60    missing = [s for s in makers if not (SHARED / "logos" / f"{s}.svg").exists()]6162    def lit(s: str) -> str:63        return json.dumps(s, ensure_ascii=False)6465    lines = [66        "//",67        "//  SP Chat iOS",68        "//  File: SPChat-iOS/Core/Models/LogoRegistry.generated.swift",69        "//  Description: Generated file — do not edit. Maker-logo registry (Scripts/sync-logos.py, from the shared logos.generated.json).",70        "//",71        "//  Author: Simon-Pierre Boucher",72        "//  Contact: contact@spboucher.ai",73        "//",74        "",75        "extension LogoRegistry {",76        f"    static let source = {lit(registry.get('source', ''))}",77        f"    static let license = {lit(registry.get('license', ''))}",78        "    /// slug → (display name, a colour variant `<slug>-color` exists)",79        "    static let makers: [String: (name: String, color: Bool)] = [",80    ]81    lines += [f"        {lit(s)}: ({lit(m['name'])}, {'true' if m.get('color') else 'false'})," for s, m in sorted(makers.items()) if s not in missing]82    lines += ["    ]", "    /// catalogue vendor prefix → logo slug", "    static let aliases: [String: String] = ["]83    lines += [f"        {lit(a)}: {lit(s)}," for a, s in sorted(aliases.items())]84    lines += ["    ]", "}", ""]85    SWIFT.write_text("\n".join(lines), encoding="utf-8")86    print(f"logos : {mono} monochromes, {colour} couleur → {OUT.relative_to(ROOT)} ; registre : {len(makers) - len(missing)} créateurs, {len(aliases)} alias")87    if missing:88        print("sans fichier SVG (monogramme neutre) : " + ", ".join(missing))89    return 0909192if __name__ == "__main__":93    sys.exit(main())94
Files main
Menu

Theme