Branchwell

Glimmer / app / native / installer / package.py

1"""Package the signed Glimmer app in the shared Protomota Finder installer layout.
2
3 python native/installer/package.py <Glimmer.app> <output.dmg>
4
5The window background is the artwork inside the signed app (Contents/Resources/installer-background.tiff),
6the same way Jotley and Inkspring do it, so the disk image shows only Glimmer and Applications.
7Needs dmgbuild, ds-store and mac-alias (native/installer/requirements.txt). Signing and notarizing
8the disk image are done by native/scripts/release-mac.sh.
9"""
10
11import argparse
12import os
13import subprocess
14import tempfile
15from pathlib import Path
16
17from dmgbuild import build_dmg
18from ds_store import DSStore
19from mac_alias import Alias
20
21ARTWORK = Path("Contents/Resources/installer-background.tiff")
22WINDOW = ((160, 160), (720, 520))
23ICON_SIZE = 104
24LOCATIONS = {"Glimmer.app": (195, 230), "Applications": (525, 230)}
25
26
27def package(app: Path, output: Path) -> None:
28 mount: Path
29
30 def remember_mount(mount_point: str, _options: dict) -> None:
31 nonlocal mount
32 mount = Path(mount_point)
33
34 def write_background_alias(event: dict) -> None:
35 if event.get("type") != "operation::finished" or event.get("operation") != "dsstore::create":
36 return
37 with DSStore.open(str(mount / ".DS_Store"), "r+") as store:
38 icons = store["."]["icvp"]
39 icons["backgroundType"] = 2
40 icons["backgroundImageAlias"] = Alias.for_file(str(mount / app.name / ARTWORK)).to_bytes()
41 store["."]["icvp"] = icons
42
43 build_dmg(str(output), app.stem, settings={
44 "format": "UDZO",
45 "filesystem": "HFS+",
46 "files": [str(app)],
47 "symlinks": {"Applications": "/Applications"},
48 "window_rect": WINDOW,
49 "default_view": "icon-view",
50 "show_toolbar": False,
51 "show_sidebar": False,
52 "show_status_bar": False,
53 "show_pathbar": False,
54 "show_tab_view": False,
55 "arrange_by": None,
56 "scroll_position": (0, 0),
57 "icon_size": ICON_SIZE,
58 "text_size": 15,
59 "label_pos": "bottom",
60 "icon_locations": LOCATIONS,
61 "create_hook": remember_mount,
62 }, callback=write_background_alias)
63
64
65def verify(image: Path) -> None:
66 """Check the layout in the finished image: two items, their positions, and the artwork reference."""
67 # Finder metadata reads reliably only from a local mount point, even when builds run on an external drive.
68 with tempfile.TemporaryDirectory(prefix="glimmer-dmg-check-", dir="/private/tmp") as temporary:
69 mount = Path(temporary) / "volume"
70 mount.mkdir()
71 subprocess.run(["hdiutil", "attach", "-readonly", "-nobrowse", "-noautoopen", "-noverify",
72 "-mountpoint", str(mount), str(image)], check=True, stdout=subprocess.DEVNULL)
73 try:
74 names = {path.name for path in mount.iterdir()}
75 assert names - {".DS_Store", ".fseventsd", ".Trashes"} == set(LOCATIONS), names
76 assert os.readlink(mount / "Applications") == "/Applications"
77 with DSStore.open(str(mount / ".DS_Store"), "r") as store:
78 window, icons = store["."]["bwsp"], store["."]["icvp"]
79 locations = {name: tuple(store[name]["Iloc"])[:2] for name in LOCATIONS}
80 assert window["WindowBounds"] == "{{160, 160}, {720, 520}}", window["WindowBounds"]
81 assert icons["iconSize"] == ICON_SIZE and icons["backgroundType"] == 2, icons
82 assert locations == LOCATIONS, locations
83 assert Alias.from_bytes(icons["backgroundImageAlias"]).target.filename == ARTWORK.name
84 assert (mount / "Glimmer.app" / ARTWORK).is_file()
85 finally:
86 subprocess.run(["hdiutil", "detach", str(mount)], check=True, stdout=subprocess.DEVNULL)
87
88
89if __name__ == "__main__":
90 parser = argparse.ArgumentParser(description=__doc__)
91 parser.add_argument("app", type=Path)
92 parser.add_argument("output", type=Path)
93 arguments = parser.parse_args()
94 if arguments.app.name != "Glimmer.app" or not (arguments.app / ARTWORK).is_file():
95 parser.error("Use a Glimmer.app that contains the installer artwork (a Mac build of the current project).")
96 if arguments.output.exists():
97 parser.error("The output path already exists.")
98 package(arguments.app, arguments.output)
99 verify(arguments.output)
100 print("Installer layout verified.")