-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdev.sh
More file actions
executable file
·186 lines (165 loc) · 5.58 KB
/
Copy pathdev.sh
File metadata and controls
executable file
·186 lines (165 loc) · 5.58 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
#!/usr/bin/env bash
# Claudette dev launcher.
#
# Probes for the first free Vite port (starting at 14253 — deliberately NOT
# Tauri's stock 1420, so other Tauri starter-template dev builds can't
# accidentally rebind our port and swap their bundle into our webview) and
# the first free debug eval port (starting at 19432), exports them for the
# child processes, then starts `cargo tauri dev` with an inline config
# override so the webview loads from the port Vite actually bound.
#
# A discovery file is written to ${TMPDIR:-/tmp}/claudette-dev/<pid>.json so
# helpers like `debug-eval.sh` can find the matching instance when multiple
# dev builds run side-by-side. The file is cleaned up on exit.
#
# Env overrides:
# VITE_PORT_BASE start port for Vite probe (default 14253)
# CLAUDETTE_DEBUG_PORT_BASE start port for debug probe (default 19432)
# CARGO_TAURI_FEATURES features to pass to tauri (default devtools,server,voice)
set -euo pipefail
repo_root="$(cd "$(dirname "$0")/.." && pwd)"
cd "$repo_root"
state_dir="$repo_root/target/.claudette-dev"
pid_file="$state_dir/dev.pid"
mkdir -p "$state_dir"
collect_tree_pids() {
local pid="$1"
local child
echo "$pid"
while IFS= read -r child; do
[ -n "$child" ] || continue
collect_tree_pids "$child"
done < <(pgrep -P "$pid" 2>/dev/null || true)
}
kill_tree() {
local pid="$1"
local pids
pids="$(collect_tree_pids "$pid" | sort -rn | tr '\n' ' ')"
[ -n "$pids" ] || return
# shellcheck disable=SC2086
kill $pids 2>/dev/null || true
}
kill_tree_now() {
local pid="$1"
local pids
pids="$(collect_tree_pids "$pid" | sort -rn | tr '\n' ' ')"
[ -n "$pids" ] || return
# Give children one short chance to run their cleanup, then force the
# remaining process tree down. Ctrl+C in dev should return control
# immediately, not leave cargo-tauri/Vite/app children alive.
# shellcheck disable=SC2086
kill $pids 2>/dev/null || true
sleep 0.15
# shellcheck disable=SC2086
kill -9 $pids 2>/dev/null || true
}
is_expected_dev_pid() {
local pid="$1"
local command
command="$(ps -p "$pid" -o command= 2>/dev/null || true)"
case "$command" in
*"scripts/dev.sh"*|*"cargo tauri dev"*) return 0 ;;
*) return 1 ;;
esac
}
terminate_existing_dev() {
if [ ! -f "$pid_file" ]; then
return
fi
local old_pid
old_pid="$(cat "$pid_file" 2>/dev/null || true)"
if [ -z "$old_pid" ] || [ "$old_pid" = "$$" ]; then
rm -f "$pid_file"
return
fi
if ! kill -0 "$old_pid" 2>/dev/null; then
rm -f "$pid_file"
return
fi
if ! is_expected_dev_pid "$old_pid"; then
echo "▸ Ignoring stale dev pid file for unrelated process (pid $old_pid)"
rm -f "$pid_file"
return
fi
echo "▸ Stopping existing dev process tree (pid $old_pid)"
kill_tree "$old_pid"
for _ in {1..80}; do
if ! kill -0 "$old_pid" 2>/dev/null; then
rm -f "$pid_file"
return
fi
sleep 0.1
done
kill -9 "$old_pid" 2>/dev/null || true
rm -f "$pid_file"
}
find_free_port() {
local p=$1
while lsof -iTCP:"$p" -sTCP:LISTEN -n -P >/dev/null 2>&1; do
p=$((p + 1))
done
echo "$p"
}
terminate_existing_dev
echo "$$" >"$pid_file"
# Default Vite port is 14253 — deliberately moved off Tauri's stock 1420
# to avoid the cross-app dev-port hijack scenario where another Tauri
# starter template (which also defaults to 1420) launches and rebinds
# the port underneath our running webview, displaying its own bundle in
# Claudette's window. The inline guard in src/ui/index.html catches the
# residual case where another app still picks the same number.
vite_port=$(find_free_port "${VITE_PORT_BASE:-14253}")
debug_port=$(find_free_port "${CLAUDETTE_DEBUG_PORT_BASE:-19432}")
export VITE_PORT="$vite_port"
export CLAUDETTE_DEBUG_PORT="$debug_port"
discovery_dir="${TMPDIR:-/tmp}/claudette-dev"
mkdir -p "$discovery_dir"
discovery_file="$discovery_dir/$$.json"
branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo unknown)"
cwd="$(pwd)"
started="$(date +%s)"
# Build JSON via python3's json module so paths or branch names containing
# quotes, backslashes, or newlines don't break the discovery file. `python3`
# is already an explicit prerequisite of the debug eval helper, so making
# the devshell depend on it too is consistent.
python3 -c '
import json, sys
out, pid, debug_port, vite_port, started, cwd, branch = sys.argv[1:8]
with open(out, "w") as f:
json.dump({
"pid": int(pid),
"debug_port": int(debug_port),
"vite_port": int(vite_port),
"cwd": cwd,
"branch": branch,
"started_at": int(started),
}, f)
' "$discovery_file" "$$" "$debug_port" "$vite_port" "$started" "$cwd" "$branch"
cleanup() {
rm -f "$discovery_file"
if [ -f "$pid_file" ] && [ "$(cat "$pid_file" 2>/dev/null || true)" = "$$" ]; then
rm -f "$pid_file"
fi
if [ -n "${cargo_pid:-}" ] && kill -0 "$cargo_pid" 2>/dev/null; then
kill_tree_now "$cargo_pid"
fi
}
trap cleanup EXIT
trap 'cleanup; exit 130' INT TERM
echo "▸ Branch: $branch"
echo "▸ Vite dev server: http://localhost:$vite_port"
echo "▸ Debug eval port: $debug_port"
echo "▸ Discovery file: $discovery_file"
(cd src/ui && bun install)
host_triple="$(rustc -vV | awk '/host:/ {print $2}')"
scripts/stage-cli-sidecar.sh "$host_triple" --profile debug
features="${CARGO_TAURI_FEATURES:-devtools,server,voice}"
runner_args=()
if [[ "$(uname -s)" == "Darwin" ]]; then
runner_args=(--runner "$repo_root/scripts/macos-dev-app-runner.sh")
fi
cargo tauri dev --features "$features" \
"${runner_args[@]}" \
-c "{\"build\":{\"devUrl\":\"http://localhost:$vite_port\"}}" &
cargo_pid=$!
wait "$cargo_pid"