Repository Public
1#!/usr/bin/env bash2# Notarizes dist/SPBrowser.app with an EXISTING notarytool keychain profile, staples the ticket,3# then builds, signs, notarizes and staples dist/SPBrowser.dmg.4#5# Usage: scripts/notarize.sh6# Environment:7# NOTARY_PROFILE name of the keychain profile created earlier with `xcrun notarytool store-credentials`8# (default: MacLustr-Notarize). This script never creates, prints or modifies credentials.9# The word "notarized" is printed only when Apple's service returned status Accepted.10set -euo pipefail11cd "$(dirname "$0")/.."12PROFILE="${NOTARY_PROFILE:-MacLustr-Notarize}"13APP="dist/SPBrowser.app"14DMG="dist/SPBrowser.dmg"15ZIP="build/SPBrowser-notarize.zip"16[[ -d "$APP" ]] || { echo "ERROR: $APP not found. Run scripts/build-release.sh first." >&2; exit 1; }17mkdir -p build1819submit() { # submit <file> <json-output>; returns 0 only when status is Accepted20 local file="$1" out="$2"21 if ! xcrun notarytool submit "$file" --keychain-profile "$PROFILE" --wait --output-format json >"$out" 2>"$out.err"; then22 echo "Notary service refused the submission of $file:" >&223 sed -e 's/^/ /' "$out.err" | head -8 >&224 [[ -s "$out" ]] && head -c 600 "$out" >&2 && echo >&225 return 126 fi27 local status id28 status="$(/usr/bin/python3 -c 'import json,sys; print(json.load(open(sys.argv[1])).get("status",""))' "$out")"29 id="$(/usr/bin/python3 -c 'import json,sys; print(json.load(open(sys.argv[1])).get("id",""))' "$out")"30 echo "Submission $id: $status"31 if [[ "$status" != "Accepted" ]]; then32 xcrun notarytool log "$id" --keychain-profile "$PROFILE" "build/notary-log-$id.json" >/dev/null 2>&1 || true33 echo "See build/notary-log-$id.json for the reasons." >&234 return 135 fi36}3738echo "== 1/4 Submitting the application =="39rm -f "$ZIP"40ditto -c -k --keepParent "$APP" "$ZIP"41if ! submit "$ZIP" build/notary-app.json; then42 echo "RESULT: NOT notarized. dist/SPBrowser.app is signed but carries no notarization ticket." >&243 exit 244fi45echo "== 2/4 Stapling the application =="46xcrun stapler staple "$APP"47xcrun stapler validate "$APP"4849echo "== 3/4 Building the disk image =="50IDENTITY="$(codesign -dvv "$APP" 2>&1 | sed -n 's/^Authority=\(Developer ID Application: .*\)$/\1/p' | head -1)"51rm -f "$DMG"52STAGE="$(mktemp -d)"53ditto "$APP" "$STAGE/SP Browser.app"54ln -s /Applications "$STAGE/Applications"55hdiutil create -volname "SP Browser" -srcfolder "$STAGE" -ov -format UDZO "$DMG" >/dev/null56rm -rf "$STAGE"57codesign --force --timestamp --sign "$IDENTITY" "$DMG"5859echo "== 4/4 Submitting and stapling the disk image =="60if ! submit "$DMG" build/notary-dmg.json; then61 echo "RESULT: the application is notarized and stapled, but the disk image is NOT." >&262 exit 363fi64xcrun stapler staple "$DMG"65xcrun stapler validate "$DMG"66echo "RESULT: notarized and stapled — $APP and $DMG"67