#!/usr/bin/env bash # Publish a built Cotton.app (the menu bar app) through its update feed, so every installed copy updates itself # (Sparkle; Sources/Updates.swift). The coordinator runs this: it reads the Sparkle private key and puts objects # in the production bucket. # # bash apps/cotton/scripts/publish-cotton-mac.sh --build --dry-run # build the release bundle, then check it # bash apps/cotton/scripts/publish-cotton-mac.sh # publish the bundle built before # # --build runs pnpm -s mac:cotton:build first, with the release keys read below in its environment (so the feed # and the public key are stamped in and the build stops if it cannot sign with the Developer ID). # # Reads COTTON_SPARKLE_PRIVATE_KEY_FILE (and, when set, COTTON_NOTARY_KEY_PATH, COTTON_NOTARY_KEY_ID and # COTTON_NOTARY_ISSUER to notarise first), from the environment or ~/.cotton-credentials/cotton/mac-release.env. # # What it does, in order, stopping at the first thing wrong: # 1. checks the bundle: Developer ID signed, SUFeedURL is the feed below, SUPublicEDKey is set, and its build # number is above the one the live feed offers (Sparkle compares CFBundleVersion); # 2. notarises and staples it when the notary key is given (optional: Sparkle installs a Developer ID build); # 3. zips it (Cotton--.zip) and signs the zip with Sparkle's sign_update (EdDSA); # 4. writes appcast.xml naming that one build; # 5. puts the zip, then the feed, in R2 (cotton-mac-releases, under cotton/), which cotton-site serves at # https://cottondb.com/mac/cotton/ (apps/cotton-site/src/shared/macCotton.ts); # 6. reads both back over https and checks the length and the feed's build number. # --dry-run stops before 5 and leaves the zip and feed in apps/cotton/Mac/build/publish/. set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)" MAC_DIR="$ROOT/apps/cotton/Mac" BUNDLE="$MAC_DIR/build/Cotton.app" OUT="$MAC_DIR/build/publish" FEED_URL="https://cottondb.com/mac/cotton/appcast.xml" BASE_URL="https://cottondb.com/mac/cotton" BUCKET="cotton-mac-releases" DRY=0 BUILD_FIRST=0 for arg in "$@"; do case "$arg" in --) ;; --dry-run) DRY=1 ;; --build) BUILD_FIRST=1 ;; *) echo "[publish-cotton-mac] unknown argument $arg (takes --build and --dry-run)" >&2; exit 2 ;; esac done say() { echo "[publish-cotton-mac] $*" >&2; } die() { say "STOP: $*"; exit 1; } CREDS="$HOME/.cotton-credentials/cotton/mac-release.env" if [ -z "${COTTON_SPARKLE_PRIVATE_KEY_FILE:-}" ] && [ -f "$CREDS" ]; then set -a # shellcheck disable=SC1090 . "$CREDS" set +a fi [ -n "${COTTON_SPARKLE_PRIVATE_KEY_FILE:-}" ] && [ -f "$COTTON_SPARKLE_PRIVATE_KEY_FILE" ] \ || die "no Sparkle private key file (COTTON_SPARKLE_PRIVATE_KEY_FILE)" # ── 0. Build (with --build) ────────────────────────────────────────────────── if [ "$BUILD_FIRST" = 1 ]; then [ -n "${COTTON_SPARKLE_PUBLIC_KEY:-}" ] || die "no COTTON_SPARKLE_PUBLIC_KEY to build with" say "building the release bundle" (cd "$ROOT" && COTTON_SPARKLE_PUBLIC_KEY="$COTTON_SPARKLE_PUBLIC_KEY" bash apps/cotton/scripts/build-cotton-mac.sh) >&2 \ || die "the build failed (its last lines above say why)" fi # ── 1. The bundle ──────────────────────────────────────────────────────────── [ -d "$BUNDLE" ] || die "no bundle at $BUNDLE: run COTTON_SPARKLE_PUBLIC_KEY=… pnpm -s mac:cotton:build first" PLIST="$BUNDLE/Contents/Info.plist" read_plist() { /usr/libexec/PlistBuddy -c "Print :$1" "$PLIST" 2>/dev/null || true; } VERSION="$(read_plist CFBundleShortVersionString)" BUILD="$(read_plist CFBundleVersion)" [ "$(read_plist SUFeedURL)" = "$FEED_URL" ] || die "the bundle's SUFeedURL is not $FEED_URL (built without COTTON_SPARKLE_PUBLIC_KEY?)" [ -n "$(read_plist SUPublicEDKey)" ] || die "the bundle has no SUPublicEDKey" [ "$(read_plist CFBundleIdentifier)" = "com.decemberfm.cotton.mac" ] || die "the bundle is not com.decemberfm.cotton.mac" codesign --verify --deep --strict "$BUNDLE" || die "the bundle's signature does not verify" # `-dvv`: plain `-dv` prints no Authority lines, so it read every bundle as unsigned (23 Nov 2026). Read into a # variable first: `codesign | grep -q` under pipefail fails whenever grep stops reading early. SIGNATURE="$(codesign -dvv "$BUNDLE" 2>&1 || true)" case "$SIGNATURE" in *"Authority=Developer ID Application"*) ;; *) die "the bundle is not signed with a Developer ID";; esac case "$BUILD" in ''|*[!0-9]*) die "CFBundleVersion '$BUILD' is not a whole number";; esac LIVE_BUILD="$(curl -fsS "$FEED_URL" 2>/dev/null | sed -n 's:.*\([0-9]*\).*:\1:p' | head -1 || true)" if [ -n "$LIVE_BUILD" ] && [ "$BUILD" -le "$LIVE_BUILD" ]; then die "build $BUILD is not above the live feed's $LIVE_BUILD: raise CURRENT_PROJECT_VERSION in apps/cotton/Mac/project.yml" fi say "Cotton $VERSION ($BUILD); the live feed offers ${LIVE_BUILD:-nothing}" # ── 2. Notarise (optional) ─────────────────────────────────────────────────── mkdir -p "$OUT" ZIP_NAME="Cotton-$VERSION-$BUILD.zip" ZIP="$OUT/$ZIP_NAME" rm -f "$ZIP" if [ -n "${COTTON_NOTARY_KEY_PATH:-}" ] && [ -n "${COTTON_NOTARY_KEY_ID:-}" ] && [ -n "${COTTON_NOTARY_ISSUER:-}" ]; then say "notarising" ditto -c -k --sequesterRsrc --keepParent "$BUNDLE" "$ZIP" xcrun notarytool submit "$ZIP" --key "$COTTON_NOTARY_KEY_PATH" --key-id "$COTTON_NOTARY_KEY_ID" \ --issuer "$COTTON_NOTARY_ISSUER" --wait || die "notarisation failed" xcrun stapler staple "$BUNDLE" || die "stapling failed" rm -f "$ZIP" else say "no notary key: publishing the Developer ID build as it is (Sparkle installs it; the installed build is the same kind)" fi # ── 3. Zip and sign ────────────────────────────────────────────────────────── ditto -c -k --sequesterRsrc --keepParent "$BUNDLE" "$ZIP" # shellcheck source=../../../scripts/lib/derived-data.sh . "$ROOT/scripts/lib/derived-data.sh" SIGN_UPDATE="$(cotton_derived_data)/mac/SourcePackages/artifacts/sparkle/Sparkle/bin/sign_update" [ -x "$SIGN_UPDATE" ] || die "no Sparkle sign_update at $SIGN_UPDATE (the build fetches it with the Sparkle package)" SIGNED="$("$SIGN_UPDATE" -f "$COTTON_SPARKLE_PRIVATE_KEY_FILE" "$ZIP")" || die "sign_update failed" echo "$SIGNED" | grep -q 'sparkle:edSignature="' || die "sign_update gave no signature" LENGTH="$(stat -f %z "$ZIP")" # ── 4. The feed ────────────────────────────────────────────────────────────── MIN_SYSTEM="$(read_plist LSMinimumSystemVersion)" PUB_DATE="$(LC_ALL=C date -u '+%a, %d %b %Y %H:%M:%S +0000')" cat > "$OUT/appcast.xml" < Cotton for the Mac $FEED_URL Cotton $VERSION $PUB_DATE $BUILD $VERSION ${MIN_SYSTEM:-26.0} XML xmllint --noout "$OUT/appcast.xml" 2>/dev/null || die "the feed is not well-formed XML" say "wrote $ZIP ($LENGTH bytes) and $OUT/appcast.xml" if [ "$DRY" = 1 ]; then say "dry run: nothing published" exit 0 fi # ── 5. Publish: the zip first, so the feed never names a missing build ─────── WRANGLER=(pnpm --dir "$ROOT/apps/cotton-site" exec wrangler) "${WRANGLER[@]}" r2 object put "$BUCKET/cotton/$ZIP_NAME" --file "$ZIP" --content-type application/zip --remote \ || die "the zip did not go up" "${WRANGLER[@]}" r2 object put "$BUCKET/cotton/appcast.xml" --file "$OUT/appcast.xml" \ --content-type "application/xml; charset=utf-8" --remote || die "the feed did not go up" # ── 6. Read back ───────────────────────────────────────────────────────────── SERVED_LENGTH="$(curl -fsSI "$BASE_URL/$ZIP_NAME" | tr -d '\r' | awk 'tolower($1)=="content-length:"{print $2}')" [ "$SERVED_LENGTH" = "$LENGTH" ] || die "cottondb.com serves the zip at ${SERVED_LENGTH:-no} bytes, not $LENGTH (is cotton-site deployed with /mac/cotton/?)" SERVED_BUILD="$(curl -fsS -H 'Cache-Control: no-cache' "$FEED_URL?$(date +%s)" | sed -n 's:.*\([0-9]*\).*:\1:p' | head -1)" [ "$SERVED_BUILD" = "$BUILD" ] || say "the feed still offers ${SERVED_BUILD:-nothing}: the edge caches it up to five minutes" say "published Cotton $VERSION ($BUILD): installed copies take it within the hour, or at once by Check for updates"