Skip to content

A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.

C++
6.0 KB
Raw Blame
#!/bin/bash
# sign-notarize.sh — Sign + Notarize a macOS .app or .dmg for distribution.
# Handles inside-out signing when Sparkle.framework is embedded.
#
# Usage:
#   ./scripts/sign-notarize.sh <path-to-app-or-dmg>
#
# Env:
#   SIGN_IDENTITY     SHA-1 or common name of Developer ID Application cert.
#   NOTARY_PROFILE    notarytool keychain profile (default: gridex-notarize).
#   SKIP_SIGN=1       Skip resigning (notarize only).
#   SKIP_NOTARIZE=1   Skip Apple Notary submission (sign only).
#
# Flow for .app:
#   1. Sparkle inside-out: XPCServices → Autoupdate → Updater.app → Sparkle.framework
#   2. Sign main app bundle with runtime entitlements
#   3. Verify → zip → submit → wait → staple → Gatekeeper check

set -euo pipefail

if [ -z "${1:-}" ]; then
    echo "Usage: $0 <path-to-app-or-dmg>"
    exit 1
fi

TARGET="$1"

if [ ! -e "$TARGET" ]; then
    echo "✗ Not found: $TARGET"
    exit 1
fi

SIGN_IDENTITY="${SIGN_IDENTITY:-${GRIDEX_SIGN_IDENTITY:-}}"
if [ -z "$SIGN_IDENTITY" ] && [ -f "$(dirname "$0")/../.env" ]; then
    SIGN_IDENTITY=$(grep -E "^SIGN_IDENTITY=" "$(dirname "$0")/../.env" | cut -d= -f2- | tr -d '"')
fi
if [ -z "$SIGN_IDENTITY" ]; then
    echo "✗ SIGN_IDENTITY not set. Set it in .env or export it:"
    echo "    echo 'SIGN_IDENTITY=<your-sha1-or-cert-name>' >> .env"
    echo "  Find your cert with: security find-identity -v -p codesigning"
    exit 1
fi

NOTARY_PROFILE="${NOTARY_PROFILE:-gridex-notarize}"
SKIP_SIGN="${SKIP_SIGN:-0}"
SKIP_NOTARIZE="${SKIP_NOTARIZE:-0}"

echo "═══════════════════════════════════════════"
echo "  Sign + Notarize"
echo "  Target:   $TARGET"
echo "  Identity: $SIGN_IDENTITY"
echo "  Profile:  $NOTARY_PROFILE"
[ "$SKIP_NOTARIZE" = "1" ] && echo "  Mode:     SIGN ONLY (SKIP_NOTARIZE=1)"
echo "═══════════════════════════════════════════"

EXT="${TARGET##*.}"

# Helper: codesign wrapper that filters "unsealed contents" warnings but
# still catches real failures (works around pipefail interaction with grep -v).
sign() {
    local out
    out=$(codesign --force --options runtime --timestamp \
        --sign "$SIGN_IDENTITY" "$@" 2>&1) || {
            echo "✗ codesign failed for: $*"
            echo "$out"
            exit 1
        }
    echo "$out" | grep -v "unsealed contents" || true
}

sign_with_entitlements() {
    local ent="$1"
    shift
    local out
    out=$(codesign --force --options runtime --timestamp \
        --sign "$SIGN_IDENTITY" \
        --entitlements "$ent" \
        --generate-entitlement-der \
        "$@" 2>&1) || {
            echo "✗ codesign failed for: $*"
            echo "$out"
            exit 1
        }
    echo "$out" | grep -v "unsealed contents" || true
}

sign_sparkle() {
    local app="$1"
    local sparkle="$app/Contents/Frameworks/Sparkle.framework"
    [ -d "$sparkle" ] || return 0

    echo "→ Signing Sparkle inside-out..."
    local versions_dir="$sparkle/Versions/B"
    [ -d "$versions_dir" ] || versions_dir="$sparkle/Versions/A"

    # 1. XPC services (Downloader.xpc, Installer.xpc)
    if [ -d "$versions_dir/XPCServices" ]; then
        for xpc in "$versions_dir/XPCServices"/*.xpc; do
            [ -d "$xpc" ] || continue
            sign "$xpc"
        done
    fi

    # 2. Autoupdate helper binary
    [ -f "$versions_dir/Autoupdate" ] && sign "$versions_dir/Autoupdate"

    # 3. Updater.app (sign inner binary first, then bundle)
    if [ -d "$versions_dir/Updater.app" ]; then
        local updater_bin="$versions_dir/Updater.app/Contents/MacOS/Updater"
        [ -f "$updater_bin" ] && sign "$updater_bin"
        sign "$versions_dir/Updater.app"
    fi

    # 4. Sparkle.framework itself
    sign "$sparkle"
}

sign_app() {
    local app="$1"

    sign_sparkle "$app"

    # Entitlements live alongside the .app (generated by build-app.sh)
    local ent_path="$(dirname "$app")/runtime.entitlements"

    echo "→ Signing app bundle..."
    if [ -f "$ent_path" ]; then
        sign_with_entitlements "$ent_path" "$app"
    else
        sign "$app"
    fi
}

verify_app() {
    local app="$1"
    echo "→ Verifying signature..."
    codesign --verify --deep --strict --verbose=2 "$app"
    echo "→ Checking Gatekeeper assessment (pre-notarize, may reject — OK)..."
    spctl -a -t exec -vv "$app" 2>&1 || true
}

notarize() {
    local archive="$1"
    echo "→ Submitting to Apple Notary Service (5-15 min)..."
    xcrun notarytool submit "$archive" \
        --keychain-profile "$NOTARY_PROFILE" \
        --wait
}

if [ "$EXT" = "app" ]; then
    APP_PATH="$TARGET"

    if [ "$SKIP_SIGN" != "1" ]; then
        sign_app "$APP_PATH"
    fi
    verify_app "$APP_PATH"

    if [ "$SKIP_NOTARIZE" = "1" ]; then
        echo ""
        echo "✓ Signed (not notarized): $APP_PATH"
        exit 0
    fi

    ZIP_PATH="$(dirname "$APP_PATH")/$(basename "$APP_PATH" .app).zip"
    echo "→ Creating zip for notarization..."
    rm -f "$ZIP_PATH"
    ditto -c -k --keepParent "$APP_PATH" "$ZIP_PATH"

    notarize "$ZIP_PATH"
    rm -f "$ZIP_PATH"

    echo "→ Stapling notarization ticket..."
    xcrun stapler staple "$APP_PATH"

    echo "→ Final Gatekeeper check..."
    spctl -a -t exec -vv "$APP_PATH"

    echo ""
    echo "✓ Done: $APP_PATH"

elif [ "$EXT" = "dmg" ]; then
    DMG_PATH="$TARGET"

    if [ "$SKIP_SIGN" != "1" ]; then
        echo "→ Signing DMG..."
        codesign --force --sign "$SIGN_IDENTITY" --timestamp "$DMG_PATH"
    fi

    if [ "$SKIP_NOTARIZE" = "1" ]; then
        echo ""
        echo "✓ Signed (not notarized): $DMG_PATH"
        exit 0
    fi

    notarize "$DMG_PATH"

    echo "→ Stapling notarization ticket..."
    xcrun stapler staple "$DMG_PATH"

    echo "→ Final Gatekeeper check..."
    spctl -a -t open --context context:primary-signature -vv "$DMG_PATH"

    echo ""
    echo "✓ Done: $DMG_PATH"

else
    echo "✗ Target must be .app or .dmg (got .$EXT)"
    exit 1
fi