| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | cd "$(dirname "$0")/.." |
| 4 | if [[ "$(uname -s)" != Darwin ]]; then echo 'Cage.app must be built on macOS.' >&2; exit 1; fi |
| 5 | case "${1:-}" in ''|--install|--macos-trial|--install-macos-trial) ;; *) echo 'Usage: scripts/build-app.sh [--install|--macos-trial|--install-macos-trial]' >&2; exit 2 ;; esac |
| 6 | bundle_id=io.github.lee-xeo.claude-cage |
| 7 | app_name=Cage |
| 8 | if [[ "${1:-}" == *macos-trial ]]; then |
| 9 | bundle_id+=.macos-trial |
| 10 | app_name='Cage macOS' |
| 11 | fi |
| 12 | installed_app="$HOME/Applications/$app_name.app" |
| 13 | app_destination="${CAGE_BUILD_OUTPUT:-$PWD/_output/$app_name.app}" |
| 14 | if [[ "$app_destination" != /*.app ]]; then echo 'CAGE_BUILD_OUTPUT must be an absolute .app path.' >&2; exit 2; fi |
| 15 | check_destination() { |
| 16 | local target="$1" |
| 17 | [[ ! -e "$target" ]] && return 0 |
| 18 | if [[ "$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$target/Contents/Info.plist" 2>/dev/null)" != "$bundle_id" ]]; then |
| 19 | echo 'Build destination is not a Cage application.' >&2; exit 1 |
| 20 | fi |
| 21 | if ! codesign --verify --deep --strict "$target"; then |
| 22 | echo '现有 Cage 应用包含修改内容或无法验证完整性,已保留。请将个人文件移出应用包,或选择新的构建目录后重试。' >&2 |
| 23 | exit 1 |
| 24 | fi |
| 25 | local running_pids |
| 26 | running_pids="$(/usr/sbin/lsof -t "$target/Contents/MacOS/Cage" "$target/Contents/Resources/cage" 2>/dev/null || true)" |
| 27 | if [[ -n "$running_pids" ]]; then |
| 28 | echo 'Cage is running from this destination. Quit it before replacing the App.' >&2; exit 1 |
| 29 | fi |
| 30 | } |
| 31 | check_destination "$app_destination" |
| 32 | if [[ "${1:-}" == --install* ]]; then check_destination "$installed_app"; fi |
| 33 | version="$(tr -d '\n\r' < VERSION)" |
| 34 | if [[ ! "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[A-Za-z0-9.-]+)?$ ]]; then echo 'Invalid VERSION.' >&2; exit 1; fi |
| 35 | short_version="${version%%-*}" |
| 36 | build_version="${CAGE_BUILD_NUMBER:-1}" |
| 37 | if [[ ! "$build_version" =~ ^[0-9]+(\.[0-9]+){0,2}$ ]]; then echo 'Invalid CAGE_BUILD_NUMBER.' >&2; exit 1; fi |
| 38 | identity="${CAGE_SIGNING_IDENTITY:--}" |
| 39 | export CAGE_SIGNING_IDENTITY="$identity" |
| 40 | |
| 41 | |
| 42 | export MACOSX_DEPLOYMENT_TARGET=14.0 |
| 43 | export CGO_CFLAGS="${CGO_CFLAGS:-} -mmacosx-version-min=14.0" |
| 44 | export CGO_LDFLAGS="${CGO_LDFLAGS:-} -mmacosx-version-min=14.0" |
| 45 | bash scripts/fetch-proxy-core.sh |
| 46 | if [[ "$(uname -m)" == arm64 ]]; then |
| 47 | bash scripts/guest-os/build-macos-guest-core.sh "$version" |
| 48 | else |
| 49 | make cage VERSION="$version" |
| 50 | fi |
| 51 | _output/bin/cage verify-web-assets |
| 52 | build_stage="$(mktemp -d "$PWD/_output/app-build.XXXXXX")" |
| 53 | install_stage='' |
| 54 | trap 'rm -rf "$build_stage"; if [[ -n "$install_stage" ]]; then rm -rf "$install_stage"; fi' EXIT |
| 55 | bundle="$build_stage/Cage.app" |
| 56 | mkdir -p "$bundle/Contents/MacOS" "$bundle/Contents/Resources" |
| 57 | swiftc -O -target "$(uname -m)-apple-macosx14.0" -parse-as-library -framework Cocoa -framework WebKit -framework Security -framework LocalAuthentication desktop/Cage.swift desktop/MacKeyboard.swift desktop/NativeInput.swift desktop/Updates.swift desktop/AccountDevice.swift desktop/MacGuestLogin.swift -o "$bundle/Contents/MacOS/Cage" |
| 58 | swiftc -O -target "$(uname -m)-apple-macosx14.0" -parse-as-library -framework Cocoa -framework Security desktop/Updates.swift desktop/UpdateInstaller.swift -o "$bundle/Contents/Resources/CageUpdateInstaller" |
| 59 | cp _output/bin/cage "$bundle/Contents/Resources/cage" |
| 60 | if [[ "$(uname -m)" == arm64 ]]; then "$bundle/Contents/Resources/cage" verify-macos-guest; fi |
| 61 | if [[ "$(uname -m)" == arm64 ]]; then |
| 62 | bash scripts/guest-os/package-macos-guest.sh "$bundle/Contents/Resources/guest-os" |
| 63 | mkdir -p "$bundle/Contents/Resources/licenses/sshfs" |
| 64 | cp -R "$bundle/Contents/Resources/guest-os/licenses/sshfs/." "$bundle/Contents/Resources/licenses/sshfs/" |
| 65 | |
| 66 | |
| 67 | |
| 68 | fi |
| 69 | node scripts/bundle-cloud-kit.mjs "$bundle/Contents/Resources" |
| 70 | cp _output/proxy-core/mihomo "$bundle/Contents/Resources/mihomo" |
| 71 | for executable in "$bundle/Contents/MacOS/Cage" "$bundle/Contents/Resources/CageUpdateInstaller" "$bundle/Contents/Resources/cage" "$bundle/Contents/Resources/mihomo"; do |
| 72 | minimum="$(otool -l "$executable" | awk '$1 == "minos" {print $2}')" |
| 73 | if [[ -z "$minimum" ]] || ! awk -v version="$minimum" 'BEGIN {split(version,v,"."); exit !(v[1] < 14 || (v[1] == 14 && v[2] == 0 && v[3]+0 == 0))}'; then |
| 74 | printf 'Unsupported deployment target %s in %s (expected macOS 14.0 or earlier).\n' "$minimum" "$executable" >&2 |
| 75 | exit 1 |
| 76 | fi |
| 77 | done |
| 78 | mkdir -p "$bundle/Contents/Resources/licenses/mihomo" |
| 79 | cp _output/proxy-core/LICENSE _output/proxy-core/NOTICE "$bundle/Contents/Resources/licenses/mihomo/" |
| 80 | node scripts/collect-app-licenses.mjs "$bundle/Contents/Resources/licenses" |
| 81 | swift desktop/Icon.swift logo.png _output/Cage.iconset |
| 82 | iconutil -c icns _output/Cage.iconset -o "$bundle/Contents/Resources/Cage.icns" |
| 83 | cat > "$bundle/Contents/Info.plist" <<PLIST |
| 84 | <?xml version="1.0" encoding="UTF-8"?> |
| 85 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 86 | <plist version="1.0"><dict> |
| 87 | <key>CFBundleName</key><string>$app_name</string> |
| 88 | <key>CFBundleDisplayName</key><string>$app_name</string> |
| 89 | <key>CFBundleIdentifier</key><string>$bundle_id</string> |
| 90 | <key>CFBundleVersion</key><string>$build_version</string> |
| 91 | <key>CFBundleShortVersionString</key><string>$short_version</string> |
| 92 | <key>CageVersion</key><string>$version</string> |
| 93 | <key>CFBundleExecutable</key><string>Cage</string> |
| 94 | <key>CFBundleIconFile</key><string>Cage</string> |
| 95 | <key>CFBundlePackageType</key><string>APPL</string> |
| 96 | <key>LSMinimumSystemVersion</key><string>14.0</string> |
| 97 | <key>NSHighResolutionCapable</key><true/> |
| 98 | <key>NSAppTransportSecurity</key><dict><key>NSAllowsLocalNetworking</key><true/></dict> |
| 99 | </dict></plist> |
| 100 | PLIST |
| 101 | sign=(codesign --force --sign "$identity") |
| 102 | if [[ "$identity" != - ]]; then sign+=(--timestamp --options runtime); fi |
| 103 | "${sign[@]}" --entitlements third_party/lima/vz.entitlements "$bundle/Contents/Resources/cage" |
| 104 | account_signing=("${sign[@]}") |
| 105 | if [[ -n "${CAGE_PROVISIONING_PROFILE:-}" ]]; then |
| 106 | [[ "$identity" != - ]] || { echo 'Biometric provisioning requires a Developer ID signature.' >&2; exit 1; } |
| 107 | signing_team="$(codesign -dv --verbose=4 "$bundle/Contents/Resources/cage" 2>&1 | sed -n 's/^TeamIdentifier=//p')" |
| 108 | bash scripts/prepare-account-signing.sh "$CAGE_PROVISIONING_PROFILE" "$signing_team" "$build_stage/account.entitlements" |
| 109 | cp "$CAGE_PROVISIONING_PROFILE" "$bundle/Contents/embedded.provisionprofile" |
| 110 | account_signing+=(--entitlements "$build_stage/account.entitlements") |
| 111 | fi |
| 112 | "${sign[@]}" "$bundle/Contents/Resources/mihomo" |
| 113 | "${sign[@]}" "$bundle/Contents/Resources/CageUpdateInstaller" |
| 114 | "${account_signing[@]}" "$bundle/Contents/MacOS/Cage" |
| 115 | "${account_signing[@]}" "$bundle" |
| 116 | codesign --verify --deep --strict --verbose=2 "$bundle" |
| 117 | # Keep the working App until the replacement has passed signature verification. |
| 118 | check_destination "$app_destination" |
| 119 | mkdir -p "$(dirname "$app_destination")" |
| 120 | if [[ -e "$app_destination" ]]; then mv "$app_destination" "$build_stage/previous.app"; fi |
| 121 | if ! mv "$bundle" "$app_destination"; then |
| 122 | if [[ -d "$build_stage/previous.app" ]]; then mv "$build_stage/previous.app" "$app_destination"; fi |
| 123 | exit 1 |
| 124 | fi |
| 125 | bundle="$app_destination" |
| 126 | printf 'Built: %s\n' "$bundle" |
| 127 | if [[ "${1:-}" == --install* ]]; then |
| 128 | mkdir -p "$HOME/Applications" |
| 129 | install_stage="$(mktemp -d "$HOME/Applications/.Cage-install.XXXXXX")" |
| 130 | ditto "$bundle" "$install_stage/Cage.app" |
| 131 | codesign --verify --deep --strict --verbose=2 "$install_stage/Cage.app" |
| 132 | check_destination "$installed_app" |
| 133 | if [[ -e "$installed_app" ]]; then mv "$installed_app" "$install_stage/previous.app"; fi |
| 134 | if ! mv "$install_stage/Cage.app" "$installed_app"; then |
| 135 | if [[ -d "$install_stage/previous.app" ]]; then mv "$install_stage/previous.app" "$installed_app"; fi |
| 136 | exit 1 |
| 137 | fi |
| 138 | printf 'Installed: %s\n' "$installed_app" |
| 139 | # An installed build has one launchable copy. Remove the staging build too, |
| 140 | # along with historical validation copies, after the installed App verifies. |
| 141 | codesign --verify --deep --strict --verbose=2 "$installed_app" |
| 142 | if [[ "${1:-}" == --install-macos-trial ]]; then |
| 143 | # Normal release cleanup scans other Cage installations. The trial has a separate identity. |
| 144 | if [[ ! "$bundle" -ef "$installed_app" ]]; then mv "$bundle" "$build_stage/installed-trial.app"; fi |
| 145 | exit 0 |
| 146 | fi |
| 147 | node scripts/cleanup-apps.mjs --keep "$installed_app" \ |
| 148 | --root "$bundle" --root "$PWD/_output" --root "$HOME/Applications" \ |
| 149 | --root /Applications --root "$HOME/.claude-cage-validation" |
| 150 | else |
| 151 | if [[ -z "${CAGE_BUILD_OUTPUT:-}" && "${1:-}" != --macos-trial ]]; then |
| 152 | node scripts/cleanup-apps.mjs --keep "$bundle" --root "$PWD/_output" |
| 153 | fi |
| 154 | fi |