| 1 | #!/bin/bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | export PATH=/usr/bin:/bin:/usr/sbin:/sbin |
| 5 | [[ ( $# == 1 || ( $# == 2 && $2 == --components ) ) && $1 == /* && $EUID == 0 && $(sysctl -n hw.model) =~ ^VirtualMac[0-9]+,[0-9]+$ ]] |
| 6 | components=${2:-} |
| 7 | marker=/var/db/cage-guest-os |
| 8 | [[ ! -L $marker && -f $marker && $(stat -f %u "$marker") == 0 && $(cat "$marker") == macos ]] |
| 9 | mode=$(stat -f %OLp "$marker") |
| 10 | (( (8#$mode & 022) == 0 )) |
| 11 | kit=$(cd -P "$1" && pwd) |
| 12 | |
| 13 | [[ -z $(find "$kit" \( -type l -o ! -user root -o -perm -0020 -o -perm -0002 \) -print -quit) ]] |
| 14 | parent=$kit |
| 15 | while [[ $parent != / ]]; do |
| 16 | mode=$(stat -f %Op "$parent") |
| 17 | [[ $(stat -f %u "$parent") == 0 ]] || exit 1 |
| 18 | (( (8#$mode & 022) == 0 || (8#$mode & 01000) != 0 )) || exit 1 |
| 19 | parent=$(dirname "$parent") |
| 20 | done |
| 21 | umask 022 |
| 22 | base=/usr/local/libexec |
| 23 | mkdir -p "$base" /usr/local/bin |
| 24 | work=$(mktemp -d "$base/.cage-cli-install.XXXXXXXX") |
| 25 | trap 'rm -rf "$work"' EXIT |
| 26 | cd "$work" |
| 27 | cp "$kit"/{macos-cli.py,cli_channel.py,cli_channel.resume.md,claude_account_resume.md,claude-account.py} . |
| 28 | runtime="$base/cage-cli-runtime-2.1.311-py3.13.18-git2.56.0" |
| 29 | if [[ $components == --components ]]; then |
| 30 | [[ -x $runtime/bin/python3 && -x $runtime/bin/claude && -x $runtime/bin/git ]] || { echo '请先更新环境工具,再同步组件。' >&2; exit 1; } |
| 31 | else |
| 32 | cp "$kit"/{claude,uv.tar.gz,python.tar.gz,git.tar.gz} . |
| 33 | check() { [[ $(shasum -a 256 "$2" | cut -d' ' -f1) == "$1" ]]; } |
| 34 | check 85d8c08707fcfd1e78ffa0204eefbc6ec9e4fe22f375fb2767ef85d957ed3161 claude |
| 35 | check 81b811b57e4624df7ed3aac148e0359100d9962f3d9dce00aec533dc2e086731 uv.tar.gz |
| 36 | check 8392b6ea51d94391befb2c31ebe75463f045d0b69dfcf5e1bb3234e1f5fa3405 python.tar.gz |
| 37 | check 1110de343de11cc6f469b782b60f36217b9199b5e06875dd65baa26dd60563c7 git.tar.gz |
| 38 | chmod 755 claude |
| 39 | codesign --verify --strict --architecture arm64 -R '=anchor apple generic and identifier "com.anthropic.claude-code" and certificate leaf[subject.OU] = "Q6L2SF6YDW"' claude |
| 40 | fi |
| 41 | |
| 42 | |
| 43 | publish_dir() { |
| 44 | /usr/bin/perl -e 'rename($ARGV[0], $ARGV[1]) || -d $ARGV[1] || die "publish: $!"' "$1" "$2" |
| 45 | } |
| 46 | if [[ ! -d $runtime ]]; then |
| 47 | tar -xzf uv.tar.gz --no-same-owner |
| 48 | mkdir -p tools/bin |
| 49 | |
| 50 | mv python.tar.gz cpython-3.13.18-aarch64-apple-darwin.tar.gz |
| 51 | cat > python.json <<JSON |
| 52 | {"cpython-3.13.18-darwin-aarch64-none":{"name":"cpython","arch":{"family":"aarch64","variant":null},"os":"darwin","libc":"none","major":3,"minor":13,"patch":18,"prerelease":"","url":"file://$work/cpython-3.13.18-aarch64-apple-darwin.tar.gz","sha256":"8392b6ea51d94391befb2c31ebe75463f045d0b69dfcf5e1bb3234e1f5fa3405","variant":null,"build":"20261026"}} |
| 53 | JSON |
| 54 | UV_CACHE_DIR="$work/cache" ./uv-aarch64-apple-darwin/uv python install 3.13.18 --install-dir "$work/tools/python" --no-bin --no-config --offline --python-downloads-json-url "$work/python.json" |
| 55 | ln -s ../python/cpython-3.13.18-macos-aarch64-none/bin/python3.13 tools/bin/python3 |
| 56 | tools/bin/python3 -I -c 'import sys, ssl; assert sys.version_info[:3] == (3,13,18)' |
| 57 | mv claude tools/bin/ |
| 58 | mkdir tools/git |
| 59 | tar -xzf git.tar.gz -C tools/git --no-same-owner |
| 60 | [[ $(tools/git/bin/git --version) == 'git version 2.56.0' ]] |
| 61 | |
| 62 | |
| 63 | cat > tools/bin/git <<GIT |
| 64 | #!/bin/sh |
| 65 | export GIT_EXEC_PATH="\${GIT_EXEC_PATH:-$runtime/git/libexec/git-core}" |
| 66 | export GIT_TEMPLATE_DIR="\${GIT_TEMPLATE_DIR:-$runtime/git/share/git-core/templates}" |
| 67 | export GIT_CONFIG_SYSTEM="\${GIT_CONFIG_SYSTEM:-$runtime/git/etc/gitconfig}" |
| 68 | exec "$runtime/git/bin/git" "\$@" |
| 69 | GIT |
| 70 | chmod 755 tools/bin/git |
| 71 | chmod -R a+rX,go-w tools |
| 72 | publish_dir tools "$runtime" |
| 73 | fi |
| 74 | revision=$(shasum -a 256 macos-cli.py cli_channel.py cli_channel.resume.md claude_account_resume.md claude-account.py | shasum -a 256 | cut -c1-16) |
| 75 | target="$base/cage-cli-2.1.311-git2.56.0-$revision" |
| 76 | if [[ -d $target ]]; then |
| 77 | intact=yes |
| 78 | for name in macos-cli.py cli_channel.py cli_channel.resume.md; do |
| 79 | if ! cmp -s "$name" "$target/$name"; then intact=no; fi |
| 80 | done |
| 81 | if ! cmp -s claude_account_resume.md "$target/cli_channel.account-resume.md" || |
| 82 | ! cmp -s <(sed "1s@.*@#!$runtime/bin/python3 -I@" claude-account.py) "$target/bin/cage-claude-account"; then intact=no; fi |
| 83 | for name in python3 claude git; do |
| 84 | if [[ ! -L $target/bin/$name || $(readlink "$target/bin/$name") != "$runtime/bin/$name" ]]; then intact=no; fi |
| 85 | done |
| 86 | if [[ $intact == no ]]; then |
| 87 | |
| 88 | |
| 89 | target="$target-repair-${work##*.}" |
| 90 | fi |
| 91 | fi |
| 92 | if [[ ! -d $target ]]; then |
| 93 | mkdir -p helpers/bin |
| 94 | for name in python3 claude git; do ln -s "$runtime/bin/$name" "helpers/bin/$name"; done |
| 95 | install -m 644 macos-cli.py cli_channel.py cli_channel.resume.md helpers/ |
| 96 | install -m 644 claude_account_resume.md helpers/cli_channel.account-resume.md |
| 97 | sed "1s@.*@#!$runtime/bin/python3 -I@" claude-account.py > helpers/bin/cage-claude-account |
| 98 | chmod 755 helpers/bin/cage-claude-account |
| 99 | ln -s cage-claude-account helpers/bin/cage-open |
| 100 | publish_dir helpers "$target" |
| 101 | fi |
| 102 | current="$base/cage-cli" |
| 103 | if [[ -e $current || -L $current ]]; then |
| 104 | [[ -L $current && $(readlink "$current") == "$base"/cage-cli-2.* ]] |
| 105 | fi |
| 106 | ln -s "$target" "$work/current" |
| 107 | mv -fh "$work/current" "$current" |
| 108 | account=/usr/local/bin/cage-claude-account |
| 109 | if [[ ! -e $account && ! -L $account ]]; then ln -s "$current/bin/cage-claude-account" "$account"; fi |
| 110 | [[ -L $account && $(readlink "$account") == "$current/bin/cage-claude-account" ]] |
| 111 | for name in claude git; do |
| 112 | if [[ ! -e /usr/local/bin/$name && ! -L /usr/local/bin/$name ]]; then ln -s "$current/bin/$name" "/usr/local/bin/$name"; fi |
| 113 | done |
| 114 | "$current/bin/claude" --version |