跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

build-app.sh

154 行 · 8.7 KB历史下载
1#!/usr/bin/env bash
2set -euo pipefail
3cd "$(dirname "$0")/.."
4if [[ "$(uname -s)" != Darwin ]]; then echo 'Cage.app must be built on macOS.' >&2; exit 1; fi
5case "${1:-}" in ''|--install|--macos-trial|--install-macos-trial) ;; *) echo 'Usage: scripts/build-app.sh [--install|--macos-trial|--install-macos-trial]' >&2; exit 2 ;; esac
6bundle_id=io.github.lee-xeo.claude-cage
7app_name=Cage
8if [[ "${1:-}" == *macos-trial ]]; then
9 bundle_id+=.macos-trial
10 app_name='Cage macOS'
11fi
12installed_app="$HOME/Applications/$app_name.app"
13app_destination="${CAGE_BUILD_OUTPUT:-$PWD/_output/$app_name.app}"
14if [[ "$app_destination" != /*.app ]]; then echo 'CAGE_BUILD_OUTPUT must be an absolute .app path.' >&2; exit 2; fi
15check_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}
31check_destination "$app_destination"
32if [[ "${1:-}" == --install* ]]; then check_destination "$installed_app"; fi
33version="$(tr -d '\n\r' < VERSION)"
34if [[ ! "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[A-Za-z0-9.-]+)?$ ]]; then echo 'Invalid VERSION.' >&2; exit 1; fi
35short_version="${version%%-*}"
36build_version="${CAGE_BUILD_NUMBER:-1}"
37if [[ ! "$build_version" =~ ^[0-9]+(\.[0-9]+){0,2}$ ]]; then echo 'Invalid CAGE_BUILD_NUMBER.' >&2; exit 1; fi
38identity="${CAGE_SIGNING_IDENTITY:--}"
39export CAGE_SIGNING_IDENTITY="$identity"
40# The host SDK may default to the developer's newer macOS version. Set the
41# deployment target for the Go/cgo helper as well as the Swift launcher.
42export MACOSX_DEPLOYMENT_TARGET=14.0
43export CGO_CFLAGS="${CGO_CFLAGS:-} -mmacosx-version-min=14.0"
44export CGO_LDFLAGS="${CGO_LDFLAGS:-} -mmacosx-version-min=14.0"
45bash scripts/fetch-proxy-core.sh
46if [[ "$(uname -m)" == arm64 ]]; then
47 bash scripts/guest-os/build-macos-guest-core.sh "$version"
48else
49 make cage VERSION="$version"
50fi
51_output/bin/cage verify-web-assets
52build_stage="$(mktemp -d "$PWD/_output/app-build.XXXXXX")"
53install_stage=''
54trap 'rm -rf "$build_stage"; if [[ -n "$install_stage" ]]; then rm -rf "$install_stage"; fi' EXIT
55bundle="$build_stage/Cage.app"
56mkdir -p "$bundle/Contents/MacOS" "$bundle/Contents/Resources"
57swiftc -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"
58swiftc -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"
59cp _output/bin/cage "$bundle/Contents/Resources/cage"
60if [[ "$(uname -m)" == arm64 ]]; then "$bundle/Contents/Resources/cage" verify-macos-guest; fi
61if [[ "$(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 # The kit signs all nested native code before this outer App is sealed.
66 # Do not generate or reuse an unsigned component manifest here: installation
67 # hashes the final signed kit, including each nested bundle's CodeResources.
68fi
69node scripts/bundle-cloud-kit.mjs "$bundle/Contents/Resources"
70cp _output/proxy-core/mihomo "$bundle/Contents/Resources/mihomo"
71for 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
77done
78mkdir -p "$bundle/Contents/Resources/licenses/mihomo"
79cp _output/proxy-core/LICENSE _output/proxy-core/NOTICE "$bundle/Contents/Resources/licenses/mihomo/"
80node scripts/collect-app-licenses.mjs "$bundle/Contents/Resources/licenses"
81swift desktop/Icon.swift logo.png _output/Cage.iconset
82iconutil -c icns _output/Cage.iconset -o "$bundle/Contents/Resources/Cage.icns"
83cat > "$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>
100PLIST
101sign=(codesign --force --sign "$identity")
102if [[ "$identity" != - ]]; then sign+=(--timestamp --options runtime); fi
103"${sign[@]}" --entitlements third_party/lima/vz.entitlements "$bundle/Contents/Resources/cage"
104account_signing=("${sign[@]}")
105if [[ -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")
111fi
112"${sign[@]}" "$bundle/Contents/Resources/mihomo"
113"${sign[@]}" "$bundle/Contents/Resources/CageUpdateInstaller"
114"${account_signing[@]}" "$bundle/Contents/MacOS/Cage"
115"${account_signing[@]}" "$bundle"
116codesign --verify --deep --strict --verbose=2 "$bundle"
117# Keep the working App until the replacement has passed signature verification.
118check_destination "$app_destination"
119mkdir -p "$(dirname "$app_destination")"
120if [[ -e "$app_destination" ]]; then mv "$app_destination" "$build_stage/previous.app"; fi
121if ! mv "$bundle" "$app_destination"; then
122 if [[ -d "$build_stage/previous.app" ]]; then mv "$build_stage/previous.app" "$app_destination"; fi
123 exit 1
124fi
125bundle="$app_destination"
126printf 'Built: %s\n' "$bundle"
127if [[ "${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"
150else
151 if [[ -z "${CAGE_BUILD_OUTPUT:-}" && "${1:-}" != --macos-trial ]]; then
152 node scripts/cleanup-apps.mjs --keep "$bundle" --root "$PWD/_output"
153 fi
154fi