diff --git a/.gitignore b/.gitignore index 7fceccc..72bc02f 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,5 @@ Thumbs.db /aqtinstall.log .claude/worktrees/ -.junie \ No newline at end of file +.junie +/dist/Asteria.dmg diff --git a/packaging/dmg-background.png b/packaging/dmg-background.png new file mode 100644 index 0000000..6b06e53 Binary files /dev/null and b/packaging/dmg-background.png differ diff --git a/packaging/dmg-background.svg b/packaging/dmg-background.svg new file mode 100644 index 0000000..df645e1 --- /dev/null +++ b/packaging/dmg-background.svg @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/packaging/generate_ds_store.py b/packaging/generate_ds_store.py new file mode 100644 index 0000000..88318a8 --- /dev/null +++ b/packaging/generate_ds_store.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python3 +"""Generate a macOS Finder .DS_Store for a DMG installer window without AppleEvents/TCC.""" + +import ctypes +import plistlib +import struct +import sys +from pathlib import Path + + +def make_carbon_alias(volume_root: Path, target_path: Path) -> bytes: + """Create a genuine Carbon AliasRecord ('alis') via macOS CoreServices.""" + cs = ctypes.CDLL("/System/Library/Frameworks/CoreServices.framework/CoreServices") + cs.FSPathMakeRef.argtypes = [ctypes.c_char_p, ctypes.c_void_p, ctypes.c_void_p] + cs.FSPathMakeRef.restype = ctypes.c_int32 + cs.FSNewAlias.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)] + cs.FSNewAlias.restype = ctypes.c_int32 + cs.GetHandleSize.argtypes = [ctypes.c_void_p] + cs.GetHandleSize.restype = ctypes.c_long + cs.DisposeHandle.argtypes = [ctypes.c_void_p] + cs.DisposeHandle.restype = None + + from_ref = (ctypes.c_ubyte * 80)() + err = cs.FSPathMakeRef(str(volume_root).encode("utf-8"), from_ref, None) + if err != 0: + raise RuntimeError(f"FSPathMakeRef({volume_root}) failed with OSStatus {err}") + + target_ref = (ctypes.c_ubyte * 80)() + err = cs.FSPathMakeRef(str(target_path).encode("utf-8"), target_ref, None) + if err != 0: + raise RuntimeError(f"FSPathMakeRef({target_path}) failed with OSStatus {err}") + + alias_handle = ctypes.c_void_p() + err = cs.FSNewAlias(from_ref, target_ref, ctypes.byref(alias_handle)) + if err != 0 or not alias_handle: + raise RuntimeError(f"FSNewAlias({target_path}) failed with OSStatus {err}") + + try: + size = cs.GetHandleSize(alias_handle) + ptr = ctypes.cast(alias_handle, ctypes.POINTER(ctypes.c_void_p))[0] + return ctypes.string_at(ptr, size) + finally: + cs.DisposeHandle(alias_handle) + + +def encode_record(filename: str, code: bytes, dtype: bytes, value: bytes) -> bytes: + utf16 = filename.encode("utf-16-be") + return struct.pack(">I", len(filename)) + utf16 + code + dtype + value + + +def build_ds_store(volume_root: Path, bg_path: Path, app_name: str) -> bytes: + alias_bytes = make_carbon_alias(volume_root, bg_path) + + bwsp_plist = plistlib.dumps( + { + "ContainerShowSidebar": False, + "ShowPathbar": False, + "ShowSidebar": False, + "ShowStatusBar": False, + "ShowTabView": False, + "ShowToolbar": False, + "WindowBounds": "{{200, 300}, {660, 428}}", + }, + fmt=plistlib.FMT_BINARY, + ) + + icvp_plist = plistlib.dumps( + { + "arrangeBy": "none", + "backgroundColorBlue": 1.0, + "backgroundColorGreen": 1.0, + "backgroundColorRed": 1.0, + "backgroundImageAlias": alias_bytes, + "backgroundType": 2, + "gridOffsetX": 0.0, + "gridOffsetY": 0.0, + "gridSpacing": 100.0, + "iconSize": 128.0, + "labelOnBottom": True, + "scrollPositionX": 0.0, + "scrollPositionY": 0.0, + "showIconPreview": True, + "showItemInfo": False, + "textSize": 13.0, + "viewOptionsVersion": 1, + }, + fmt=plistlib.FMT_BINARY, + ) + + iloc_app = struct.pack(">IIII", 175, 180, 0xFFFFFFFF, 0xFFFF0000) + iloc_apps = struct.pack(">IIII", 485, 180, 0xFFFFFFFF, 0xFFFF0000) + + records = [ + (".", b"bwsp", b"blob", struct.pack(">I", len(bwsp_plist)) + bwsp_plist), + (".", b"icvp", b"blob", struct.pack(">I", len(icvp_plist)) + icvp_plist), + (".", b"vSrn", b"long", struct.pack(">I", 1)), + (".", b"vstl", b"type", b"icnv"), + ("Applications", b"Iloc", b"blob", struct.pack(">I", len(iloc_apps)) + iloc_apps), + (app_name, b"Iloc", b"blob", struct.pack(">I", len(iloc_app)) + iloc_app), + ] + records.sort(key=lambda r: (r[0].lower(), r[1].lower())) + + # B-tree leaf node (4096 bytes) + leaf_body = bytearray() + leaf_body += struct.pack(">II", 0, len(records)) # next_node=0 (leaf), record_count + for fname, code, dtype, val in records: + leaf_body += encode_record(fname, code, dtype, val) + if len(leaf_body) > 4096: + raise RuntimeError(f"Leaf node overflow: {len(leaf_body)} > 4096") + leaf_block = bytes(leaf_body).ljust(4096, b"\x00") + + # DSDB master block (32 bytes) + # root_node_block=2, height=0 (single leaf), num_records, num_nodes=1, page_size=4096 + dsdb_block = struct.pack(">IIIII", 2, 0, len(records), 1, 4096).ljust(32, b"\x00") + + # Root bookkeeping block at buddy offset 0x2000 (size 2048 = 2^11) + # Block table: 0 -> root (0x200b), 1 -> DSDB (0x0045), 2 -> leaf (0x100c) + addrs = [0x2000 | 11, 0x0040 | 5, 0x1000 | 12] + root_body = bytearray() + root_body += struct.pack(">II", len(addrs), 0) + for a in addrs: + root_body += struct.pack(">I", a) + root_body += b"\x00" * ((256 - len(addrs)) * 4) + + # Table of Contents: 1 entry ("DSDB" -> block 1) + root_body += struct.pack(">IB4sI", 1, 4, b"DSDB", 1) + + # Free lists for buddy buckets 0..31 covering [0x0000..0x4000) + freelists = { + 5: [0x0020, 0x0060], + 7: [0x0080], + 8: [0x0100], + 9: [0x0200], + 10: [0x0400], + 11: [0x0800, 0x2800], + 12: [0x3000], + } + for bucket in range(32): + entries = freelists.get(bucket, []) + root_body += struct.pack(">I", len(entries)) + for off in entries: + root_body += struct.pack(">I", off) + + if len(root_body) > 2048: + raise RuntimeError(f"Root block overflow: {len(root_body)} > 2048") + root_block = bytes(root_body).ljust(2048, b"\x00") + + # Assemble buddy heap [0x0000..0x4000) prefixed by 4-byte alignment header (0x00000001) + heap = bytearray(0x4000) + heap[0x0000:0x0020] = struct.pack(">4sIII16s", b"Bud1", 0x2000, 2048, 0x2000, b"\x00" * 16) + heap[0x0040:0x0060] = dsdb_block + heap[0x1000:0x2000] = leaf_block + heap[0x2000:0x2800] = root_block + + return struct.pack(">I", 1) + bytes(heap) + + +def main() -> int: + if len(sys.argv) != 4: + print(f"Usage: {sys.argv[0]} ", file=sys.stderr) + return 2 + + mount_dir = Path(sys.argv[1]).resolve() + bg_path = Path(sys.argv[2]).resolve() + app_name = sys.argv[3] + + ds_store_bytes = build_ds_store(mount_dir, bg_path, app_name) + out_path = mount_dir / ".DS_Store" + out_path.write_bytes(ds_store_bytes) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/packaging/package-macos.sh b/packaging/package-macos.sh index 24dd104..f993515 100755 --- a/packaging/package-macos.sh +++ b/packaging/package-macos.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Package macOS build as a .dmg disk image +# Package macOS build as a styled .dmg disk image # Usage: package-macos.sh set -euo pipefail @@ -9,17 +9,45 @@ OUTPUT_DIR="${3:?}" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" -APP_PATH="$BUILD_DIR/asteria-qt.app" +SRC_APP_PATH="$BUILD_DIR/asteria-qt.app" DMG_NAME="Asteria.dmg" +VOL_NAME="Asteria" +APP_BUNDLE_NAME="Asteria.app" +BG_SOURCE="$PROJECT_DIR/packaging/dmg-background.png" +ICON_SOURCE="$PROJECT_DIR/src/assets/asteria-qt.icns" CODESIGN="${CODESIGN:-true}" CODESIGN_IDENTITY="${CODESIGN_IDENTITY:--}" -if [ ! -d "$APP_PATH" ]; then - echo "ERROR: App bundle not found at $APP_PATH" >&2 +if [ ! -d "$SRC_APP_PATH" ]; then + echo "ERROR: App bundle not found at $SRC_APP_PATH" >&2 exit 1 fi -# Deploy Qt frameworks into the .app bundle +if [ ! -f "$BG_SOURCE" ]; then + echo "ERROR: DMG background image not found at $BG_SOURCE" >&2 + exit 1 +fi + +WORK_DIR="$(mktemp -d)" +STAGING_DIR="$WORK_DIR/staging" +RW_DMG="$WORK_DIR/Asteria-rw.dmg" +APP_PATH="$STAGING_DIR/$APP_BUNDLE_NAME" +MOUNT_DIR="" +DEVICE="" + +cleanup() { + if [ -n "$DEVICE" ]; then + hdiutil detach "$DEVICE" -quiet 2>/dev/null || \ + hdiutil detach "$DEVICE" -force -quiet 2>/dev/null || true + fi + rm -rf "$WORK_DIR" +} +trap cleanup EXIT + +mkdir -p "$STAGING_DIR/.background" +ditto "$SRC_APP_PATH" "$APP_PATH" + +# Deploy Qt frameworks into the staged .app bundle "$QT_DIR/bin/macdeployqt" "$APP_PATH" -qmldir="$PROJECT_DIR/src/qml" if [ "$CODESIGN" = "true" ]; then @@ -47,9 +75,68 @@ if [ "$CODESIGN" = "true" ]; then codesign --verify --deep --strict --verbose=2 "$APP_PATH" fi -# Create .dmg directly in output directory +# Populate standard macOS drag-to-Applications DMG layout +ln -s /Applications "$STAGING_DIR/Applications" +cp "$BG_SOURCE" "$STAGING_DIR/.background/dmg-background.png" +if [ -f "$ICON_SOURCE" ]; then + cp "$ICON_SOURCE" "$STAGING_DIR/.VolumeIcon.icns" +fi + +# Create temporary read-write HFS+ DMG to configure Finder metadata +hdiutil create -srcfolder "$STAGING_DIR" -volname "$VOL_NAME" \ + -fs HFS+ -fsargs "-c c=64,a=16,e=16" -format UDRW -ov "$RW_DMG" >/dev/null + +ATTACH_PLIST="$WORK_DIR/attach.plist" +hdiutil attach -readwrite -noverify -noautoopen -plist "$RW_DMG" > "$ATTACH_PLIST" + +IFS=$'\t' read -r DEVICE MOUNT_DIR < <(python3 - "$ATTACH_PLIST" <<'PY' +import plistlib, sys +with open(sys.argv[1], "rb") as f: + data = plistlib.load(f) +dev = "" +mnt = "" +for ent in data.get("system-entities", []): + if "mount-point" in ent: + dev = ent.get("dev-entry", "") + mnt = ent["mount-point"] + break +print(f"{dev}\t{mnt}") +PY +) + +if [ -z "$DEVICE" ] || [ -z "$MOUNT_DIR" ]; then + echo "ERROR: Failed to attach temporary DMG" >&2 + exit 1 +fi + +# Hide background directory and enable custom volume icon +if command -v SetFile >/dev/null 2>&1; then + SetFile -a V "$MOUNT_DIR/.background" || true + if [ -f "$MOUNT_DIR/.VolumeIcon.icns" ]; then + SetFile -a V "$MOUNT_DIR/.VolumeIcon.icns" || true + SetFile -c icnC "$MOUNT_DIR/.VolumeIcon.icns" || true + SetFile -a C "$MOUNT_DIR" || true + fi +fi + +# Configure Finder view options and icon coordinates (.DS_Store) without AppleEvents/TCC +python3 "$SCRIPT_DIR/generate_ds_store.py" \ + "$MOUNT_DIR" \ + "$MOUNT_DIR/.background/dmg-background.png" \ + "$APP_BUNDLE_NAME" + +if command -v SetFile >/dev/null 2>&1; then + SetFile -a V "$MOUNT_DIR/.DS_Store" || true +fi + +sync +sleep 1 +hdiutil detach "$DEVICE" >/dev/null || hdiutil detach "$DEVICE" -force >/dev/null +DEVICE="" + +# Convert to compressed read-only DMG mkdir -p "$OUTPUT_DIR" -hdiutil create -volname "Asteria" -srcfolder "$APP_PATH" \ - -ov -format UDZO "$OUTPUT_DIR/$DMG_NAME" +hdiutil convert "$RW_DMG" -format UDZO -imagekey zlib-level=9 -ov -o "$OUTPUT_DIR/$DMG_NAME" >/dev/null +hdiutil verify "$OUTPUT_DIR/$DMG_NAME" >/dev/null echo "Created: $OUTPUT_DIR/$DMG_NAME"