Repository navigation
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·205 lines (187 loc) · 8.21 KB
/
Copy pathdev.sh
File metadata and controls
executable file
·205 lines (187 loc) · 8.21 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env bash
# Opal Dev Server — HMR-style rebuild loop.
#
# Watches src/ tools/ build.zig. On change:
# - Rebuild. On failure, keep old binary running (zero downtime).
# - On success, kill old, launch new.
# - Session restore preserves player state across restarts.
#
# Usage: ./dev.sh # Debug
# ./dev.sh -r # ReleaseFast
# ./dev.sh -v # Verbose (full build output)
# ./dev.sh -- <args> # Pass args through to the binary
IFS=$'\n\t'
# ── Flags ──────────────────────────────────────────────────────
OPTIMIZE=""
VERBOSE=0
PASSTHROUGH=()
while [[ $# -gt 0 ]]; do
case "$1" in
-r|--release) OPTIMIZE="-Doptimize=ReleaseFast"; shift ;;
-v|--verbose) VERBOSE=1; shift ;;
--) shift; PASSTHROUGH=("$@"); break ;;
*) PASSTHROUGH+=("$1"); shift ;;
esac
done
BIN="./zig-out/bin/opal"
WATCH_PATHS=(src tools build.${ZIG:-zig} build.zig.zon)
PID=""
BUILD_N=0
DEBOUNCE_MS=150
# ── Colors ─────────────────────────────────────────────────────
C_DIM="\033[2m"; C_RST="\033[0m"
C_CYAN="\033[36m"; C_GREEN="\033[32m"; C_RED="\033[31m"
C_YELLOW="\033[33m"; C_MAGENTA="\033[1;35m"
log() { printf "%b[%s]%b %b\n" "$C_DIM" "$(date +%H:%M:%S)" "$C_RST" "$*"; }
ok() { printf "%b[%s]%b %b✓%b %b\n" "$C_DIM" "$(date +%H:%M:%S)" "$C_RST" "$C_GREEN" "$C_RST" "$*"; }
warn() { printf "%b[%s]%b %b⚠%b %b\n" "$C_DIM" "$(date +%H:%M:%S)" "$C_RST" "$C_YELLOW" "$C_RST" "$*"; }
err() { printf "%b[%s]%b %b✗%b %b\n" "$C_DIM" "$(date +%H:%M:%S)" "$C_RST" "$C_RED" "$C_RST" "$*"; }
# ── Watcher picker ─────────────────────────────────────────────
pick_watcher() {
command -v fswatch >/dev/null 2>&1 && { echo "fswatch"; return; }
command -v inotifywait >/dev/null 2>&1 && { echo "inotifywait"; return; }
command -v watchexec >/dev/null 2>&1 && { echo "watchexec"; return; }
echo ""
}
WATCHER="$(pick_watcher)"
if [[ -z "$WATCHER" ]]; then
err "No file watcher found."
echo " macOS: brew install fswatch (or watchexec)"
echo " Linux: apt install inotify-tools (or cargo install watchexec-cli)"
exit 1
fi
# ── Cleanup ────────────────────────────────────────────────────
kill_running() {
if [[ -n "$PID" ]] && kill -0 "$PID" 2>/dev/null; then
kill -TERM "$PID" 2>/dev/null || true
for _ in 1 2 3 4 5; do
kill -0 "$PID" 2>/dev/null || break
sleep 0.1
done
kill -KILL "$PID" 2>/dev/null || true
wait "$PID" 2>/dev/null || true
fi
pkill -f lang_server.py 2>/dev/null || true
# The DPI-bypass proxy only stops cleanly via main.zig's shutdown path, which
# a SIGKILL above never reaches. It binds SO_REUSEPORT, so a survivor keeps
# LISTENing alongside the next one instead of failing — 57 of them piled up
# over two days, splitting traffic across stale --mode settings.
pkill -f zig-bypassdpi 2>/dev/null || true
PID=""
}
cleanup() {
echo
warn "Shutting down…"
kill_running
pkill -f opal-voice-server 2>/dev/null || true
pkill -f opal-tts-server 2>/dev/null || true
pkill -f opal-stt-server 2>/dev/null || true
pkill -f Suwayomi-Server- 2>/dev/null || true
pkill -f engines/nova2.py 2>/dev/null || true
exit 0
}
trap cleanup INT TERM
# ── Build + launch ─────────────────────────────────────────────
build_and_launch() {
BUILD_N=$((BUILD_N + 1))
local t0 t1 elapsed out rc
if date +%s%N 2>/dev/null | grep -q N; then
# BSD date (macOS) — no %N support; fall back to seconds.
t0=$(date +%s)
else
t0=$(date +%s%N)
fi
printf "%b[%s]%b %b◆%b build #%d … " \
"$C_DIM" "$(date +%H:%M:%S)" "$C_RST" "$C_CYAN" "$C_RST" "$BUILD_N"
if (( VERBOSE )); then
echo
${ZIG:-zig} build $OPTIMIZE
rc=$?
out=""
else
out=$(${ZIG:-zig} build $OPTIMIZE 2>&1)
rc=$?
fi
if date +%s%N 2>/dev/null | grep -q N; then
t1=$(date +%s)
elapsed="$(( t1 - t0 ))s"
else
t1=$(date +%s%N)
elapsed="$(( (t1 - t0) / 1000000 ))ms"
fi
if [[ $rc -eq 0 ]]; then
(( VERBOSE )) || printf "%bok%b %s\n" "$C_GREEN" "$C_RST" "$elapsed"
kill_running
# Launch through a lightweight .app skeleton so macOS gives the dev run a
# real bundle identity (name "Opal", logo in Dock + About, Credits body).
# Falls back to the bare binary if the wrapper can't be built. CWD stays
# the project root, so CWD-relative resource lookups still resolve.
local launch_bin
launch_bin=$(./scripts/dev-app.sh 2>/dev/null) || launch_bin="$BIN"
if [[ ${#PASSTHROUGH[@]} -gt 0 ]]; then
"$launch_bin" "${PASSTHROUGH[@]}" &
else
"$launch_bin" &
fi
PID=$!
ok "launched pid=$PID ${C_DIM}(Ctrl+C to stop)${C_RST}"
else
(( VERBOSE )) || printf "%bFAIL%b %s\n" "$C_RED" "$C_RST" "$elapsed"
if (( ! VERBOSE )); then
echo "$out" \
| grep -v "warning(link)" \
| grep -E "(error:|note:|\.zig:[0-9]+:[0-9]+:)" \
| head -20 \
| sed "s/^/ /"
fi
if [[ -n "$PID" ]] && kill -0 "$PID" 2>/dev/null; then
warn "Keeping previous build running (pid=$PID)"
fi
fi
}
# ── Banner ─────────────────────────────────────────────────────
printf "%b╔════════════════════════════════════════════╗%b\n" "$C_MAGENTA" "$C_RST"
printf "%b║ Opal Dev Server ║%b\n" "$C_MAGENTA" "$C_RST"
printf "%b║ watcher: %-32s ║%b\n" "$C_MAGENTA" "$WATCHER" "$C_RST"
printf "%b║ mode: %-32s ║%b\n" "$C_MAGENTA" "${OPTIMIZE:-Debug}" "$C_RST"
OLD_IFS="$IFS"; IFS=' '; PATHS_STR="${WATCH_PATHS[*]}"; IFS="$OLD_IFS"
printf "%b║ paths: %-32s ║%b\n" "$C_MAGENTA" "$PATHS_STR" "$C_RST"
printf "%b╚════════════════════════════════════════════╝%b\n" "$C_MAGENTA" "$C_RST"
build_and_launch
# ── Watch loop (per-watcher) ────────────────────────────────────
debounce() { sleep "$(awk "BEGIN{printf \"%.3f\", $DEBOUNCE_MS/1000}")"; }
case "$WATCHER" in
fswatch)
fswatch -0 -o -r \
--exclude 'zig-cache' --exclude 'zig-out' --exclude '\.zig-cache' \
--exclude '\.swp$' --exclude '~$' --exclude '\.git' \
"${WATCH_PATHS[@]}" \
| while IFS= read -r -d '' _ ; do
debounce
log "change detected"
build_and_launch
done
;;
inotifywait)
while true; do
inotifywait -qq -r -e modify,create,delete,move \
--exclude '(\.git|zig-cache|zig-out|\.swp|~$)' \
"${WATCH_PATHS[@]}"
debounce
log "change detected"
build_and_launch
done
;;
watchexec)
# Hand off process management to watchexec.
kill_running
log "watchexec will manage the child process"
exec watchexec \
--no-vcs-ignore \
--ignore 'zig-cache/**' --ignore 'zig-out/**' --ignore '.zig-cache/**' \
--ignore '*.swp' --ignore '*~' \
--watch src --watch tools --watch build.zig --watch build.zig.zon \
--on-busy-update restart --stop-signal SIGTERM \
-- bash -c "${ZIG:-zig} build $OPTIMIZE && exec \"\$(./scripts/dev-app.sh 2>/dev/null || echo $BIN)\" ${PASSTHROUGH[*]:-}"
;;
esac