-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesktop.sh
More file actions
executable file
·306 lines (278 loc) · 8.28 KB
/
desktop.sh
File metadata and controls
executable file
·306 lines (278 loc) · 8.28 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_PATH="$(readlink -f "${BASH_SOURCE[0]}")"
ROOT_DIR="$(cd "$(dirname "$SCRIPT_PATH")" && pwd)"
WBEAM_CONFIG_HELPER="$ROOT_DIR/host/scripts/wbeam_config.sh"
WBEAM_GUI_HELPER="$ROOT_DIR/host/scripts/gui_session.sh"
if [[ -f "$WBEAM_CONFIG_HELPER" ]]; then
# shellcheck source=host/scripts/wbeam_config.sh
source "$WBEAM_CONFIG_HELPER"
wbeam_load_config "$ROOT_DIR"
fi
if [[ -f "$WBEAM_GUI_HELPER" ]]; then
# shellcheck source=host/scripts/gui_session.sh
source "$WBEAM_GUI_HELPER"
fi
TAURI_DIR="$ROOT_DIR/desktop/apps/desktop-tauri"
LOG_DIR="$ROOT_DIR/logs"
ensure_log_dir() {
mkdir -p "$LOG_DIR"
}
next_run_id() {
local day counter_file n
ensure_log_dir
day="$(date +%Y%m%d)"
counter_file="$LOG_DIR/.run.${day}.counter"
n=0
if [[ -f "$counter_file" ]]; then
n="$(tr -d '[:space:]' < "$counter_file" 2>/dev/null || echo 0)"
fi
if [[ ! "$n" =~ ^[0-9]+$ ]]; then
n=0
fi
n=$((n + 1))
printf '%s' "$n" > "$counter_file"
printf '%04d' "$n"
}
new_log_file() {
local domain ts run_id
domain="$1"
ts="$(date +%Y%m%d-%H%M%S)"
run_id="$(next_run_id)"
echo "$LOG_DIR/${ts}.${domain}.${run_id}.log"
}
usage() {
cat <<'EOF'
WBeam Desktop GUI launcher
Usage:
./desktop.sh
./desktop.sh --release
EOF
}
desktop_ensure_graphical_context() {
if declare -F wbeam_ensure_graphical_context >/dev/null 2>&1; then
wbeam_ensure_graphical_context \
"$ROOT_DIR" \
"desktop.sh" \
"desktop" \
"WBEAM_DESKTOP_AUTO_REEXEC" \
"WBEAM_DESKTOP_REEXEC" \
"$@"
return $?
fi
echo "[desktop] missing GUI helper: ${WBEAM_GUI_HELPER}" >&2
return 1
}
ensure_supported_node() {
if ! command -v node >/dev/null 2>&1; then
echo "[desktop] node is required (recommended v20 or v22 LTS)." >&2
return 1
fi
local major
major="$(node -p 'parseInt(process.versions.node.split(".")[0],10)' 2>/dev/null || echo 0)"
if [[ ! "$major" =~ ^[0-9]+$ ]]; then
echo "[desktop] unable to detect node version." >&2
return 1
fi
if (( major < 18 )); then
echo "[desktop] unsupported node version: $(node -v)" >&2
echo "[desktop] minimum supported is Node 18+, recommended 20.x or 22.x LTS." >&2
return 1
fi
if (( major > 22 )); then
echo "[desktop] warning: using newer Node version $(node -v); 20.x/22.x LTS is recommended." >&2
if [[ "${WBEAM_DESKTOP_STRICT_NODE:-0}" == "1" ]]; then
echo "[desktop] strict mode enabled (WBEAM_DESKTOP_STRICT_NODE=1), refusing to start." >&2
return 1
fi
fi
}
desktop_adb_preflight() {
if ! command -v adb >/dev/null 2>&1; then
echo "[desktop] warning: adb not found in PATH; Android device discovery will be unavailable." >&2
return 0
fi
if ! adb start-server >/dev/null 2>&1; then
echo "[desktop] warning: failed to start adb server; Android device discovery may be unavailable." >&2
return 0
fi
}
desktop_dev_port_pid() {
if command -v lsof >/dev/null 2>&1; then
lsof -tiTCP:1420 -sTCP:LISTEN 2>/dev/null | head -n1 || true
return 0
fi
if command -v ss >/dev/null 2>&1; then
ss -lptn 'sport = :1420' 2>/dev/null \
| sed -n 's/.*pid=\([0-9][0-9]*\).*/\1/p' \
| head -n1 || true
return 0
fi
echo ""
}
desktop_kill_stale_dev() {
local pid cmd
pid="$(desktop_dev_port_pid)"
if [[ -z "$pid" ]]; then
return 0
fi
cmd="$(ps -p "$pid" -o args= 2>/dev/null || true)"
if [[ "$cmd" =~ (desktop-tauri|wbeam-desktop-tauri|tauri\ dev|vite\ --port\ 1420) ]]; then
echo "[desktop] found stale desktop dev process on :1420 (pid=$pid), terminating..."
kill "$pid" 2>/dev/null || true
sleep 1
pid="$(desktop_dev_port_pid)"
if [[ -n "$pid" ]]; then
kill -9 "$pid" 2>/dev/null || true
sleep 1
fi
return 0
fi
echo "[desktop] port 1420 is in use by non-WBeam process (pid=$pid)." >&2
echo "[desktop] command: $cmd" >&2
echo "[desktop] free port 1420 and retry." >&2
return 1
}
desktop_kill_stale_tauri_app() {
local pids pid cmd remaining
pids="$(
ps -eo pid=,args= 2>/dev/null \
| awk '/wbeam-desktop-tauri/ && $0 !~ /awk/ {print $1}'
)"
if [[ -z "${pids//[[:space:]]/}" ]]; then
return 0
fi
echo "[desktop] found stale desktop tauri process(es): $(echo "$pids" | tr '\n' ' ' | sed 's/[[:space:]]*$//')"
while read -r pid; do
[[ -n "${pid:-}" ]] || continue
cmd="$(ps -p "$pid" -o args= 2>/dev/null || true)"
[[ "$cmd" == *"wbeam-desktop-tauri"* ]] || continue
kill "$pid" 2>/dev/null || true
done <<< "$pids"
sleep 1
remaining="$(
ps -eo pid=,args= 2>/dev/null \
| awk '/wbeam-desktop-tauri/ && $0 !~ /awk/ {print $1}'
)"
if [[ -n "${remaining//[[:space:]]/}" ]]; then
while read -r pid; do
[[ -n "${pid:-}" ]] || continue
cmd="$(ps -p "$pid" -o args= 2>/dev/null || true)"
[[ "$cmd" == *"wbeam-desktop-tauri"* ]] || continue
kill -9 "$pid" 2>/dev/null || true
done <<< "$remaining"
fi
}
desktop_check_daemon() {
local control_port daemon_name unit_path svc_active retries
control_port="${WBEAM_CONTROL_PORT:-5001}"
daemon_name="${WBEAM_DAEMON_SERVICE_NAME:-wbeam-daemon}"
if curl -fsS --max-time 2 "http://127.0.0.1:${control_port}/v1/health" >/dev/null 2>&1; then
echo "[desktop] daemon OK (port=${control_port})"
return 0
fi
if ! command -v systemctl >/dev/null 2>&1; then
echo "[desktop] warning: daemon unreachable at port ${control_port} and systemctl not available." >&2
echo "[desktop] start daemon manually: ./wbeam host run" >&2
return 0
fi
svc_active="$(systemctl --user is-active "$daemon_name" 2>/dev/null || true)"
if [[ "$svc_active" == "active" || "$svc_active" == "activating" ]]; then
echo "[desktop] daemon service ${svc_active}, waiting for API..."
retries=0
while (( retries < 8 )); do
sleep 0.5
if curl -fsS --max-time 1 "http://127.0.0.1:${control_port}/v1/health" >/dev/null 2>&1; then
echo "[desktop] daemon API ready"
return 0
fi
retries=$(( retries + 1 ))
done
echo "[desktop] daemon service running but API not yet reachable; proceeding..." >&2
return 0
fi
unit_path="$HOME/.config/systemd/user/${daemon_name}.service"
if [[ ! -f "$unit_path" ]]; then
echo "[desktop] warning: daemon service not installed at ${unit_path}." >&2
echo "[desktop] install the service first, then start it." >&2
return 0
fi
echo "[desktop] daemon service not running; attempting start..."
if systemctl --user start "$daemon_name" 2>/dev/null; then
retries=0
while (( retries < 10 )); do
sleep 0.5
if curl -fsS --max-time 1 "http://127.0.0.1:${control_port}/v1/health" >/dev/null 2>&1; then
echo "[desktop] daemon started OK"
return 0
fi
retries=$(( retries + 1 ))
done
echo "[desktop] daemon started but API not reachable yet; proceeding..." >&2
else
echo "[desktop] warning: failed to start daemon service; run ./wbeam host debug for details." >&2
fi
return 0
}
desktop_apply_tauri_stability_env() {
if declare -F wbeam_apply_tauri_stability_env >/dev/null 2>&1; then
wbeam_apply_tauri_stability_env "desktop"
return 0
fi
echo "[desktop] missing GUI helper: ${WBEAM_GUI_HELPER}" >&2
return 1
}
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
usage
exit 0
fi
desktop_ensure_graphical_context "$@"
run_dev() {
local log_file
ensure_supported_node
desktop_adb_preflight
desktop_check_daemon
desktop_kill_stale_tauri_app
desktop_kill_stale_dev
log_file="$(new_log_file "desktop")"
echo "[desktop] log=$log_file"
(
cd "$TAURI_DIR"
if [[ ! -d node_modules ]]; then
npm install
fi
desktop_apply_tauri_stability_env
exec npm run tauri dev
) 2>&1 | tee -a "$log_file"
}
run_release() {
local log_file
ensure_supported_node
desktop_adb_preflight
desktop_check_daemon
desktop_kill_stale_tauri_app
log_file="$(new_log_file "desktop")"
echo "[desktop] log=$log_file"
(
cd "$TAURI_DIR"
if [[ ! -d node_modules ]]; then
npm install
fi
npm run build
desktop_apply_tauri_stability_env
exec cargo run --release --manifest-path src-tauri/Cargo.toml
) 2>&1 | tee -a "$log_file"
}
case "${1:-}" in
""|--dev)
run_dev
;;
--release)
run_release
;;
*)
echo "[desktop] unknown arg: $1" >&2
usage >&2
exit 1
;;
esac