#!/bin/zsh # A Calls Mac release, up to but not including publication: "Oficina Calls.app" # as works.oficina.calls, Developer ID signed inside out, notarised, stapled, # checked by Gatekeeper as a quarantined download, launched, and validated # against the feed by the publisher's own read-only plan. # # native/scripts/release-mac.sh # native/scripts/release-mac.sh 0.5.0 31 # # It publishes nothing: no tag, no release asset, no appcast commit. The last # thing it prints is the publish command, for whoever publishes (see "Releasing # the Mac app" in native/README.md). # # Everything Calls-specific is read from the repository, not written here: the # bundle ID, feed and Sparkle key from native/mac-updates.json, the app's file # name from what scripts/bundle-mac.py built, and rastrillo/native's release # tools from the revision Package.resolved pins (apple/.build/checkouts/native). # So it cannot drift from the app the way release-calls-0.4.16.sh (outside the # repository, net.amadan.oficina.calls and build/Calls.app hard-coded) did. # # Environment: # IDENTITY Developer ID identity (default Team Tito Limited's) # TEAM its Team ID (default 9U4A84C4WK) # NOTARY_PROFILE notarytool keychain profile (default slopbox-notary) # RECORD release directory (default # ~/amadan.net/oficina/sparkle-worktrees/releases/calls--); # it must not exist, unless it holds this build's finished # release, which is then checked and reused # ARTIFACTS the oficina/native-releases clone the plan is checked # against (default ~/amadan.net/oficina/sparkle-worktrees/native-releases); # unset or missing skips the plan with a warning # OFICINA_SKIP_PREPARE=1 reuse native/build's CallsBridge, RNNoise and # DeepFilter instead of rebuilding them (build-mac.sh) # ALLOW_DIRTY=1 build from a tree with local changes (a rehearsal only: # the release records the commit, which then does not name it) set -e -u -o pipefail setopt no_nomatch VERSION=${1:?usage: release-mac.sh } BUILD=${2:?usage: release-mac.sh } [[ $VERSION =~ '^[0-9]+(\.[0-9]+){0,2}$' ]] || { print -u2 "version must be numeric, like 0.5.0"; exit 2 } [[ $BUILD =~ '^[1-9][0-9]*$' ]] || { print -u2 "build must be a positive integer"; exit 2 } IDENTITY=${IDENTITY:-'Developer ID Application: Team Tito Limited (9U4A84C4WK)'} TEAM=${TEAM:-9U4A84C4WK} NOTARY_PROFILE=${NOTARY_PROFILE:-slopbox-notary} NATIVE=${0:A:h:h} REPO=${NATIVE:h} CONFIG=$NATIVE/mac-updates.json TOOLS=$NATIVE/apple/.build/checkouts/native/tools SPARKLE_BIN=$NATIVE/apple/.build/artifacts/sparkle/Sparkle/bin RECORD=${RECORD:-$HOME/amadan.net/oficina/sparkle-worktrees/releases/calls-$VERSION-$BUILD} ARTIFACTS=${ARTIFACTS-$HOME/amadan.net/oficina/sparkle-worktrees/native-releases} json() { python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))[sys.argv[2]])' "$@" } BUNDLE_ID=$(json "$CONFIG" bundle_id) FEED_URL=$(json "$CONFIG" feed_url) ZIP=$BUNDLE_ID-$BUILD.zip typeset -a NOTES STEP=0 SMOKE_PID="" TMP=$(mktemp -d "${TMPDIR:-/tmp}/calls-release.XXXXXX") step() { STEP=$((STEP + 1)); print -P "\n%B%F{cyan}==> [$STEP/6] $*%f%b" } say() { print -r -- " $*" } ok() { print -n -P " %F{green}ok%f "; print -r -- "$*" } warn() { print -n -P " %F{yellow}warning:%f "; print -r -- "$*"; NOTES+=("$*") } die() { print -n -P "%F{red}error:%f " >&2; print -r -- "$*" >&2; exit 1 } need() { [[ -e $1 ]] || die "missing: $1" } plist() { plutil -extract "$2" raw -o - "$1/Contents/Info.plist" } on_exit() { local rc=$? [[ -n $SMOKE_PID ]] && kill "$SMOKE_PID" 2>/dev/null || true rm -rf "$TMP" (( rc == 0 )) || print -P "\n%F{red}%BFAILED%b at step $STEP, exit $rc. Nothing was published.%f" >&2 } trap on_exit EXIT trap 'exit 130' INT TERM COMMIT=$(git -C "$REPO" rev-parse HEAD) print -P "%BOficina Calls $VERSION ($BUILD), $BUNDLE_ID%b, commit ${COMMIT[1,12]}" # ---- 1. Preflight (read-only) --------------------------------------------- step "Preflight: tree, tools, feed" if [[ -n $(git -C "$REPO" status --porcelain --untracked-files=no) ]]; then [[ ${ALLOW_DIRTY:-0} == 1 ]] || die "the tree has local changes; commit them (or ALLOW_DIRTY=1 for a rehearsal)" warn "built from a tree with local changes: $COMMIT does not name this build" fi [[ $BUNDLE_ID == works.oficina.calls ]] || die "mac-updates.json says $BUNDLE_ID, not works.oficina.calls" for tool in python3 curl shasum ditto xcrun spctl codesign; do command -v $tool >/dev/null || die "$tool not on PATH"; done # The pinned tools exist only after SwiftPM has resolved the package once. [[ -d $TOOLS ]] || swift package --package-path "$NATIVE/apple" resolve for file in "$TOOLS/release_mac.py" "$TOOLS/publish_mac_update.py" "$SPARKLE_BIN/sign_update" "$SPARKLE_BIN/generate_keys" "$SPARKLE_BIN/generate_appcast"; do need "$file"; done pinned=$(python3 -c 'import json,sys; print(next(p["state"]["revision"] for p in json.load(open(sys.argv[1]))["pins"] if p["identity"] == "native"))' "$NATIVE/apple/Package.resolved") checkout=$(git -C "$TOOLS/.." rev-parse HEAD) [[ $checkout == $pinned ]] || die "rastrillo/native checkout is ${checkout[1,7]}, Package.resolved pins ${pinned[1,7]}; run swift package resolve" ok "rastrillo/native tools at the pin, ${pinned[1,7]}" # The publisher refuses a build at or below any published one; say so now, # before twenty minutes of building and notarising. curl -fsS "$FEED_URL" -o "$TMP/feed.xml" || die "could not fetch $FEED_URL" newest=$(python3 - "$TMP/feed.xml" <<'PY' import sys, xml.etree.ElementTree as ET S = '{http://www.andymatuschak.org/xml-namespaces/sparkle}' print(max([int(v.text) for v in ET.parse(sys.argv[1]).iter(S + 'version')] or [0])) PY ) (( BUILD > newest )) || die "the feed already has build $newest; this build must be above it" ok "feed's newest build is $newest; $BUILD is above it" # ---- 2. Keychain (read-only) ------------------------------------------------ step "Keychain: Developer ID, Sparkle key, notary profile" [[ $(security find-identity -v -p codesigning) == *"\"$IDENTITY\""* ]] || die "no valid '$IDENTITY' identity" ok "$IDENTITY" key=$("$SPARKLE_BIN/generate_keys" --account "$(json "$CONFIG" keychain_account)" -p) [[ $key == $(json "$CONFIG" public_ed_key) ]] || die "the Sparkle key in the keychain is not mac-updates.json's public_ed_key" ok "Sparkle EdDSA key matches mac-updates.json" xcrun notarytool history --keychain-profile "$NOTARY_PROFILE" >/dev/null || die "notary profile $NOTARY_PROFILE did not authenticate" ok "notary profile $NOTARY_PROFILE authenticates" # ---- 3. Build --------------------------------------------------------------- REUSE=0 if [[ -e $RECORD ]]; then [[ -f $RECORD/release.json && -f $RECORD/$ZIP ]] || die "$RECORD exists without release.json and $ZIP; inspect and remove it" [[ $(json "$RECORD/release.json" version) == $VERSION && $(json "$RECORD/release.json" build) == $BUILD ]] || die "$RECORD is another release" REUSE=1 fi step "Build Oficina Calls.app $VERSION ($BUILD), release configuration" APP=$NATIVE/build/Oficina\ Calls.app if (( REUSE )); then say "skipped: reusing the notarised release in ${RECORD/#$HOME/~}" else (cd "$NATIVE" && OFICINA_BUILD_CONFIGURATION=release scripts/build-mac.sh \ --updates-config mac-updates.json --version "$VERSION" --build "$BUILD" --sign-identity "$IDENTITY") [[ $(plist "$APP" CFBundleIdentifier) == $BUNDLE_ID ]] || die "built app is not $BUNDLE_ID" [[ $(plist "$APP" CFBundleShortVersionString) == $VERSION && $(plist "$APP" CFBundleVersion) == $BUILD ]] || die "built app is not $VERSION ($BUILD)" ok "${APP/#$HOME/~} is $BUNDLE_ID $VERSION ($BUILD)" fi # ---- 4. Sign, notarise, staple, EdDSA (release_mac.py) ------------------------- step "release_mac.py: sign inside out, notarise, staple, Gatekeeper, Sparkle archive" if (( ! REUSE )); then say "notarisation usually takes a few minutes" python3 "$TOOLS/release_mac.py" "$APP" --config "$CONFIG" --sparkle-tools "$SPARKLE_BIN" \ --identity "$IDENTITY" --team "$TEAM" --notary-profile "$NOTARY_PROFILE" --output "$RECORD" fi SHA=$(json "$RECORD/release.json" sha256); LEN=$(json "$RECORD/release.json" length) [[ $(shasum -a 256 "$RECORD/$ZIP" | cut -d' ' -f1) == $SHA ]] || die "$ZIP's digest differs from release.json" ok "$ZIP: $LEN bytes, sha256 $SHA, notarisation $(json "$RECORD/release.json" notarization_id)" say "entitlements: $(python3 -c 'import json,sys; print(", ".join(sorted(json.load(open(sys.argv[1]))["entitlements"])))' "$RECORD/release.json")" # ---- 5. The download, as a person gets it ---------------------------------------- step "Smoke test: quarantined copy, names, Gatekeeper, translocated launch for 20 s" SMOKE=$TMP/smoke mkdir -p "$SMOKE" ditto -x -k "$RECORD/$ZIP" "$SMOKE" apps=("$SMOKE"/*.app(N)) (( ${#apps} == 1 )) || die "the archive holds ${#apps} apps, not one" GOT=${apps[1]} [[ ${GOT:t} == "Oficina Calls.app" ]] || die "the archive holds ${GOT:t}, not Oficina Calls.app" for pair in CFBundleIdentifier=$BUNDLE_ID CFBundleDisplayName="Oficina Calls" CFBundleName=Calls SUBundleName=Calls CFBundleIconName=AppIcon; do [[ $(plist "$GOT" ${pair%%=*}) == ${pair#*=} ]] || die "${pair%%=*} is '$(plist "$GOT" ${pair%%=*})', not '${pair#*=}'" done ok "Oficina Calls.app: $BUNDLE_ID, display name Oficina Calls, CFBundleName and SUBundleName Calls" [[ -f $GOT/Contents/Resources/Assets.car ]] || die "no Assets.car" [[ -d $GOT/Contents/Resources/RastrilloNative_RastrilloApple.bundle ]] || die "no RastrilloApple resources (the app menu's strings)" xattr -w com.apple.quarantine "0081;$(printf %x $(date +%s));Safari;" "$GOT" xcrun stapler validate "$GOT" >/dev/null || die "no stapled ticket" gk=$(spctl --assess --type execute -vv "$GOT" 2>&1) || die "Gatekeeper rejected the app: $gk" [[ $gk == *"source=Notarized Developer ID"* && $gk == *"origin=$IDENTITY"* ]] || die "unexpected Gatekeeper verdict: $gk" ok "Gatekeeper: accepted, Notarized Developer ID, $TEAM" say "launching; if macOS asks whether to open an app downloaded from the Internet, click Open" open -n "$GOT" for _ in {1..60}; do SMOKE_PID=$(pgrep -f 'AppTranslocation/.*/Oficina Calls\.app/Contents/MacOS/CallsMac' | head -1 || true) [[ -n $SMOKE_PID ]] && break sleep 1 done [[ -n $SMOKE_PID ]] || die "the translocated app did not start within 60 s" sleep 20 kill -0 "$SMOKE_PID" 2>/dev/null || die "the app quit within 20 s of launch" ok "ran translocated for 20 s (pid $SMOKE_PID)" kill "$SMOKE_PID" 2>/dev/null || true SMOKE_PID="" # ---- 6. The publisher's plan (read-only) ------------------------------------------ step "publish_mac_update.py plan (validates; changes nothing)" if [[ -n $ARTIFACTS && -d $ARTIFACTS/.git ]]; then python3 "$TOOLS/publish_mac_update.py" "$RECORD" --config "$CONFIG" --repo "$ARTIFACTS" --sparkle-tools "$SPARKLE_BIN" else warn "no native-releases clone at '${ARTIFACTS:-unset}'; the publisher's plan was not checked" fi cat <} \\ --sparkle-tools $SPARKLE_BIN --publish ------------------------------------------------------------------------ EOF