-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·177 lines (160 loc) · 5.18 KB
/
Copy pathrun.sh
File metadata and controls
executable file
·177 lines (160 loc) · 5.18 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env bash
set -euo pipefail
MODE="${1:-run}"
if [[ "${CODETWO_DEV_PROFILE+x}" == x ]]; then
PROFILE_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
if [[ -d /opt/homebrew/opt/zig@0.15/bin ]]; then
export PATH="/opt/homebrew/opt/zig@0.15/bin:$PATH"
fi
exec bun "$PROFILE_ROOT/apps/desktop/scripts/run-profile.ts" "$MODE"
fi
BUNDLE_ID="dev.codetwo.app.dev"
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
DESKTOP_DIR="$ROOT_DIR/apps/desktop"
case "$(uname -m)" in
arm64|aarch64) ELECTROBUN_ARCH="arm64" ;;
x86_64) ELECTROBUN_ARCH="x64" ;;
*)
echo "unsupported macOS architecture: $(uname -m)" >&2
exit 1
;;
esac
APP_BUNDLE="$DESKTOP_DIR/build/dev-macos-$ELECTROBUN_ARCH/C2-dev.app"
APP_EXECUTABLE="$APP_BUNDLE/Contents/MacOS/launcher"
STATE_DIR="$ROOT_DIR/.codex/run"
PID_FILE="$STATE_DIR/codetwo-dev.pid"
APP_RUNNER_PID=""
# Homebrew keeps versioned Zig formulae keg-only. Keep the project requirement
# local to this launcher rather than modifying the user's global shell setup.
if [[ -d /opt/homebrew/opt/zig@0.15/bin ]]; then
export PATH="/opt/homebrew/opt/zig@0.15/bin:$PATH"
fi
require_command() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "missing required command: $1" >&2
exit 1
fi
}
# Inspect only this launcher's tracked owner. This is not a cross-process data-directory lock.
find_existing() {
EXISTING_PID=""
if [[ -f "$PID_FILE" ]]; then
local previous_pid previous_command
previous_pid="$(<"$PID_FILE")"
if [[ "$previous_pid" =~ ^[1-9][0-9]*$ ]] && [[ "$previous_pid" -gt 1 ]] && kill -0 "$previous_pid" >/dev/null 2>&1; then
previous_command="$(ps -p "$previous_pid" -o command= 2>/dev/null || true)"
if [[ "$previous_command" == "$APP_EXECUTABLE" || "$previous_command" == "$APP_EXECUTABLE "* ]]; then
EXISTING_PID="$previous_pid"
fi
fi
fi
}
require_stopped() {
find_existing
if [[ -n "$EXISTING_PID" ]]; then
echo "C2 already running (pid: $EXISTING_PID). Use --logs to inspect it or --restart to replace it explicitly." >&2
exit 1
fi
}
restart_existing() {
find_existing
if [[ -z "$EXISTING_PID" ]]; then return; fi
kill "$EXISTING_PID"
for _ in {1..20}; do
if ! kill -0 "$EXISTING_PID" >/dev/null 2>&1; then return; fi
sleep 0.1
done
echo "C2 process $EXISTING_PID did not exit; refusing to rebuild or start another instance." >&2
exit 1
}
cleanup() {
if [[ -n "$APP_RUNNER_PID" ]]; then
kill "$APP_RUNNER_PID" >/dev/null 2>&1 || true
wait "$APP_RUNNER_PID" >/dev/null 2>&1 || true
fi
if [[ -f "$PID_FILE" ]] && [[ "$(<"$PID_FILE")" == "$APP_RUNNER_PID" ]]; then
rm -f "$PID_FILE"
fi
}
build_app() {
require_command bun
require_command cargo
require_command zig
if [[ "$(zig version)" != "0.15.2" ]]; then
echo "C2 requires Zig 0.15.2; found $(zig version)." >&2
exit 1
fi
cd "$DESKTOP_DIR"
if [[ ! -d node_modules ]]; then
bun install --frozen-lockfile
fi
# Always run from the generated bundle so macOS can attribute microphone and speech-recognition
# permission prompts to C2 instead of to a loose helper executable.
bun run build
if [[ ! -x "$APP_EXECUTABLE" ]]; then
echo "Electrobun did not create $APP_EXECUTABLE" >&2
exit 1
fi
actual_bundle_id="$(/usr/bin/plutil -extract CFBundleIdentifier raw -- "$APP_BUNDLE/Contents/Info.plist")"
if [[ "$actual_bundle_id" != "$BUNDLE_ID" ]]; then
echo "C2-dev.app has bundle identifier $actual_bundle_id; expected $BUNDLE_ID." >&2
exit 1
fi
for privacy_key in NSMicrophoneUsageDescription NSSpeechRecognitionUsageDescription; do
privacy_value="$(/usr/bin/plutil -extract "$privacy_key" raw -- "$APP_BUNDLE/Contents/Info.plist")"
if [[ -z "$privacy_value" ]]; then
echo "C2-dev.app has an empty $privacy_key value." >&2
exit 1
fi
done
}
start_app() {
mkdir -p "$STATE_DIR"
"$APP_EXECUTABLE" &
APP_RUNNER_PID=$!
echo "$APP_RUNNER_PID" > "$PID_FILE"
}
wait_for_app() {
for _ in {1..20}; do
if ! kill -0 "$APP_RUNNER_PID" >/dev/null 2>&1; then
wait "$APP_RUNNER_PID"
return $?
fi
sleep 0.25
done
echo "C2 launched successfully (pid: $APP_RUNNER_PID)."
}
case "$MODE" in
run|--verify|verify|--debug|debug|--restart|restart)
if [[ "$MODE" == --restart || "$MODE" == restart ]]; then
restart_existing
else
require_stopped
fi
if [[ "$MODE" == --debug || "$MODE" == debug ]]; then
export RUST_BACKTRACE=1
export RUST_LOG="${RUST_LOG:-debug}"
fi
trap cleanup EXIT INT TERM
build_app
start_app
if [[ "$MODE" == --verify || "$MODE" == verify ]]; then wait_for_app; fi
wait "$APP_RUNNER_PID"
;;
--logs|logs|--telemetry|telemetry)
find_existing
if [[ -z "$EXISTING_PID" ]]; then
echo "No tracked C2 instance is running. Start it with $0 run first." >&2
exit 1
fi
predicate="processID == $EXISTING_PID"
if [[ "$MODE" == --telemetry || "$MODE" == telemetry ]]; then
predicate="$predicate AND subsystem == \"$BUNDLE_ID\""
fi
exec /usr/bin/log stream --info --style compact --predicate "$predicate"
;;
*)
echo "usage: $0 [run|--verify|--debug|--restart|--logs|--telemetry]" >&2
exit 2
;;
esac