#!/bin/bash set -euo pipefail cd "$(dirname "$0")/.." source Scripts/product.env source Scripts/packaging-paths.sh source Scripts/detach-image.sh if [[ $# -gt 2 ]]; then echo "Usage: $0 [app] [output.dmg]" >&2; exit 2; fi app=$(packaging_app_path "${1:-build/$PRODUCT_NAME.app}") output=$(packaging_output_path "${2:-build/$PRODUCT_NAME.dmg}") Scripts/bundle-preflight.sh "$app" mkdir -p "$(dirname "$output")" work=$(mktemp -d "${TMPDIR:-/tmp}/$PRODUCT_CASK-dmg.XXXXXX") mounted=false detach_failed=false mountpoint="$work/mount" detach_mount() { if detach_image "$mountpoint"; then mounted=false else detach_failed=true return 1 fi } cleanup() { local status=$? trap - EXIT INT TERM if [[ $mounted == true ]]; then if [[ $detach_failed == true ]] || ! detach_mount; then echo "Could not detach $mountpoint; leaving the staging directory intact." >&2 exit 1 fi fi rm -rf "$work" exit "$status" } trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM swiftc -swift-version 6 -warnings-as-errors -parse-as-library \ Packaging/DMGLayout/Sources/DMGLayout/*.swift -o "$work/dmg-layout" mkdir -p "$work/stage/.background" "$mountpoint" ditto "$app" "$work/stage/$PRODUCT_NAME.app" Scripts/bundle-preflight.sh "$work/stage/$PRODUCT_NAME.app" xattr -cr "$work/stage/$PRODUCT_NAME.app" ln -s /Applications "$work/stage/Applications" tiffutil -cathidpicheck Packaging/background.png Packaging/background@2x.png \ -out "$work/stage/.background/background.tiff" hdiutil makehybrid -hfs -hfs-volume-name "$PRODUCT_NAME" -hfs-openfolder "$work/stage" \ -o "$work/seed.dmg" "$work/stage" hdiutil convert "$work/seed.dmg" -format UDRW -o "$work/writable.dmg" # The hybrid filesystem is packed; leave room for HFS+ catalog updates and Finder metadata. # hdiutil's b suffix means 512-byte sectors; express the byte count in KiB instead. size=$(stat -f %z "$work/seed.dmg") hdiutil resize -size "$(((size + 1023) / 1024 + 16 * 1024))k" "$work/writable.dmg" hdiutil attach "$work/writable.dmg" -mountpoint "$mountpoint" -nobrowse -noautoopen mounted=true # DiscRecording adds Finder attributes while constructing the hybrid filesystem. xattr -cr "$mountpoint/$PRODUCT_NAME.app" "$work/dmg-layout" write "$mountpoint" "$work/dmg-layout" verify "$mountpoint" "$work/dmg-layout" verify-open-folder "$mountpoint" sync detach_mount hdiutil convert "$work/writable.dmg" -format UDZO -imagekey zlib-level=9 -o "$work/final.dmg" hdiutil verify "$work/final.dmg" mountpoint="$work/verify-mount" mkdir "$mountpoint" hdiutil attach "$work/final.dmg" -mountpoint "$mountpoint" -readonly -nobrowse -noautoopen mounted=true "$work/dmg-layout" verify "$mountpoint" "$work/dmg-layout" verify-open-folder "$mountpoint" mounted_app="$mountpoint/$PRODUCT_NAME.app" Scripts/bundle-preflight.sh "$mounted_app" codesign --verify --deep --strict "$mounted_app" if ! codesign -dv "$mounted_app" > "$work/signature.log" 2>&1; then /bin/cat "$work/signature.log" >&2 exit 1 fi if [[ ${PARCEL_SIGN_IDENTITY:-} == 'Developer ID Application:'* ]] || grep -q '^Authority=Developer ID Application:' "$work/signature.log"; then spctl --assess --type execute "$mounted_app" fi detach_mount if [[ -n ${PARCEL_SIGN_IDENTITY:-} ]]; then args=(--force --sign "$PARCEL_SIGN_IDENTITY") if [[ $PARCEL_SIGN_IDENTITY != '-' ]]; then args+=(--timestamp); fi codesign "${args[@]}" "$work/final.dmg" codesign --verify --strict "$work/final.dmg" fi mv -n "$work/final.dmg" "$output" [[ ! -e $work/final.dmg ]] || { echo "Refusing to replace: $output" >&2; exit 1; } echo "$output"