-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwbgui
More file actions
executable file
·429 lines (380 loc) · 13.5 KB
/
wbgui
File metadata and controls
executable file
·429 lines (380 loc) · 13.5 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/env bash
# wbgui — interactive TUI menu for WBeam
# Navigation: arrow keys or j/k. Enter = select. q/Esc = quit.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WBEAM="$SCRIPT_DIR/wbeam"
if [[ ! -x "$WBEAM" ]]; then
echo "wbeam not found or not executable: $WBEAM" >&2
exit 1
fi
MENU_ENTRIES=()
selectable=()
n=0
sel_pos=0
ANDROID_SERIALS=()
ANDROID_APIS=()
ANDROID_LABELS=()
collect_android_devices() {
ANDROID_SERIALS=()
ANDROID_APIS=()
ANDROID_LABELS=()
if ! command -v adb >/dev/null 2>&1; then
return 1
fi
adb start-server >/dev/null 2>&1 || true
# -l gives transport/product/model fields; tr -d '\r' strips CR from ADB on
# USB firmwares that return CRLF — without it state=="device\r" never matches.
while IFS= read -r line; do
# strip trailing CR
line="${line%$'\r'}"
[[ -z "$line" ]] && continue
[[ "$line" == "List of devices attached" ]] && continue
[[ "$line" == "* daemon"* ]] && continue
# first field = serial, second = state; rest are key:value from -l
serial="$(awk '{print $1}' <<<"$line")"
state="$(awk '{print $2}' <<<"$line")"
[[ -z "$serial" || "$state" != "device" ]] && continue
# Use || true on every getprop call — old devices (API 17) may return
# non-zero exit codes which would abort the loop under set -euo pipefail.
api="$(adb -s "$serial" shell getprop ro.build.version.sdk 2>/dev/null | tr -d '\r[:space:]' || true)"
release="$(adb -s "$serial" shell getprop ro.build.version.release 2>/dev/null | tr -d '\r[:space:]' || true)"
model="$(adb -s "$serial" shell getprop ro.product.model 2>/dev/null | tr -d '\r\n' || true)"
model="${model:-unknown-model}"
api="${api:-?}"
release="${release:-?}"
ANDROID_SERIALS+=("$serial")
ANDROID_APIS+=("$api")
ANDROID_LABELS+=("$model android $release api $api $serial")
done < <(adb devices -l 2>/dev/null || true)
[[ ${#ANDROID_SERIALS[@]} -gt 0 ]]
}
auto_pipeline_for_api() {
local api="$1"
# modern pipeline builds with minSdk=19, so API 18 must stay on legacy
if [[ "$api" =~ ^[0-9]+$ ]] && (( api <= 18 )); then
echo "legacy"
else
echo "modern"
fi
}
auto_pipeline_for_all_devices() {
local min_api=999
local api
for api in "${ANDROID_APIS[@]}"; do
if [[ "$api" =~ ^[0-9]+$ ]] && (( api < min_api )); then
min_api="$api"
fi
done
auto_pipeline_for_api "$min_api"
}
pick_android_device() {
local n i
n=${#ANDROID_SERIALS[@]}
while true; do
tput clear >/dev/tty
printf '\033[1;36m WBeam: Android deploy target\033[0m\n' >/dev/tty
printf '\033[90m wybierz urzadzenie (numer), a=all, q=anuluj\033[0m\n\n' >/dev/tty
for (( i=0; i<n; i++ )); do
printf ' %d) %s\n' "$((i + 1))" "${ANDROID_LABELS[$i]}" >/dev/tty
done
printf '\n a) all devices (adb default)\n q) anuluj\n\n> ' >/dev/tty
choice=""
IFS= read -r choice </dev/tty
case "$choice" in
q|Q)
return 1
;;
a|A|"")
SELECTED_ANDROID_SERIAL=""
SELECTED_ANDROID_API=""
return 0
;;
*)
if [[ "$choice" =~ ^[0-9]+$ ]] && (( choice >= 1 && choice <= n )); then
idx=$((choice - 1))
SELECTED_ANDROID_SERIAL="${ANDROID_SERIALS[$idx]}"
SELECTED_ANDROID_API="${ANDROID_APIS[$idx]}"
return 0
fi
;;
esac
done
}
pick_android_pipeline() {
local auto_mode="$1"
while true; do
tput clear >/dev/tty
printf '\033[1;36m WBeam: Android pipeline\033[0m\n' >/dev/tty
printf '\033[90m wybierz pipeline buildu\033[0m\n\n' >/dev/tty
printf ' 1) auto (api-based, default: %s)\n' "$auto_mode" >/dev/tty
printf ' 2) modern (minSdk 19)\n' >/dev/tty
printf ' 3) legacy (minSdk 17)\n' >/dev/tty
printf ' q) anuluj\n\n> ' >/dev/tty
choice=""
IFS= read -r choice </dev/tty
case "$choice" in
q|Q)
return 1
;;
1|"")
SELECTED_ANDROID_PIPELINE="$auto_mode"
return 0
;;
2)
SELECTED_ANDROID_PIPELINE="modern"
return 0
;;
3)
SELECTED_ANDROID_PIPELINE="legacy"
return 0
;;
esac
done
}
menu_rebuild_selectable() {
n=${#MENU_ENTRIES[@]}
selectable=()
local i
for (( i=0; i<n; i++ )); do
[[ "${MENU_ENTRIES[$i]}" == ──* ]] || selectable+=("$i")
done
}
menu_device_letter() {
local idx="$1"
if (( idx < 26 )); then
printf "\\$(printf '%03o' $((65 + idx)))"
else
printf "%d" "$((idx + 1))"
fi
}
build_menu_entries() {
MENU_ENTRIES=(
"──── Android ────────────────────────────────────────"
" Deploy (build + install + launch):android:deploy"
)
if collect_android_devices; then
local i letter label
for (( i=0; i<${#ANDROID_SERIALS[@]}; i++ )); do
letter="$(menu_device_letter "$i")"
label="${ANDROID_LABELS[$i]}"
MENU_ENTRIES+=(" Deploy Device ${letter} (${label}):android:deploy-device|${ANDROID_SERIALS[$i]}|${ANDROID_APIS[$i]}")
done
fi
MENU_ENTRIES+=(
" Build APK:android:build"
" Install APK:android:install"
" Launch App:android:launch"
" Build APK (release):android:build-release"
" Install APK (release):android:install-release"
" Deploy APK (release):android:deploy-release"
" Rebuild All (host + legacy + modern):android:build-all"
"──── Host ───────────────────────────────────────────────"
" Run Host (release, foreground):host:run"
" Run Host (debug + coloured logs):host:debug"
" Host Status:host:status"
"──── Network ────────────────────────────────────────"
" Transport Up (adb reverse / RNDIS auto-detect):ip:up"
" Transport Down (remove adb reverse):ip:down"
" Transport Status:ip:status"
"──────────────────────────────────────────────────────"
" Quit:__quit__:__quit__"
)
menu_rebuild_selectable
}
build_menu_entries
# ── terminal setup ────────────────────────────────────────────────────────────
if [[ ! -t 1 ]]; then
echo "wbgui requires an interactive terminal" >&2
exit 1
fi
saved_stty="$(stty -g </dev/tty 2>/dev/null || true)"
tty_restored=0
cleanup() {
if [[ "$tty_restored" -eq 1 ]]; then
return
fi
tty_restored=1
# Restore terminal unconditionally
tput rmcup >/dev/tty 2>/dev/null || true
tput cnorm >/dev/tty 2>/dev/null || true
if [[ -n "$saved_stty" ]]; then
stty "$saved_stty" </dev/tty 2>/dev/null || stty sane </dev/tty 2>/dev/null || true
else
stty sane </dev/tty 2>/dev/null || true
fi
}
trap cleanup EXIT INT TERM HUP
tput smcup >/dev/tty
tput civis >/dev/tty
stty -echo cbreak </dev/tty
# ── render ────────────────────────────────────────────────────────────────────
render() {
tput clear >/dev/tty
printf '\033[1;36m WBeam\033[0m\n' >/dev/tty
printf '\033[90m \xe2\x86\x91/\xe2\x86\x93 j/k navigate Enter select q quit\033[0m\n\n' >/dev/tty
local cur="${selectable[$sel_pos]}"
for (( i=0; i<n; i++ )); do
local e="${MENU_ENTRIES[$i]}"
if [[ "$e" == ──* ]]; then
printf '\033[90m%s\033[0m\n' "$e" >/dev/tty
elif [[ "$i" == "$cur" ]]; then
local lbl="${e%%:*}"
printf '\033[1;33m\xe2\x96\xb6%s\033[0m\n' "${lbl:1}" >/dev/tty
else
printf '\033[37m%s\033[0m\n' "${e%%:*}" >/dev/tty
fi
done
}
# ── main loop ─────────────────────────────────────────────────────────────────
selected_action=""
while true; do
render
# Read one key directly from /dev/tty (never a subshell).
# For escape-based keys (arrows/home/end/etc), read the full sequence.
key=""
IFS= read -r -s -n1 key </dev/tty
if [[ "$key" == $'\x1b' ]]; then
# Slurp any immediately following bytes that belong to this escape sequence.
# This avoids treating a slow arrow sequence as bare ESC.
ch=""
while IFS= read -r -s -n1 -t 0.05 ch </dev/tty; do
key+="$ch"
done
fi
case "$key" in
# ── movement ──────────────────────────────────────────────────────────────
$'\x1b[A'|$'\x1bOA'|$'\x1b['*A) # Up arrow
if (( sel_pos > 0 )); then
sel_pos=$((sel_pos - 1))
fi
;;
$'\x1b[B'|$'\x1bOB'|$'\x1b['*B) # Down arrow
if (( sel_pos < ${#selectable[@]} - 1 )); then
sel_pos=$((sel_pos + 1))
fi
;;
k|K)
if (( sel_pos > 0 )); then
sel_pos=$((sel_pos - 1))
fi
;;
j|J)
if (( sel_pos < ${#selectable[@]} - 1 )); then
sel_pos=$((sel_pos + 1))
fi
;;
# ── select ────────────────────────────────────────────────────────────────
$'\n'|$'\r'|'')
ci="${selectable[$sel_pos]}"
ce="${MENU_ENTRIES[$ci]}"
rest="${ce#*:}" # strip label
grp="${rest%%:*}" # first field after label = group
cmd_="${rest#*:}" # everything after group = cmd
selected_action="${grp}:${cmd_}"
break
;;
# ── quit ──────────────────────────────────────────────────────────────────
$'\x03'|q|Q)
selected_action="quit"
break
;;
esac
done
# cleanup runs via trap
trap - EXIT INT TERM HUP
cleanup
# ── dispatch ──────────────────────────────────────────────────────────────────
case "$selected_action" in
quit|__quit__:__quit__)
exit 0
;;
*:*)
grp="${selected_action%%:*}"
cmd_="${selected_action#*:}"
echo ""
if [[ "$grp" == "android" ]]; then
SELECTED_ANDROID_SERIAL=""
SELECTED_ANDROID_API=""
SELECTED_ANDROID_PIPELINE="modern"
PRESET_ANDROID_SERIAL=""
PRESET_ANDROID_API=""
if [[ "$cmd_" == deploy-device\|* ]]; then
IFS='|' read -r _ PRESET_ANDROID_SERIAL PRESET_ANDROID_API <<<"$cmd_"
cmd_="deploy"
fi
if [[ -n "$PRESET_ANDROID_SERIAL" ]]; then
SELECTED_ANDROID_SERIAL="$PRESET_ANDROID_SERIAL"
SELECTED_ANDROID_API="$PRESET_ANDROID_API"
SELECTED_ANDROID_PIPELINE="$(auto_pipeline_for_api "$SELECTED_ANDROID_API")"
elif [[ "$cmd_" == "build" || "$cmd_" == "build-release" ]]; then
if ! pick_android_pipeline "modern"; then
exit 0
fi
elif [[ "$cmd_" == "build-all" ]]; then
# no prompts — always builds both pipelines
true
else
if collect_android_devices; then
if ! pick_android_device; then
exit 0
fi
if [[ -n "$SELECTED_ANDROID_API" ]]; then
auto_mode="$(auto_pipeline_for_api "$SELECTED_ANDROID_API")"
else
auto_mode="$(auto_pipeline_for_all_devices)"
fi
if ! pick_android_pipeline "$auto_mode"; then
exit 0
fi
else
if ! pick_android_pipeline "modern"; then
exit 0
fi
fi
fi
# ── build-all: build legacy + modern without any device/pipeline prompt ─
if [[ "$cmd_" == "build-all" ]]; then
echo "[wbgui] === Build All: host backend (Rust release) ==="
"$WBEAM" host build
rc_host=$?
echo "[wbgui] === Build All: Android legacy (minSdk=17) ==="
WBEAM_ANDROID_PIPELINE="legacy" "$WBEAM" android build
rc_legacy=$?
echo "[wbgui] === Build All: Android modern (minSdk=19) ==="
WBEAM_ANDROID_PIPELINE="modern" "$WBEAM" android build
rc_modern=$?
if (( rc_host != 0 || rc_legacy != 0 || rc_modern != 0 )); then
echo "[wbgui] build-all FAILED (host=$rc_host legacy=$rc_legacy modern=$rc_modern)" >&2
exit 1
fi
echo "[wbgui] build-all OK"
exit 0
fi
ALLOW_NO_TRANSPORT="0"
API_ONLY_FALLBACK="0"
if [[ "$cmd_" == "deploy" || "$cmd_" == "deploy-release" ]]; then
ALLOW_NO_TRANSPORT="1"
API_ONLY_FALLBACK="1"
fi
if [[ -n "$SELECTED_ANDROID_SERIAL" ]]; then
WBEAM_ANDROID_SERIAL="$SELECTED_ANDROID_SERIAL" \
WBEAM_ANDROID_PIPELINE="$SELECTED_ANDROID_PIPELINE" \
WBEAM_ANDROID_ALLOW_NO_TRANSPORT="$ALLOW_NO_TRANSPORT" \
WBEAM_ANDROID_API_ONLY_FALLBACK="$API_ONLY_FALLBACK" \
"$WBEAM" "$grp" "$cmd_"
else
WBEAM_ANDROID_PIPELINE="$SELECTED_ANDROID_PIPELINE" \
WBEAM_ANDROID_ALLOW_NO_TRANSPORT="$ALLOW_NO_TRANSPORT" \
WBEAM_ANDROID_API_ONLY_FALLBACK="$API_ONLY_FALLBACK" \
"$WBEAM" "$grp" "$cmd_"
fi
exit $?
fi
"$WBEAM" "$grp" "$cmd_"
exit $?
;;
*)
exit 0
;;
esac