| 1 | #!/bin/zsh |
| 2 | # A Calls Mac release, up to but not including publication: "Oficina Calls.app" |
| 3 | # as works.oficina.calls, Developer ID signed inside out, notarised, stapled, |
| 4 | # checked by Gatekeeper as a quarantined download, launched, and validated |
| 5 | # against the feed by the publisher's own read-only plan. |
| 6 | # |
| 7 | # native/scripts/release-mac.sh <version> <build> |
| 8 | # native/scripts/release-mac.sh 0.5.0 31 |
| 9 | # |
| 10 | # It publishes nothing: no tag, no release asset, no appcast commit. The last |
| 11 | # thing it prints is the publish command, for whoever publishes (see "Releasing |
| 12 | # the Mac app" in native/README.md). |
| 13 | # |
| 14 | # Everything Calls-specific is read from the repository, not written here: the |
| 15 | # bundle ID, feed and Sparkle key from native/mac-updates.json, the app's file |
| 16 | # name from what scripts/bundle-mac.py built, and rastrillo/native's release |
| 17 | # tools from the revision Package.resolved pins (apple/.build/checkouts/native). |
| 18 | # So it cannot drift from the app the way release-calls-0.4.16.sh (outside the |
| 19 | # repository, net.amadan.oficina.calls and build/Calls.app hard-coded) did. |
| 20 | # |
| 21 | # Environment: |
| 22 | # IDENTITY Developer ID identity (default Team Tito Limited's) |
| 23 | # TEAM its Team ID (default 9U4A84C4WK) |
| 24 | # NOTARY_PROFILE notarytool keychain profile (default slopbox-notary) |
| 25 | # RECORD release directory (default |
| 26 | # ~/amadan.net/oficina/sparkle-worktrees/releases/calls-<version>-<build>); |
| 27 | # it must not exist, unless it holds this build's finished |
| 28 | # release, which is then checked and reused |
| 29 | # ARTIFACTS the oficina/native-releases clone the plan is checked |
| 30 | # against (default ~/amadan.net/oficina/sparkle-worktrees/native-releases); |
| 31 | # unset or missing skips the plan with a warning |
| 32 | # OFICINA_SKIP_PREPARE=1 reuse native/build's CallsBridge, RNNoise and |
| 33 | # DeepFilter instead of rebuilding them (build-mac.sh) |
| 34 | # ALLOW_DIRTY=1 build from a tree with local changes (a rehearsal only: |
| 35 | # the release records the commit, which then does not name it) |
| 36 | |
| 37 | set -e -u -o pipefail |
| 38 | setopt no_nomatch |
| 39 | |
| 40 | VERSION=${1:?usage: release-mac.sh <version> <build>} |
| 41 | BUILD=${2:?usage: release-mac.sh <version> <build>} |
| 42 | [[ $VERSION =~ '^[0-9]+(\.[0-9]+){0,2}$' ]] || { print -u2 "version must be numeric, like 0.5.0"; exit 2 } |
| 43 | [[ $BUILD =~ '^[1-9][0-9]*$' ]] || { print -u2 "build must be a positive integer"; exit 2 } |
| 44 | |
| 45 | IDENTITY=${IDENTITY:-'Developer ID Application: Team Tito Limited (9U4A84C4WK)'} |
| 46 | TEAM=${TEAM:-9U4A84C4WK} |
| 47 | NOTARY_PROFILE=${NOTARY_PROFILE:-slopbox-notary} |
| 48 | |
| 49 | NATIVE=${0:A:h:h} |
| 50 | REPO=${NATIVE:h} |
| 51 | CONFIG=$NATIVE/mac-updates.json |
| 52 | TOOLS=$NATIVE/apple/.build/checkouts/native/tools |
| 53 | SPARKLE_BIN=$NATIVE/apple/.build/artifacts/sparkle/Sparkle/bin |
| 54 | RECORD=${RECORD:-$HOME/amadan.net/oficina/sparkle-worktrees/releases/calls-$VERSION-$BUILD} |
| 55 | ARTIFACTS=${ARTIFACTS-$HOME/amadan.net/oficina/sparkle-worktrees/native-releases} |
| 56 | |
| 57 | json() { python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))[sys.argv[2]])' "$@" } |
| 58 | BUNDLE_ID=$(json "$CONFIG" bundle_id) |
| 59 | FEED_URL=$(json "$CONFIG" feed_url) |
| 60 | ZIP=$BUNDLE_ID-$BUILD.zip |
| 61 | |
| 62 | typeset -a NOTES |
| 63 | STEP=0 |
| 64 | SMOKE_PID="" |
| 65 | TMP=$(mktemp -d "${TMPDIR:-/tmp}/calls-release.XXXXXX") |
| 66 | step() { STEP=$((STEP + 1)); print -P "\n%B%F{cyan}==> [$STEP/6] $*%f%b" } |
| 67 | say() { print -r -- " $*" } |
| 68 | ok() { print -n -P " %F{green}ok%f "; print -r -- "$*" } |
| 69 | warn() { print -n -P " %F{yellow}warning:%f "; print -r -- "$*"; NOTES+=("$*") } |
| 70 | die() { print -n -P "%F{red}error:%f " >&2; print -r -- "$*" >&2; exit 1 } |
| 71 | need() { [[ -e $1 ]] || die "missing: $1" } |
| 72 | plist() { plutil -extract "$2" raw -o - "$1/Contents/Info.plist" } |
| 73 | on_exit() { |
| 74 | local rc=$? |
| 75 | [[ -n $SMOKE_PID ]] && kill "$SMOKE_PID" 2>/dev/null || true |
| 76 | rm -rf "$TMP" |
| 77 | (( rc == 0 )) || print -P "\n%F{red}%BFAILED%b at step $STEP, exit $rc. Nothing was published.%f" >&2 |
| 78 | } |
| 79 | trap on_exit EXIT |
| 80 | trap 'exit 130' INT TERM |
| 81 | |
| 82 | COMMIT=$(git -C "$REPO" rev-parse HEAD) |
| 83 | print -P "%BOficina Calls $VERSION ($BUILD), $BUNDLE_ID%b, commit ${COMMIT[1,12]}" |
| 84 | |
| 85 | # ---- 1. Preflight (read-only) --------------------------------------------- |
| 86 | step "Preflight: tree, tools, feed" |
| 87 | if [[ -n $(git -C "$REPO" status --porcelain --untracked-files=no) ]]; then |
| 88 | [[ ${ALLOW_DIRTY:-0} == 1 ]] || die "the tree has local changes; commit them (or ALLOW_DIRTY=1 for a rehearsal)" |
| 89 | warn "built from a tree with local changes: $COMMIT does not name this build" |
| 90 | fi |
| 91 | [[ $BUNDLE_ID == works.oficina.calls ]] || die "mac-updates.json says $BUNDLE_ID, not works.oficina.calls" |
| 92 | for tool in python3 curl shasum ditto xcrun spctl codesign; do command -v $tool >/dev/null || die "$tool not on PATH"; done |
| 93 | # The pinned tools exist only after SwiftPM has resolved the package once. |
| 94 | [[ -d $TOOLS ]] || swift package --package-path "$NATIVE/apple" resolve |
| 95 | 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 |
| 96 | 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") |
| 97 | checkout=$(git -C "$TOOLS/.." rev-parse HEAD) |
| 98 | [[ $checkout == $pinned ]] || die "rastrillo/native checkout is ${checkout[1,7]}, Package.resolved pins ${pinned[1,7]}; run swift package resolve" |
| 99 | ok "rastrillo/native tools at the pin, ${pinned[1,7]}" |
| 100 | # The publisher refuses a build at or below any published one; say so now, |
| 101 | # before twenty minutes of building and notarising. |
| 102 | curl -fsS "$FEED_URL" -o "$TMP/feed.xml" || die "could not fetch $FEED_URL" |
| 103 | newest=$(python3 - "$TMP/feed.xml" <<'PY' |
| 104 | import sys, xml.etree.ElementTree as ET |
| 105 | S = '{http://www.andymatuschak.org/xml-namespaces/sparkle}' |
| 106 | print(max([int(v.text) for v in ET.parse(sys.argv[1]).iter(S + 'version')] or [0])) |
| 107 | PY |
| 108 | ) |
| 109 | (( BUILD > newest )) || die "the feed already has build $newest; this build must be above it" |
| 110 | ok "feed's newest build is $newest; $BUILD is above it" |
| 111 | |
| 112 | # ---- 2. Keychain (read-only) ------------------------------------------------ |
| 113 | step "Keychain: Developer ID, Sparkle key, notary profile" |
| 114 | [[ $(security find-identity -v -p codesigning) == *"\"$IDENTITY\""* ]] || die "no valid '$IDENTITY' identity" |
| 115 | ok "$IDENTITY" |
| 116 | key=$("$SPARKLE_BIN/generate_keys" --account "$(json "$CONFIG" keychain_account)" -p) |
| 117 | [[ $key == $(json "$CONFIG" public_ed_key) ]] || die "the Sparkle key in the keychain is not mac-updates.json's public_ed_key" |
| 118 | ok "Sparkle EdDSA key matches mac-updates.json" |
| 119 | xcrun notarytool history --keychain-profile "$NOTARY_PROFILE" >/dev/null || die "notary profile $NOTARY_PROFILE did not authenticate" |
| 120 | ok "notary profile $NOTARY_PROFILE authenticates" |
| 121 | |
| 122 | # ---- 3. Build --------------------------------------------------------------- |
| 123 | REUSE=0 |
| 124 | if [[ -e $RECORD ]]; then |
| 125 | [[ -f $RECORD/release.json && -f $RECORD/$ZIP ]] || die "$RECORD exists without release.json and $ZIP; inspect and remove it" |
| 126 | [[ $(json "$RECORD/release.json" version) == $VERSION && $(json "$RECORD/release.json" build) == $BUILD ]] || die "$RECORD is another release" |
| 127 | REUSE=1 |
| 128 | fi |
| 129 | step "Build Oficina Calls.app $VERSION ($BUILD), release configuration" |
| 130 | APP=$NATIVE/build/Oficina\ Calls.app |
| 131 | if (( REUSE )); then |
| 132 | say "skipped: reusing the notarised release in ${RECORD/#$HOME/~}" |
| 133 | else |
| 134 | (cd "$NATIVE" && OFICINA_BUILD_CONFIGURATION=release scripts/build-mac.sh \ |
| 135 | --updates-config mac-updates.json --version "$VERSION" --build "$BUILD" --sign-identity "$IDENTITY") |
| 136 | [[ $(plist "$APP" CFBundleIdentifier) == $BUNDLE_ID ]] || die "built app is not $BUNDLE_ID" |
| 137 | [[ $(plist "$APP" CFBundleShortVersionString) == $VERSION && $(plist "$APP" CFBundleVersion) == $BUILD ]] || die "built app is not $VERSION ($BUILD)" |
| 138 | ok "${APP/#$HOME/~} is $BUNDLE_ID $VERSION ($BUILD)" |
| 139 | fi |
| 140 | |
| 141 | # ---- 4. Sign, notarise, staple, EdDSA (release_mac.py) ------------------------- |
| 142 | step "release_mac.py: sign inside out, notarise, staple, Gatekeeper, Sparkle archive" |
| 143 | if (( ! REUSE )); then |
| 144 | say "notarisation usually takes a few minutes" |
| 145 | python3 "$TOOLS/release_mac.py" "$APP" --config "$CONFIG" --sparkle-tools "$SPARKLE_BIN" \ |
| 146 | --identity "$IDENTITY" --team "$TEAM" --notary-profile "$NOTARY_PROFILE" --output "$RECORD" |
| 147 | fi |
| 148 | SHA=$(json "$RECORD/release.json" sha256); LEN=$(json "$RECORD/release.json" length) |
| 149 | [[ $(shasum -a 256 "$RECORD/$ZIP" | cut -d' ' -f1) == $SHA ]] || die "$ZIP's digest differs from release.json" |
| 150 | ok "$ZIP: $LEN bytes, sha256 $SHA, notarisation $(json "$RECORD/release.json" notarization_id)" |
| 151 | say "entitlements: $(python3 -c 'import json,sys; print(", ".join(sorted(json.load(open(sys.argv[1]))["entitlements"])))' "$RECORD/release.json")" |
| 152 | |
| 153 | # ---- 5. The download, as a person gets it ---------------------------------------- |
| 154 | step "Smoke test: quarantined copy, names, Gatekeeper, translocated launch for 20 s" |
| 155 | SMOKE=$TMP/smoke |
| 156 | mkdir -p "$SMOKE" |
| 157 | ditto -x -k "$RECORD/$ZIP" "$SMOKE" |
| 158 | apps=("$SMOKE"/*.app(N)) |
| 159 | (( ${#apps} == 1 )) || die "the archive holds ${#apps} apps, not one" |
| 160 | GOT=${apps[1]} |
| 161 | [[ ${GOT:t} == "Oficina Calls.app" ]] || die "the archive holds ${GOT:t}, not Oficina Calls.app" |
| 162 | for pair in CFBundleIdentifier=$BUNDLE_ID CFBundleDisplayName="Oficina Calls" CFBundleName=Calls SUBundleName=Calls CFBundleIconName=AppIcon; do |
| 163 | [[ $(plist "$GOT" ${pair%%=*}) == ${pair#*=} ]] || die "${pair%%=*} is '$(plist "$GOT" ${pair%%=*})', not '${pair#*=}'" |
| 164 | done |
| 165 | ok "Oficina Calls.app: $BUNDLE_ID, display name Oficina Calls, CFBundleName and SUBundleName Calls" |
| 166 | [[ -f $GOT/Contents/Resources/Assets.car ]] || die "no Assets.car" |
| 167 | [[ -d $GOT/Contents/Resources/RastrilloNative_RastrilloApple.bundle ]] || die "no RastrilloApple resources (the app menu's strings)" |
| 168 | xattr -w com.apple.quarantine "0081;$(printf %x $(date +%s));Safari;" "$GOT" |
| 169 | xcrun stapler validate "$GOT" >/dev/null || die "no stapled ticket" |
| 170 | gk=$(spctl --assess --type execute -vv "$GOT" 2>&1) || die "Gatekeeper rejected the app: $gk" |
| 171 | [[ $gk == *"source=Notarized Developer ID"* && $gk == *"origin=$IDENTITY"* ]] || die "unexpected Gatekeeper verdict: $gk" |
| 172 | ok "Gatekeeper: accepted, Notarized Developer ID, $TEAM" |
| 173 | say "launching; if macOS asks whether to open an app downloaded from the Internet, click Open" |
| 174 | open -n "$GOT" |
| 175 | for _ in {1..60}; do |
| 176 | SMOKE_PID=$(pgrep -f 'AppTranslocation/.*/Oficina Calls\.app/Contents/MacOS/CallsMac' | head -1 || true) |
| 177 | [[ -n $SMOKE_PID ]] && break |
| 178 | sleep 1 |
| 179 | done |
| 180 | [[ -n $SMOKE_PID ]] || die "the translocated app did not start within 60 s" |
| 181 | sleep 20 |
| 182 | kill -0 "$SMOKE_PID" 2>/dev/null || die "the app quit within 20 s of launch" |
| 183 | ok "ran translocated for 20 s (pid $SMOKE_PID)" |
| 184 | kill "$SMOKE_PID" 2>/dev/null || true |
| 185 | SMOKE_PID="" |
| 186 | |
| 187 | # ---- 6. The publisher's plan (read-only) ------------------------------------------ |
| 188 | step "publish_mac_update.py plan (validates; changes nothing)" |
| 189 | if [[ -n $ARTIFACTS && -d $ARTIFACTS/.git ]]; then |
| 190 | python3 "$TOOLS/publish_mac_update.py" "$RECORD" --config "$CONFIG" --repo "$ARTIFACTS" --sparkle-tools "$SPARKLE_BIN" |
| 191 | else |
| 192 | warn "no native-releases clone at '${ARTIFACTS:-unset}'; the publisher's plan was not checked" |
| 193 | fi |
| 194 | |
| 195 | cat <<EOF |
| 196 | |
| 197 | ----- Oficina Calls $VERSION ($BUILD): ready to publish, NOT published ----- |
| 198 | commit: $COMMIT |
| 199 | release: $RECORD |
| 200 | archive: $ZIP $LEN bytes sha256 $SHA |
| 201 | feed: $FEED_URL (newest now $newest) |
| 202 | notes: ${(j:; :)NOTES:-none} |
| 203 | |
| 204 | Publish (pushes tag calls-$BUILD, uploads the zip, commits the appcast), with |
| 205 | git and amadan as the agent (native/README.md, "Releasing the Mac app"): |
| 206 | python3 $TOOLS/publish_mac_update.py \\ |
| 207 | $RECORD \\ |
| 208 | --config $CONFIG --repo ${ARTIFACTS:-<native-releases clone>} \\ |
| 209 | --sparkle-tools $SPARKLE_BIN --publish |
| 210 | ------------------------------------------------------------------------ |
| 211 | EOF |
| 212 | |