Repository navigation
Expand file tree
/
Copy pathcompile_and_run.sh
More file actions
executable file
·136 lines (118 loc) · 3.74 KB
/
Copy pathcompile_and_run.sh
File metadata and controls
executable file
·136 lines (118 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env bash
# Reset CodexBar: kill running instances, build, package, relaunch, verify.
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
APP_BUNDLE="${ROOT_DIR}/CodexBar.app"
APP_PROCESS_PATTERN="CodexBar.app/Contents/MacOS/CodexBar"
DEBUG_PROCESS_PATTERN="${ROOT_DIR}/.build/debug/CodexBar"
RELEASE_PROCESS_PATTERN="${ROOT_DIR}/.build/release/CodexBar"
LOCK_KEY="$(printf '%s' "${ROOT_DIR}" | shasum -a 256 | cut -c1-8)"
LOCK_DIR="${TMPDIR:-/tmp}/codexbar-compile-and-run-${LOCK_KEY}"
LOCK_PID_FILE="${LOCK_DIR}/pid"
WAIT_FOR_LOCK=0
RUN_TESTS=0
DEBUG_LLDB=0
log() { printf '%s\n' "$*"; }
fail() { printf 'ERROR: %s\n' "$*" >&2; exit 1; }
run_step() {
local label="$1"; shift
log "==> ${label}"
if ! "$@"; then
fail "${label} failed"
fi
}
cleanup() {
if [[ -d "${LOCK_DIR}" ]]; then
rm -rf "${LOCK_DIR}"
fi
}
acquire_lock() {
while true; do
if mkdir "${LOCK_DIR}" 2>/dev/null; then
echo "$$" > "${LOCK_PID_FILE}"
return 0
fi
local existing_pid=""
if [[ -f "${LOCK_PID_FILE}" ]]; then
existing_pid="$(cat "${LOCK_PID_FILE}" 2>/dev/null || true)"
fi
if [[ -n "${existing_pid}" ]] && kill -0 "${existing_pid}" 2>/dev/null; then
if [[ "${WAIT_FOR_LOCK}" == "1" ]]; then
log "==> Another agent is compiling (pid ${existing_pid}); waiting..."
while kill -0 "${existing_pid}" 2>/dev/null; do
sleep 1
done
continue
fi
log "==> Another agent is compiling (pid ${existing_pid}); re-run with --wait."
exit 0
fi
rm -rf "${LOCK_DIR}"
done
}
trap cleanup EXIT INT TERM
kill_claude_probes() {
# CodexBar spawns `claude /usage` + `/status` in a PTY; if we kill the app mid-probe we can orphan them.
pkill -f "claude (/status|/usage) --allowed-tools" 2>/dev/null || true
sleep 0.2
pkill -9 -f "claude (/status|/usage) --allowed-tools" 2>/dev/null || true
}
kill_all_codexbar() {
for _ in {1..10}; do
pkill -f "${APP_PROCESS_PATTERN}" 2>/dev/null || true
pkill -f "${DEBUG_PROCESS_PATTERN}" 2>/dev/null || true
pkill -f "${RELEASE_PROCESS_PATTERN}" 2>/dev/null || true
pkill -x "CodexBar" 2>/dev/null || true
# If nothing remains, stop early.
if ! pgrep -f "${APP_PROCESS_PATTERN}" >/dev/null 2>&1 \
&& ! pgrep -f "${DEBUG_PROCESS_PATTERN}" >/dev/null 2>&1 \
&& ! pgrep -f "${RELEASE_PROCESS_PATTERN}" >/dev/null 2>&1 \
&& ! pgrep -x "CodexBar" >/dev/null 2>&1; then
return 0
fi
sleep 0.3
done
}
# 1) Ensure a single runner instance.
for arg in "$@"; do
case "${arg}" in
--wait|-w) WAIT_FOR_LOCK=1 ;;
--test|-t) RUN_TESTS=1 ;;
--debug-lldb) DEBUG_LLDB=1 ;;
--help|-h)
log "Usage: $(basename "$0") [--wait] [--test] [--debug-lldb]"
exit 0
;;
*)
;;
esac
done
acquire_lock
# 2) Kill all running CodexBar instances (debug, release, bundled).
log "==> Killing existing CodexBar instances"
kill_all_codexbar
kill_claude_probes
# 3) Build, package.
run_step "swift build" swift build -q
if [[ "${RUN_TESTS}" == "1" ]]; then
run_step "swift test" swift test -q
fi
if [[ "${DEBUG_LLDB}" == "1" ]]; then
run_step "package app" env CODEXBAR_ALLOW_LLDB=1 "${ROOT_DIR}/scripts/package_app.sh" debug
else
run_step "package app" "${ROOT_DIR}/scripts/package_app.sh"
fi
# 4) Launch the packaged app.
log "==> launch app"
if ! open -n "${APP_BUNDLE}"; then
log "WARN: launch app returned non-zero; falling back to direct binary launch."
"${APP_BUNDLE}/Contents/MacOS/CodexBar" >/dev/null 2>&1 &
disown
fi
# 5) Verify the app stays up for at least 1s.
sleep 1
if pgrep -f "${APP_PROCESS_PATTERN}" >/dev/null 2>&1; then
log "OK: CodexBar is running."
else
fail "App exited immediately. Check crash logs in Console.app (User Reports)."
fi