SPB Git

Repository Public

notarize.sh 4.5 KB 73 lines shellscript

1#!/bin/bash2# ORIX Docs3# File: Scripts/notarize.sh4# Description: Notarizes dist/ORIX Docs.app with an EXISTING notarytool keychain profile, staples the ticket, builds the DMG and the ZIP,5#              signs, notarizes and staples the DMG, verifies with Gatekeeper and writes SHA-256 checksums and release metadata.6#                NOTARY_PROFILE  keychain profile created earlier with `xcrun notarytool store-credentials` (default: MacLustr-Notarize).7#              This script never creates, prints or changes credentials. "notarized" is printed only when Apple answered Accepted.8#9# Author: Simon-Pierre Boucher10# Contact: contact@spboucher.ai11set -euo pipefail12cd "$(dirname "$0")/.."13PROFILE="${NOTARY_PROFILE:-MacLustr-Notarize}"14APP="dist/ORIX Docs.app"15[[ -d "$APP" ]] || { echo "ERROR: $APP not found. Run Scripts/release.sh first." >&2; exit 1; }16VERSION="$(/usr/libexec/PlistBuddy -c 'Print CFBundleShortVersionString' "$APP/Contents/Info.plist")"17BUILD="$(/usr/libexec/PlistBuddy -c 'Print CFBundleVersion' "$APP/Contents/Info.plist")"18DMG="dist/ORIX-Docs-$VERSION.dmg"; ZIP="dist/ORIX-Docs-$VERSION.zip"19mkdir -p build/notary2021submit() {   # submit <file> <name>; succeeds only when the status is Accepted22  local file="$1" out="build/notary/$2.json"23  if ! xcrun notarytool submit "$file" --keychain-profile "$PROFILE" --wait --output-format json >"$out" 2>"$out.err"; then24    echo "Notary service refused the submission of $file:" >&2; sed -e 's/^/  /' "$out.err" | head -8 >&2; return 125  fi26  local status id27  status="$(/usr/bin/python3 -c 'import json,sys; print(json.load(open(sys.argv[1])).get("status",""))' "$out")"28  id="$(/usr/bin/python3 -c 'import json,sys; print(json.load(open(sys.argv[1])).get("id",""))' "$out")"29  echo "Submission $id: $status"30  xcrun notarytool log "$id" --keychain-profile "$PROFILE" "build/notary/$2-log.json" >/dev/null 2>&1 || true31  [[ "$status" == "Accepted" ]]32}3334echo "== 1/5 Submitting the application =="35TMPZIP="build/notary/app.zip"; rm -f "$TMPZIP"36ditto -c -k --keepParent "$APP" "$TMPZIP"37submit "$TMPZIP" app || { echo "RESULT: NOT notarized. $APP is signed but carries no notarization ticket." >&2; exit 2; }38echo "== 2/5 Stapling the application =="39xcrun stapler staple "$APP"; xcrun stapler validate "$APP"4041echo "== 3/5 ZIP of the stapled application =="42rm -f "$ZIP"; ditto -c -k --keepParent "$APP" "$ZIP"4344echo "== 4/5 Disk image =="45IDENTITY="$(codesign -dvv "$APP" 2>&1 | sed -n 's/^Authority=\(Developer ID Application: .*\)$/\1/p' | head -1)"46Scripts/package.sh "$DMG"47codesign --force --timestamp --sign "$IDENTITY" "$DMG"48submit "$DMG" dmg || { echo "RESULT: the app is notarized and stapled, the DMG is NOT." >&2; exit 3; }49xcrun stapler staple "$DMG"; xcrun stapler validate "$DMG"5051echo "== 5/5 Gatekeeper and checksums =="52spctl --assess --type execute --verbose=2 "$APP" 2>&1 | sed 's/^/  /' | tee build/notary/spctl-app.txt53spctl --assess --type open --context context:primary-signature --verbose=2 "$DMG" 2>&1 | sed 's/^/  /' | tee build/notary/spctl-dmg.txt54(cd dist && shasum -a 256 "$(basename "$DMG")" "$(basename "$ZIP")" | tee SHA256SUMS.txt)55/usr/bin/python3 - "$VERSION" "$BUILD" "$DMG" "$ZIP" "$APP" <<'PY'56import json, os, subprocess, sys, hashlib57version, build, dmg, zipf, app = sys.argv[1:6]58def sha(p): return hashlib.sha256(open(p, "rb").read()).hexdigest()59def sub(name):60    p = f"build/notary/{name}.json"61    return json.load(open(p)) if os.path.exists(p) else {}62plist = subprocess.run(["/usr/libexec/PlistBuddy", "-c", "Print LSMinimumSystemVersion", f"{app}/Contents/Info.plist"], capture_output=True, text=True).stdout.strip()63archs = subprocess.run(["lipo", "-archs", f"{app}/Contents/MacOS/ORIX Docs"], capture_output=True, text=True).stdout.split()64meta = {"name": "ORIX Docs", "version": version, "build": build, "minimumSystemVersion": plist, "architectures": archs,65        "artifacts": [{"file": os.path.basename(p), "bytes": os.path.getsize(p), "sha256": sha(p)} for p in (dmg, zipf)],66        "signing": subprocess.run(["codesign", "-dvv", app], capture_output=True, text=True).stderr.split("Authority=")[1].split("\n")[0],67        "notarization": {"app": {k: sub("app").get(k) for k in ("id", "status")}, "dmg": {k: sub("dmg").get(k) for k in ("id", "status")}},68        "gatekeeper": {"app": open("build/notary/spctl-app.txt").read().strip(), "dmg": open("build/notary/spctl-dmg.txt").read().strip()}}69json.dump(meta, open("dist/release.json", "w"), indent=2)70print(json.dumps(meta, indent=2))71PY72echo "RESULT: $APP and $DMG are notarized by Apple and stapled."73
Files main
Menu

Theme