Visual Studio Code, rebuilt in Rust
Rust
#!/bin/bash
# Creates, starts and provisions one GUI lane VM on the Mac Studio (port/tools/gui-lane): port/tools/gui-vm gui-vm1
# port/tools/gui-vm gui-vm2 gui-vm1 clones a stopped gui-vm1 instead, so both lanes run the same macOS build.
# Runs on the Studio host. A tart clone of the Cirrus Labs macOS base image (SIP off, user admin/admin), 4 CPUs,
# 6 GB, the host's 1814x910 pt display at 2x, headless. Shares ~/Developer/vscode-gpui-work, ~/Developer/vscode-ref-1.148.0
# (the tools' default reference) and ~/Developer/vscode-ref (1.147.0, until its last session ends) read-only (tools
# write to /tmp in the VM), linked in the VM at the host's paths. Copies cua-driver and bun from the host and grants
# cua-driver Accessibility, Screen Recording and the capture alert. Every step can run again.
set -euo pipefail
vm=$1
source=${2:-ghcr.io/cirruslabs/macos-golden-gate-base:latest}
if ! tart get "$vm" > /dev/null 2>&1; then
tart clone "$source" "$vm"
tart set "$vm" --cpu 4 --memory 6144 --display 1814x910pt --random-mac
fi
if ! tart list --format json | jq -e --arg vm "$vm" '.[] | select(.Name == $vm and .Running)' > /dev/null; then
# A launchd job: tart run started from an ssh session stops with the session, nohup or not.
agent=$HOME/Library/LaunchAgents/org.vsrustium.$vm.plist
plutil -create xml1 "$agent"
plutil -insert Label -string "org.vsrustium.$vm" "$agent"
plutil -insert ProgramArguments -json "[\"$(command -v tart)\",\"run\",\"$vm\",\"--no-graphics\",\"--dir=work:$HOME/Developer/vscode-gpui-work:ro\",\"--dir=ref:$HOME/Developer/vscode-ref:ro\",\"--dir=ref148:$HOME/Developer/vscode-ref-1.148.0:ro\"]" "$agent"
plutil -insert RunAtLoad -bool true "$agent"
plutil -insert StandardOutPath -string "/tmp/$vm.log" "$agent"
plutil -insert StandardErrorPath -string "/tmp/$vm.log" "$agent"
launchctl bootout "gui/$(id -u)/org.vsrustium.$vm" 2> /dev/null || true
launchctl bootstrap "gui/$(id -u)" "$agent"
tart ip "$vm" --wait 300 > /dev/null
fi
until tart exec "$vm" true 2> /dev/null; do sleep 2; done
key=$(cat ~/.ssh/id_ed25519.pub)
tart exec "$vm" bash -c "mkdir -p ~/.ssh && { grep -qF '$key' ~/.ssh/authorized_keys 2>/dev/null || echo '$key' >> ~/.ssh/authorized_keys; }"
ssh_options='-q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR'
host=admin@$(tart ip "$vm")
rsync -a --delete -e "ssh $ssh_options" /Applications/CuaDriver.app ~/.bun/bin/bun "$host:/tmp/gui-vm/"
# The host's global defaults: dark appearance, glass tint and scroll bars change the window chrome in shots.
defaults export -g - | ssh $ssh_options "$host" defaults import -g -
# And its time zone (the image is on GMT), for times the apps show.
ssh $ssh_options "$host" sudo systemsetup -settimezone "$(readlink /etc/localtime | sed 's|.*/zoneinfo/||')" > /dev/null
ssh $ssh_options "$host" bash -s << 'EOF'
set -euo pipefail
sudo mkdir -p /Users/artur/Developer
sudo ln -sfn '/Volumes/My Shared Files/work' /Users/artur/Developer/vscode-gpui-work
sudo ln -sfn '/Volumes/My Shared Files/ref' /Users/artur/Developer/vscode-ref
sudo ln -sfn '/Volumes/My Shared Files/ref148' /Users/artur/Developer/vscode-ref-1.148.0
ln -sfn /Users/artur/Developer ~/Developer
sudo rm -rf /Applications/CuaDriver.app
sudo cp -R /tmp/gui-vm/CuaDriver.app /Applications/
mkdir -p ~/.local/bin ~/.bun/bin
cp /tmp/gui-vm/bun ~/.bun/bin/bun
ln -sfn /Applications/CuaDriver.app/Contents/MacOS/cua-driver ~/.local/bin/cua-driver
grep -q local/bin ~/.bash_profile 2> /dev/null || echo 'export PATH=$HOME/.local/bin:$HOME/.bun/bin:/opt/homebrew/bin:$PATH' >> ~/.bash_profile
# softwareupdated installed 27.2.1 and restarted the VM in the middle of a run: no automatic updates.
sudo softwareupdate --schedule off > /dev/null
for key in AutomaticCheckEnabled AutomaticDownload AutomaticallyInstallMacOSUpdates CriticalUpdateInstall ConfigDataInstall; do
sudo defaults write /Library/Preferences/com.apple.SoftwareUpdate "$key" -bool false
done
sudo defaults write /Library/Preferences/com.apple.commerce AutoUpdate -bool false
sudo defaults write /Library/Preferences/com.apple.commerce AutoUpdateRestartRequired -bool false
# TCC grants by bundle id and designated requirement, in the system and the user database (macOS 27 keeps the user
# one in a ProtectedSystem container, found through tccd's open files as Cirrus's update-tcc-database.sh does).
codesign -d -r- /Applications/CuaDriver.app 2>&1 | sed -n 's/^designated => //p' > /tmp/gui-vm/req.txt
csreq -r /tmp/gui-vm/req.txt -b /tmp/gui-vm/req.bin
csreq=$(xxd -p /tmp/gui-vm/req.bin | tr -d '\n')
user_db=$(sudo lsof -a -u "$(id -u)" -c tccd -Fn | sed -n 's|^n\(.*/ProtectedSystem/.*/com.apple.TCC/TCC.db\)$|\1|p' | head -1)
for db in '/Library/Application Support/com.apple.TCC/TCC.db' "$user_db"; do
for service in kTCCServiceAccessibility kTCCServiceScreenCapture kTCCServicePostEvent; do
sudo sqlite3 "$db" "INSERT OR REPLACE INTO access (service, client_type, client, auth_value, auth_reason, auth_version, csreq, indirect_object_identifier_type, indirect_object_identifier) VALUES ('$service', 0, 'com.trycua.driver', 2, 0, 1, X'$csreq', NULL, 'UNUSED');"
done
done
# Without a replayd approval cua-driver refuses to capture: the first capture would show the system alert.
approvals=~/Library/Group\ Containers/group.com.apple.replayd/ScreenCaptureApprovals.plist
mkdir -p "$(dirname "$approvals")"
[[ -f $approvals ]] || plutil -create xml1 "$approvals"
key='com\.trycua\.driver'
now=$(date -u +%Y-%m-%dT%H:%M:%SZ)
plutil -replace "$key" -dictionary "$approvals"
plutil -insert "$key.kScreenCaptureAlertableUsageCount" -integer 1 "$approvals"
plutil -insert "$key.kScreenCaptureApprovalLastAlerted" -date "$now" "$approvals"
plutil -insert "$key.kScreenCaptureApprovalLastUsed" -date "$now" "$approvals"
plutil -insert "$key.kScreenCapturePrivacyHintDate" -date 2100-01-01T00:00:00Z "$approvals"
plutil -insert "$key.kScreenCapturePrivacyHintPolicy" -integer 7776000 "$approvals"
killall replayd 2> /dev/null || true
# The daemon through LaunchServices, so its TCC identity is the app's; again at every login.
mkdir -p ~/Library/LaunchAgents
plist=~/Library/LaunchAgents/com.trycua.driver.serve.plist
plutil -create xml1 "$plist"
plutil -insert Label -string com.trycua.driver.serve "$plist"
plutil -insert ProgramArguments -json '["/usr/bin/open","-g","-a","/Applications/CuaDriver.app","--args","serve"]' "$plist"
plutil -insert RunAtLoad -bool true "$plist"
~/.local/bin/cua-driver stop > /dev/null 2>&1 || true
# open hands a still-exiting daemon the request and nothing is left running.
while pgrep -f 'CuaDriver.app/Contents/MacOS/cua-driver serve' > /dev/null; do perl -e 'select(undef, undef, undef, 0.2)'; done
open -g -a /Applications/CuaDriver.app --args serve
until ~/.local/bin/cua-driver status > /dev/null 2>&1; do sleep 0.5; done
~/.local/bin/cua-driver call get_desktop_state '{"screenshot_out_file":"/tmp/gui-vm/desktop.png"}' | grep -E '"scale_factor"|"screen_width"'
EOF