| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | set -uo pipefail |
| 14 | |
| 15 | SELF_DIR="$(CDPATH= cd -- "$(dirname "${BASH_SOURCE[0]:-$0}")/.." >/dev/null && pwd)" |
| 16 | INSTALLER="$SELF_DIR/www/public/install.sh" |
| 17 | |
| 18 | FAIL=0 |
| 19 | PASSED=0 |
| 20 | pass() { PASSED=$((PASSED + 1)); printf ' ok %s\n' "$1"; } |
| 21 | fail() { FAIL=$((FAIL + 1)); printf ' FAIL %s\n' "$1"; } |
| 22 | check() { |
| 23 | if [ "$2" = "$3" ]; then pass "$1" |
| 24 | else fail "$1"; printf ' expected: %s\n actual: %s\n' "$3" "$2"; fi |
| 25 | } |
| 26 | |
| 27 | |
| 28 | case "$(uname -s)-$(uname -m)" in |
| 29 | Darwin-arm64) PLATFORM=darwin-arm64 ;; |
| 30 | Darwin-x86_64) PLATFORM=darwin-amd64 ;; |
| 31 | Linux-aarch64 | Linux-arm64) PLATFORM=linux-arm64 ;; |
| 32 | Linux-x86_64) PLATFORM=linux-amd64 ;; |
| 33 | *) echo " skip: no release build for $(uname -s) $(uname -m)"; exit 0 ;; |
| 34 | esac |
| 35 | case "$PLATFORM" in *-arm64) OTHER="${PLATFORM%-*}-amd64" ;; *) OTHER="${PLATFORM%-*}-arm64" ;; esac |
| 36 | |
| 37 | sha256() { |
| 38 | if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1 |
| 39 | else shasum -a 256 "$1" | cut -d' ' -f1 |
| 40 | fi |
| 41 | } |
| 42 | |
| 43 | SB="$(mktemp -d)" |
| 44 | SB="$(CDPATH= cd -P -- "$SB" >/dev/null && pwd)" |
| 45 | trap 'rm -rf "$SB"' EXIT |
| 46 | |
| 47 | WF="$SB/wfix"; WBIN="$SB/wbin"; WH="$SB/whome" |
| 48 | mkdir -p "$WBIN" "$WH" "$WF" |
| 49 | cat > "$WBIN/curl" <<'STUB' |
| 50 | |
| 51 | out=""; url="" |
| 52 | while [ $# -gt 0 ]; do |
| 53 | case "$1" in -o) out="$2"; shift ;; https://*) url="$1" ;; esac |
| 54 | shift |
| 55 | done |
| 56 | echo "$url" >> "$WF/log" |
| 57 | case "$url" in |
| 58 | */releases/latest/download/*) f="$WF/$(cat "$WF/latest")/${url##*/}" ;; |
| 59 | */releases/download/*) t="${url%/*}"; f="$WF/${t##*/}/${url##*/}" ;; |
| 60 | *) exit 22 ;; |
| 61 | esac |
| 62 | [ -f "$f" ] || exit 22 |
| 63 | cp "$f" "$out" |
| 64 | STUB |
| 65 | cat > "$WBIN/git" <<'STUB' |
| 66 | |
| 67 | case "$1" in |
| 68 | clone) echo "git $*" >> "$WF/log"; mkdir -p "$3/.git" && cp -R "$WF/src/." "$3/" ;; |
| 69 | -C) |
| 70 | [ -d "$2/.git" ] || exit 128 |
| 71 | case "$3" in |
| 72 | rev-parse) cat "$WF/head" ;; |
| 73 | *) echo "git $*" >> "$WF/log" ;; |
| 74 | esac ;; |
| 75 | esac |
| 76 | STUB |
| 77 | chmod +x "$WBIN/curl" "$WBIN/git" |
| 78 | |
| 79 | |
| 80 | |
| 81 | stub_binary() { |
| 82 | cat > "$1" <<EOF |
| 83 | #!/bin/sh |
| 84 | echo "$2 \$*" >> "\$WF/ran" |
| 85 | [ "\$1" = install ] || exit 0 |
| 86 | mkdir -p "\$AGENT_VM_BIN_DIR" |
| 87 | ln -sfn "\$0" "\$AGENT_VM_BIN_DIR/agent-vm" |
| 88 | echo "agent-vm $2 installed. Next: cd your-project && agent-vm claude" |
| 89 | EOF |
| 90 | chmod +x "$1" |
| 91 | } |
| 92 | |
| 93 | |
| 94 | |
| 95 | make_release() { |
| 96 | local d="$WF/v$1" p |
| 97 | mkdir -p "$d" |
| 98 | : > "$d/SHA256SUMS" |
| 99 | for p in "$OTHER" "$PLATFORM"; do |
| 100 | local s="$WF/stage-$1-$p/agent-vm-$1" |
| 101 | mkdir -p "$s" |
| 102 | cp "$SELF_DIR/agent-vm.sh" "$s/" |
| 103 | stub_binary "$s/agent-vm" "$1-$p" |
| 104 | tar -czf "$d/agent-vm-$1-$p.tar.gz" -C "$WF/stage-$1-$p" "agent-vm-$1" |
| 105 | local sum; sum="$(sha256 "$d/agent-vm-$1-$p.tar.gz")" |
| 106 | [ -z "${2:-}" ] || [ "$p" != "$PLATFORM" ] || sum="0000$sum" |
| 107 | printf '%s agent-vm-%s-%s.tar.gz\n' "$sum" "$1" "$p" >> "$d/SHA256SUMS" |
| 108 | done |
| 109 | echo "v$1" > "$WF/latest" |
| 110 | } |
| 111 | |
| 112 | |
| 113 | make_old_release() { |
| 114 | local d="$WF/v$1" s="$WF/stage-$1/agent-vm-$1" |
| 115 | mkdir -p "$d" "$s" |
| 116 | printf '#!/usr/bin/env bash\necho "0.2 $*" >> "$WF/ran"\n' > "$s/agent-vm.sh" |
| 117 | tar -czf "$d/agent-vm-$1.tar.gz" -C "$WF/stage-$1" "agent-vm-$1" |
| 118 | printf '%s agent-vm-%s.tar.gz\n' "$(sha256 "$d/agent-vm-$1.tar.gz")" "$1" > "$d/SHA256SUMS" |
| 119 | echo "v$1" > "$WF/latest" |
| 120 | } |
| 121 | |
| 122 | |
| 123 | mkdir -p "$WF/src" |
| 124 | cp "$SELF_DIR/agent-vm.sh" "$WF/src/" |
| 125 | cat > "$WF/src/Makefile" <<'EOF' |
| 126 | agent-vm: |
| 127 | echo built >> "$$WF/builds" |
| 128 | mkdir -p _output/bin |
| 129 | cp stub _output/bin/agent-vm |
| 130 | EOF |
| 131 | stub_binary "$WF/src/stub" clone |
| 132 | echo head1 > "$WF/head" |
| 133 | |
| 134 | winst() { |
| 135 | ( export HOME="$WH" WF XDG_DATA_HOME= AGENT_VM_BIN_DIR="$WH/bin" PATH="$WBIN:$PATH" |
| 136 | sh "${WINSTALLER:-$INSTALLER}" "$@" ) 2>&1 |
| 137 | } |
| 138 | WDIR="$WH/.local/share/agent-vm" |
| 139 | |
| 140 | echo "www/public/install.sh (curl | sh)" |
| 141 | |
| 142 | |
| 143 | make_old_release 0.2.1 |
| 144 | out="$(winst)"; rc=$? |
| 145 | check "0.2 release: installs, and runs its agent-vm.sh" "$rc:$(cat "$WF/ran")" "0:0.2 install" |
| 146 | mkdir -p "$WH/bin" && ln -sfn "$WDIR/agent-vm.sh" "$WH/bin/agent-vm" |
| 147 | |
| 148 | make_release 1.0.0 |
| 149 | : > "$WF/log"; : > "$WF/ran" |
| 150 | out="$(winst)"; rc=$? |
| 151 | check "release over 0.2: installs" "$rc" "0" |
| 152 | case "$out" in *"agent-vm claude"*) pass "release: the binary's install ran" ;; *) fail "release: $out" ;; esac |
| 153 | check "release: this machine's tarball, through agent-vm.sh" "$(cat "$WF/ran")" "1.0.0-$PLATFORM install" |
| 154 | check "release: the binary linked onto the PATH, over 0.2's link" "$(readlink "$WH/bin/agent-vm")" "$WDIR/agent-vm" |
| 155 | check "release: sums from latest/, only this machine's tarball, from its tag" "$(tr '\n' ' ' < "$WF/log")" \ |
| 156 | "https://github.com/sylvinus/agent-vm/releases/latest/download/SHA256SUMS https://github.com/sylvinus/agent-vm/releases/download/v1.0.0/agent-vm-1.0.0-$PLATFORM.tar.gz " |
| 157 | |
| 158 | |
| 159 | |
| 160 | ln -sfn "$WDIR/agent-vm.sh" "$SB/old-link" |
| 161 | : > "$WF/ran" |
| 162 | ( export WF; "$SB/old-link" version ) |
| 163 | check "0.2's link to agent-vm.sh runs the binary" "$(cat "$WF/ran")" "1.0.0-$PLATFORM version" |
| 164 | : > "$WF/ran" |
| 165 | out="$(export WF; bash -c 'set +e; source "$1"; agent-vm list; echo "shell still here: $?"' _ "$WDIR/agent-vm.sh" 2>&1)" |
| 166 | check "bash rc sourcing agent-vm.sh: an agent-vm function, the shell kept" "$out:$(cat "$WF/ran")" "shell still here: 0:1.0.0-$PLATFORM list" |
| 167 | if command -v zsh >/dev/null 2>&1; then |
| 168 | : > "$WF/ran" |
| 169 | out="$(export WF; zsh -fc 'source "$1"; agent-vm list; echo "shell still here: $?"' _ "$WDIR/agent-vm.sh" 2>&1)" |
| 170 | check "zsh rc sourcing agent-vm.sh: the same" "$out:$(cat "$WF/ran")" "shell still here: 0:1.0.0-$PLATFORM list" |
| 171 | else |
| 172 | printf ' skip zsh rc (no zsh)\n' |
| 173 | fi |
| 174 | |
| 175 | make_release 1.1.0 |
| 176 | : > "$WF/ran" |
| 177 | out="$(winst)"; rc=$? |
| 178 | check "rerun: replaced by the new release" "$rc:$(cat "$WF/ran")" "0:1.1.0-$PLATFORM install" |
| 179 | check "rerun: no staging or old copy left" "$(ls -A "$WH/.local/share")" "agent-vm" |
| 180 | check "rerun: the link still leads to the binary" "$(readlink "$WH/bin/agent-vm")" "$WDIR/agent-vm" |
| 181 | |
| 182 | |
| 183 | |
| 184 | make_release 1.1.5 |
| 185 | mkdir -p "$SB/wmvfail" |
| 186 | printf '#!/bin/sh\ncase "$1" in *.new.*) exit 1 ;; esac\nexec %s "$@"\n' "$(command -v mv)" > "$SB/wmvfail/mv" |
| 187 | chmod +x "$SB/wmvfail/mv" |
| 188 | out="$(PATH="$SB/wmvfail:$PATH" winst)"; rc=$? |
| 189 | : > "$WF/ran"; ( export WF; "$WDIR/agent-vm.sh" version ) |
| 190 | check "swap cut short: the previous version is back" "$rc:$(cat "$WF/ran")" "1:1.1.0-$PLATFORM version" |
| 191 | check "swap cut short: no staging or old copy left" "$(ls -A "$WH/.local/share")" "agent-vm" |
| 192 | |
| 193 | make_release 1.2.0 corrupt |
| 194 | out="$(winst)"; rc=$? |
| 195 | : > "$WF/ran"; ( export WF; "$WDIR/agent-vm.sh" version ) |
| 196 | check "bad checksum: refused, the install is untouched" "$rc:$(cat "$WF/ran")" "1:1.1.0-$PLATFORM version" |
| 197 | case "$out" in *"checksum mismatch"*) pass "bad checksum: said" ;; *) fail "bad checksum: $out" ;; esac |
| 198 | |
| 199 | |
| 200 | mkdir -p "$SB/winbin" "$WF/v1.1.8" "$WF/stage-win/agent-vm-1.1.8" |
| 201 | printf '#!/bin/sh\ncase "$1" in -s) echo MINGW64_NT-10.0 ;; -m) echo x86_64 ;; esac\n' > "$SB/winbin/uname" |
| 202 | chmod +x "$SB/winbin/uname" |
| 203 | cp "$SELF_DIR/agent-vm.sh" "$WF/stage-win/agent-vm-1.1.8/" |
| 204 | stub_binary "$WF/stage-win/agent-vm-1.1.8/agent-vm.exe" 1.1.8-windows-amd64 |
| 205 | tar -czf "$WF/v1.1.8/agent-vm-1.1.8-windows-amd64.tar.gz" -C "$WF/stage-win" agent-vm-1.1.8 |
| 206 | printf '%s agent-vm-1.1.8-windows-amd64.tar.gz\n' "$(sha256 "$WF/v1.1.8/agent-vm-1.1.8-windows-amd64.tar.gz")" > "$WF/v1.1.8/SHA256SUMS" |
| 207 | : > "$WF/ran" |
| 208 | out="$(PATH="$SB/winbin:$PATH" winst --version 1.1.8 --dir "$SB/win")"; rc=$? |
| 209 | check "Windows (Git Bash): its tarball, agent-vm.exe run" "$rc:$(cat "$WF/ran")" "0:1.1.8-windows-amd64 install" |
| 210 | |
| 211 | |
| 212 | mkdir -p "$WF/v1.2.5" |
| 213 | printf '0000 agent-vm-1.2.5-%s.tar.gz\n' "$OTHER" > "$WF/v1.2.5/SHA256SUMS" |
| 214 | echo v1.2.5 > "$WF/latest" |
| 215 | out="$(winst)"; rc=$? |
| 216 | case "$rc:$out" in 1:*"no build for $PLATFORM"*) pass "no build for this machine: said" ;; *) fail "no build: $out" ;; esac |
| 217 | |
| 218 | : > "$WF/log" |
| 219 | out="$(winst --version v1.0.0)"; rc=$? |
| 220 | : > "$WF/ran"; ( export WF; "$WDIR/agent-vm.sh" version ) |
| 221 | check "--version: that release" "$rc:$(cat "$WF/ran")" "0:1.0.0-$PLATFORM version" |
| 222 | check "--version: its sums, from its tag" "$(head -n 1 "$WF/log")" \ |
| 223 | "https://github.com/sylvinus/agent-vm/releases/download/v1.0.0/SHA256SUMS" |
| 224 | check "--version: not a version (exit 1)" "$(winst --version 1.0 >/dev/null; echo $?)" "1" |
| 225 | check "--version with --git (exit 1)" "$(winst --version 1.0.0 --git >/dev/null; echo $?)" "1" |
| 226 | check "unknown option (exit 2)" "$(winst --nope >/dev/null; echo $?)" "2" |
| 227 | |
| 228 | make_release 1.3.0 |
| 229 | mkdir -p "$SB/wother"; echo mine > "$SB/wother/notes" |
| 230 | out="$(winst --dir "$SB/wother")"; rc=$? |
| 231 | check "a directory of the user's: refused, left as it was" "$rc:$(ls "$SB/wother")" "1:notes" |
| 232 | |
| 233 | |
| 234 | rm -rf "${WDIR:?}" "${WH:?}/bin" |
| 235 | : > "$WF/log"; : > "$WF/builds" |
| 236 | out="$(winst --git)"; rc=$? |
| 237 | check "--git: clones" "$rc:$(head -n 1 "$WF/log")" "0:git clone https://github.com/sylvinus/agent-vm.git $WDIR" |
| 238 | check "--git: built once" "$(wc -l < "$WF/builds" | tr -d ' ')" "1" |
| 239 | check "--git: the built binary linked onto the PATH" "$(readlink "$WH/bin/agent-vm")" "$WDIR/_output/bin/agent-vm" |
| 240 | out="$(winst --git)"; rc=$? |
| 241 | check "--git again: pulls" "$rc:$(tail -n 1 "$WF/log")" "0:git -C $WDIR pull --ff-only" |
| 242 | check "--git again, nothing pulled: not rebuilt" "$(wc -l < "$WF/builds" | tr -d ' ')" "1" |
| 243 | |
| 244 | echo head2 > "$WF/head" |
| 245 | ln -sfn "$WDIR/agent-vm.sh" "$SB/clone-link" |
| 246 | : > "$WF/ran" |
| 247 | ( export WF PATH="$WBIN:$PATH"; "$SB/clone-link" version ) 2>/dev/null |
| 248 | check "a 0.2 link into a clone, after a pull: rebuilt, then run" "$(wc -l < "$WF/builds" | tr -d ' '):$(cat "$WF/ran")" "2:clone version" |
| 249 | out="$(winst)"; rc=$? |
| 250 | check "release over a clone: refused" "$rc" "1" |
| 251 | case "$out" in *"is a git clone"*"git -C"*) pass "release over a clone: says how to update it" ;; *) fail "release over a clone: $out" ;; esac |
| 252 | |
| 253 | |
| 254 | rm -rf "$WDIR" |
| 255 | sed '$d' "$INSTALLER" > "$SB/install-cut.sh" |
| 256 | out="$(WINSTALLER="$SB/install-cut.sh" winst)"; rc=$? |
| 257 | check "cut short: runs nothing" "$rc:$([ -e "$WDIR" ] && echo there)" "0:" |
| 258 | |
| 259 | printf '\n%s passed, %s failed\n' "$PASSED" "$FAIL" |
| 260 | [ "$FAIL" -eq 0 ] || exit 1 |