| 1 | #!/bin/zsh |
| 2 | # Base for the Mac, released: a release build set in Timeless Sans, signed |
| 3 | # with Developer ID (Base's profile embedded), notarized, stapled, checked |
| 4 | # by Gatekeeper, then published to the Sparkle feed on |
| 5 | # oficina/native-releases (base/appcast.xml) with the archive as a release |
| 6 | # asset. Adapted from release-equipo-0.1.5.sh (2026-11-19). |
| 7 | # |
| 8 | # native/scripts/release-mac.sh VERSION BUILD # the plan |
| 9 | # native/scripts/release-mac.sh VERSION BUILD --publish # do it |
| 10 | # |
| 11 | # The plan (the default) checks what it can without signing (a clean tree |
| 12 | # at a pushed commit, the pinned Timeless Sans, the configuration, the |
| 13 | # Sparkle tools and the publisher) and prints every command it would run. |
| 14 | # --publish runs them: the signing identity, Sparkle key, notary profile |
| 15 | # and Base's Developer ID profile (Paul's portal steps) must be on this |
| 16 | # Mac. Safe to rerun: a notarized release already in the record directory |
| 17 | # is reused, and the publisher refuses a build the feed already lists. |
| 18 | set -e -u -o pipefail |
| 19 | setopt no_nomatch |
| 20 | |
| 21 | VERSION=${1:?usage: release-mac.sh VERSION BUILD [--publish]} |
| 22 | BUILD=${2:?usage: release-mac.sh VERSION BUILD [--publish]} |
| 23 | PUBLISH=0 |
| 24 | [[ ${3:-} == --publish ]] && PUBLISH=1 |
| 25 | [[ -z ${3:-} || $PUBLISH == 1 ]] || { print "usage: release-mac.sh VERSION BUILD [--publish]" >&2; exit 2 } |
| 26 | |
| 27 | IDENTITY='Developer ID Application: Team Tito Limited (9U4A84C4WK)' |
| 28 | TEAM=9U4A84C4WK |
| 29 | NOTARY_PROFILE=${NOTARY_PROFILE:-slopbox-notary} |
| 30 | SPARKLE_ACCOUNT=oficina |
| 31 | NATIVE=${0:A:h:h} |
| 32 | REPO=${NATIVE:h} |
| 33 | CONFIG=$NATIVE/mac-updates.json |
| 34 | TOOLS=$NATIVE/apple/.build/checkouts/native/tools |
| 35 | SPARKLE_BIN=$NATIVE/apple/.build/artifacts/sparkle/Sparkle/bin |
| 36 | APP="$NATIVE/build/Oficina Base.app" |
| 37 | OFICINA=$HOME/amadan.net/oficina |
| 38 | RECORD=$OFICINA/sparkle-worktrees/releases/base-$VERSION-$BUILD |
| 39 | ARTIFACTS=$OFICINA/sparkle-worktrees/native-releases |
| 40 | AGENT_HOME=$HOME/.amadan-claude-agent |
| 41 | AGENT_EMAIL='claude~paul@keymail.dev' |
| 42 | umask 077 |
| 43 | TMP=$(mktemp -d "${TMPDIR:-/tmp}/base-release.XXXXXX") |
| 44 | PAT_ID="" |
| 45 | |
| 46 | say() { print -r -- " $*" } |
| 47 | step() { print -P "\n%B%F{cyan}==> $*%f%b" } |
| 48 | die() { print -P "%F{red}error:%f $*" >&2; exit 1 } |
| 49 | run() { print -r -- " + ${(j: :)${(q-)@}}"; [[ $PUBLISH == 0 ]] || "$@" } |
| 50 | agent_amadan() { env AMADAN_HOME="$AGENT_HOME" amadan "$@" } |
| 51 | agent_git() { |
| 52 | env AMADAN_HOME="$AGENT_HOME" GIT_ASKPASS="$TMP/askpass" GIT_TERMINAL_PROMPT=0 \ |
| 53 | GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=credential.helper GIT_CONFIG_VALUE_0= \ |
| 54 | GIT_AUTHOR_NAME=Claude GIT_AUTHOR_EMAIL="$AGENT_EMAIL" \ |
| 55 | GIT_COMMITTER_NAME=Claude GIT_COMMITTER_EMAIL="$AGENT_EMAIL" "$@" |
| 56 | } |
| 57 | on_exit() { |
| 58 | local rc=$? |
| 59 | if [[ -n $PAT_ID ]]; then agent_amadan tokens revoke "$PAT_ID" >/dev/null && say "revoked PAT $PAT_ID" || print -P "%F{red}REVOKE PAT $PAT_ID BY HAND%f" >&2; fi |
| 60 | rm -rf "$TMP" |
| 61 | (( rc == 0 )) || print -P "%F{red}FAILED (exit $rc)%f" >&2 |
| 62 | } |
| 63 | trap on_exit EXIT |
| 64 | |
| 65 | [[ $PUBLISH == 1 ]] && say "publishing Base $VERSION ($BUILD)" || say "the plan for Base $VERSION ($BUILD); --publish runs it" |
| 66 | [[ $VERSION == <->.<->.<-> ]] || die "VERSION is not x.y.z" |
| 67 | [[ $BUILD == <-> && $BUILD -ge 1 ]] || die "BUILD is not a positive integer" |
| 68 | |
| 69 | step "The tree: clean, at a pushed commit" |
| 70 | COMMIT=$(git -C "$REPO" rev-parse HEAD) |
| 71 | if [[ -n $(git -C "$REPO" status --porcelain) ]]; then |
| 72 | [[ $PUBLISH == 1 ]] && die "$REPO has local changes" || say "WARNING: local changes (--publish refuses them)" |
| 73 | fi |
| 74 | if ! git -C "$REPO" branch -r --contains "$COMMIT" | grep -q .; then |
| 75 | [[ $PUBLISH == 1 ]] && die "$COMMIT is on no remote branch" || say "WARNING: $COMMIT is on no remote branch (--publish refuses it)" |
| 76 | fi |
| 77 | say "commit $COMMIT" |
| 78 | |
| 79 | step "Timeless Sans, the configuration, the Sparkle tools and the publisher" |
| 80 | "$REPO/hack/fonts.sh" check |
| 81 | python3 -c 'import json,sys; c=json.load(open(sys.argv[1])); assert c["bundle_id"]=="works.oficina.base", c["bundle_id"]; print(" config:", c["feed_url"])' "$CONFIG" |
| 82 | swift package --package-path "$NATIVE/apple" resolve >/dev/null |
| 83 | [[ -x $SPARKLE_BIN/sign_update && -f $TOOLS/release_mac.py && -f $TOOLS/publish_mac_update.py ]] || die "Sparkle tools or the kit's release tools missing" |
| 84 | say "all present" |
| 85 | |
| 86 | step "Signing: Developer ID, Sparkle key, notary profile, Base's profile" |
| 87 | profile=${$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["provisioning_profile"])' "$CONFIG")/#\~/$HOME} |
| 88 | if [[ $PUBLISH == 1 ]]; then |
| 89 | [[ $(security find-identity -v -p codesigning) == *"\"$IDENTITY\""* ]] || die "no '$IDENTITY'" |
| 90 | key=$("$SPARKLE_BIN/generate_keys" --account "$SPARKLE_ACCOUNT" -p) |
| 91 | [[ $key == $(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["public_ed_key"])' "$CONFIG") ]] || die "Sparkle key does not match mac-updates.json" |
| 92 | xcrun notarytool history --keychain-profile "$NOTARY_PROFILE" >/dev/null || die "notary profile did not authenticate" |
| 93 | [[ -f $profile ]] || die "no Developer ID profile at $profile (Paul's portal step)" |
| 94 | say "all present" |
| 95 | else |
| 96 | say "checked only with --publish: '$IDENTITY', the Sparkle key ($SPARKLE_ACCOUNT), notary profile $NOTARY_PROFILE" |
| 97 | [[ -f $profile ]] && say "Developer ID profile present" || say "WARNING: no Developer ID profile at $profile yet (Paul's portal step)" |
| 98 | fi |
| 99 | |
| 100 | if [[ -f $RECORD/release.json ]]; then |
| 101 | step "Reusing the notarized release in $RECORD" |
| 102 | else |
| 103 | step "Build Oficina Base.app $VERSION ($BUILD), release, Developer ID" |
| 104 | run "$NATIVE/scripts/build-mac.sh" --configuration release --version "$VERSION" --build "$BUILD" --sign-identity "$IDENTITY" |
| 105 | if [[ $PUBLISH == 1 ]]; then |
| 106 | [[ $(plutil -extract CFBundleShortVersionString raw -o - "$APP/Contents/Info.plist") == $VERSION ]] || die "version mismatch" |
| 107 | [[ $(plutil -extract CFBundleVersion raw -o - "$APP/Contents/Info.plist") == $BUILD ]] || die "build mismatch" |
| 108 | [[ $(plutil -extract CFBundleIdentifier raw -o - "$APP/Contents/Info.plist") == works.oficina.base ]] || die "bundle id mismatch" |
| 109 | fi |
| 110 | step "Sign inside out, notarize, staple, Gatekeeper, Sparkle archive" |
| 111 | run python3 "$TOOLS/release_mac.py" "$APP" --config "$CONFIG" --sparkle-tools "$SPARKLE_BIN" \ |
| 112 | --identity "$IDENTITY" --team "$TEAM" --notary-profile "$NOTARY_PROFILE" --output "$RECORD" |
| 113 | fi |
| 114 | |
| 115 | step "Smoke test: the stapled app passes Gatekeeper, with Oficina ID's grants" |
| 116 | SMOKE=$TMP/smoke; mkdir -p "$SMOKE" |
| 117 | zip="$RECORD/works.oficina.base-$BUILD.zip" |
| 118 | run ditto -x -k "$zip" "$SMOKE" |
| 119 | run xcrun stapler validate "$SMOKE/Oficina Base.app" |
| 120 | run spctl --assess --type execute -vv "$SMOKE/Oficina Base.app" |
| 121 | if [[ $PUBLISH == 1 ]]; then |
| 122 | [[ -f "$SMOKE/Oficina Base.app/Contents/embedded.provisionprofile" ]] || die "no embedded provisioning profile" |
| 123 | [[ -f "$SMOKE/Oficina Base.app/Contents/Resources/BaseNative_BaseKit.bundle/Contents/Resources/Fonts/TimelessSansVF.woff2" ]] || die "Timeless Sans is not in the bundle" |
| 124 | ents=$(codesign -d --entitlements - --xml "$SMOKE/Oficina Base.app" 2>/dev/null | plutil -convert json -o - -) |
| 125 | for want in 9U4A84C4WK.works.oficina.base 9U4A84C4WK.works.oficina.id 9U4A84C4WK.works.oficina webcredentials:id.oficina.works; do |
| 126 | [[ $ents == *"\"$want\""* ]] || die "entitlement $want missing" |
| 127 | done |
| 128 | say "entitlements, embedded profile and Timeless Sans present" |
| 129 | else |
| 130 | say "(checks the embedded profile, Timeless Sans in the bundle, and Base's four entitlements)" |
| 131 | fi |
| 132 | |
| 133 | step "Publish: plan, then publish as the claude agent" |
| 134 | if [[ $PUBLISH == 1 ]]; then |
| 135 | [[ $(agent_amadan auth whoami) == $AGENT_EMAIL ]] || die "agent credential is not $AGENT_EMAIL" |
| 136 | [[ -d $ARTIFACTS/.git || -f $ARTIFACTS/.git ]] || die "no native-releases checkout at $ARTIFACTS" |
| 137 | [[ -d $ARTIFACTS/base ]] || die "native-releases has no base/ yet (an operator step)" |
| 138 | LABEL=base-$VERSION-$BUILD-$(date +%Y%m%d-%H%M%S) |
| 139 | agent_amadan tokens create -label "$LABEL" | sed -n 's/^New token: //p' > "$TMP/pat" |
| 140 | [[ -s $TMP/pat ]] || die "could not read the new token" |
| 141 | PAT_ID=$(agent_amadan tokens list | awk -F'\t' -v l="$LABEL" '$2==l{print $1}') |
| 142 | [[ $PAT_ID == <-> ]] || die "no id for token $LABEL; revoke it by hand" |
| 143 | print -r -- '#!/bin/sh |
| 144 | case "$1" in Username*) echo claude ;; *) cat "'"$TMP/pat"'" ;; esac' > "$TMP/askpass"; chmod 700 "$TMP/askpass" |
| 145 | fi |
| 146 | run agent_git git -C "$ARTIFACTS" pull -q --ff-only origin main |
| 147 | run agent_git python3 "$TOOLS/publish_mac_update.py" "$RECORD" --config "$CONFIG" --repo "$ARTIFACTS" --sparkle-tools "$SPARKLE_BIN" |
| 148 | run agent_git python3 "$TOOLS/publish_mac_update.py" "$RECORD" --config "$CONFIG" --repo "$ARTIFACTS" --sparkle-tools "$SPARKLE_BIN" --publish |
| 149 | |
| 150 | step "Done" |
| 151 | say "feed: https://raw.amadan.net/oficina/native-releases/main/base/appcast.xml" |
| 152 | [[ $PUBLISH == 1 ]] || say "nothing was built, signed or published: this was the plan" |
| 153 | |