| 1 | #!/usr/bin/env python3 |
| 2 | """Writes BaseKit's Resources/Colophon.json: every open-source project and |
| 3 | font the apps ship, what each is, who made it, its licence and the licence's |
| 4 | full text (Help → Colophon). Names, URLs and licence texts are kept |
| 5 | verbatim there; what each one is and what Base uses it for are copy, so |
| 6 | the JSON holds their catalog keys (native.colophon.<id>.blurb, .usage) and |
| 7 | this writes their text into Localizable.xcstrings, where copy review's |
| 8 | words live and check-strings.py holds them. |
| 9 | |
| 10 | native/scripts/colophon.py [path/to/estilo] |
| 11 | |
| 12 | Descriptions come from estilo's catalog (colophon/projects.json, read from |
| 13 | its origin/main) where it has the project; the four it doesn't have yet |
| 14 | (KeyboardShortcuts, swift-cmark, Timeless Sans, DM Sans) are described in |
| 15 | EXTRA below until estilo adds them. Licence texts are read from the pinned |
| 16 | checkouts (apple/.build/checkouts, after `swift package resolve`) and the |
| 17 | fonts' own files. Timeless Sans's licence is a PDF (the Timeless Free Font |
| 18 | License 1.2); its entry links it. |
| 19 | """ |
| 20 | import json, os, pathlib, subprocess, sys |
| 21 | |
| 22 | native = pathlib.Path(__file__).resolve().parent.parent |
| 23 | repo = native.parent |
| 24 | checkouts = native / "apple/.build/checkouts" |
| 25 | estilo = pathlib.Path(sys.argv[1] if len(sys.argv) > 1 else os.path.expanduser("~/amadan.net/oficina/estilo")) |
| 26 | out = native / "apple/Sources/BaseKit/Resources/Colophon.json" |
| 27 | catalog_path = native / "apple/Sources/BaseKit/Resources/Localizable.xcstrings" |
| 28 | |
| 29 | catalog = json.loads(subprocess.check_output(["git", "-C", str(estilo), "show", "origin/main:colophon/projects.json"])) |
| 30 | by_id = {p["id"]: p for p in catalog["projects"]} |
| 31 | |
| 32 | def text(path): |
| 33 | return pathlib.Path(path).read_text(encoding="utf-8") |
| 34 | |
| 35 | def from_estilo(id, usage, licence_file): |
| 36 | p = by_id[id] |
| 37 | return {"id": id, "name": p["name"], "blurb": p["blurb"], "usage": usage, "author": p["author"], |
| 38 | "licence": p["licence"], "home": p["home"], "source": p["source"], "licenceText": text(licence_file)} |
| 39 | |
| 40 | EXTRA = [ |
| 41 | {"section": "typefaces", "id": "timeless-sans", "name": "Timeless Sans", |
| 42 | "blurb": "A sans serif typeface by Timeless, with two styles: Grotesk for headings and Sans for text.", |
| 43 | "usage": "Base's typeface, as on the web.", "author": "Timeless", |
| 44 | "licence": {"spdx": "LicenseRef-Timeless-Free-Font-1.2", "name": "Timeless Free Font License 1.2"}, |
| 45 | "home": "https://timeless.co/", "source": "https://media.timeless.co/type/Timeless-Type-Family-1.094.zip", |
| 46 | "licenceText": "The Timeless Free Font License 1.2 comes with the fonts as LICENSE.pdf, in the family's download above."}, |
| 47 | {"section": "typefaces", "id": "dm-sans", "name": "DM Sans", |
| 48 | "blurb": "A sans serif typeface made by Colophon Foundry for DeepMind, designed to stay clear at small sizes.", |
| 49 | "usage": "The face Base falls back to where Timeless Sans isn't bundled.", "author": "The DM Sans Project Authors", |
| 50 | "licence": {"spdx": "OFL-1.1", "name": "SIL Open Font License 1.1"}, |
| 51 | "home": "https://fonts.google.com/specimen/DM+Sans", "source": "https://github.com/googlefonts/dm-fonts", |
| 52 | "licenceText": text(repo / "internal/look/static/fonts/dm-sans/OFL.txt")}, |
| 53 | {"section": "swift", "id": "keyboardshortcuts", "name": "KeyboardShortcuts", |
| 54 | "blurb": "A Swift package for Mac keyboard shortcuts, with a recorder so you can choose your own.", |
| 55 | "usage": "The Mac app's shortcut for Ask Base.", "author": "Sindre Sorhus", |
| 56 | "licence": {"spdx": "MIT", "name": "MIT License"}, |
| 57 | "home": "https://github.com/sindresorhus/KeyboardShortcuts", "source": "https://github.com/sindresorhus/KeyboardShortcuts", |
| 58 | "licenceText": text(checkouts / "KeyboardShortcuts/license"), "platforms": ["mac"]}, |
| 59 | {"section": "swift", "id": "swift-cmark", "name": "swift-cmark", |
| 60 | "blurb": "GitHub's CommonMark parser in C, packaged for Swift.", |
| 61 | "usage": "Formats the Assistant's replies.", "author": "John MacFarlane, GitHub and others", |
| 62 | "licence": {"spdx": "BSD-2-Clause", "name": "BSD 2-Clause License"}, |
| 63 | "home": "https://github.com/swiftlang/swift-cmark", "source": "https://github.com/swiftlang/swift-cmark", |
| 64 | "licenceText": text(checkouts / "swift-cmark/COPYING")}, |
| 65 | ] |
| 66 | |
| 67 | sections = {"typefaces": [], "swift": []} |
| 68 | for e in EXTRA: |
| 69 | sections[e.pop("section")].append(e) |
| 70 | sections["swift"].append(from_estilo("rastrillo-native", "Signs Base in with Oficina ID, and its shared parts.", checkouts / "native/LICENSE")) |
| 71 | sparkle = from_estilo("sparkle", "The Mac app updates itself with it.", checkouts / "Sparkle/LICENSE") |
| 72 | sparkle["platforms"] = ["mac"] |
| 73 | sections["swift"].append(sparkle) |
| 74 | for s in sections.values(): |
| 75 | s.sort(key=lambda e: e["name"].lower()) |
| 76 | # The words go to the catalog; the JSON keeps their keys. |
| 77 | strings = json.loads(catalog_path.read_text()) |
| 78 | for entries in sections.values(): |
| 79 | for e in entries: |
| 80 | for field, what in (("blurb", "What it is"), ("usage", "What Base uses it for")): |
| 81 | key = f"native.colophon.{e['id']}.{field}" |
| 82 | strings["strings"][key] = {"comment": f"Colophon: {what} ({e['name']}).", "extractionState": "manual", |
| 83 | "localizations": {"en": {"stringUnit": {"state": "translated", "value": e.pop(field)}}}} |
| 84 | e[field + "Key"] = key |
| 85 | strings["strings"] = dict(sorted(strings["strings"].items())) |
| 86 | catalog_path.write_text(json.dumps(strings, indent=2, ensure_ascii=False) + "\n") |
| 87 | out.write_text(json.dumps({"sections": [{"id": k, "entries": v} for k, v in sections.items()]}, indent=2, ensure_ascii=False) + "\n") |
| 88 | print(f"colophon: wrote {out.relative_to(repo)}") |
| 89 | |