-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevtool
More file actions
executable file
·518 lines (470 loc) · 13.6 KB
/
devtool
File metadata and controls
executable file
·518 lines (470 loc) · 13.6 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#!/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"
if [[ -f "$WBEAM_CONFIG_HELPER" ]]; then
# shellcheck source=host/scripts/wbeam_config.sh
source "$WBEAM_CONFIG_HELPER"
wbeam_load_config "$ROOT_DIR"
fi
TARGET_DIR="$ROOT_DIR/.build/devtool"
TAURI_DIR="$ROOT_DIR/desktop/apps/desktop-tauri"
TAURI_MANIFEST="$TAURI_DIR/src-tauri/Cargo.toml"
HOST_WORKSPACE_DIR="$ROOT_DIR/host/rust"
CONTROL_PORT="${WBEAM_CONTROL_PORT:-5001}"
STREAM_PORT="${WBEAM_STREAM_PORT:-5000}"
BUILD_VERSION_FILE="$ROOT_DIR/.wbeam_build_version"
VERSION_BASE="${WBEAM_VERSION_BASE:-0.1.2}"
mkdir -p "$TARGET_DIR"
print_help() {
cat <<'EOF'
WBeam Devtool
Usage:
./devtool # desktop GUI (tauri)
./devtool gui # desktop GUI (tauri)
./devtool deps install # install host/build/runtime dependencies (Arch)
./devtool build # build host + desktop + android
./devtool deploy # deploy android app to all adb devices
./devtool host build # build host daemon binaries
Compatibility aliases:
./devtool project build
./devtool project deploy
./devtool android build
./devtool android deploy
./devtool android deploy-all
./devtool ip up|down|status # adb reverse tunnel helper
EOF
}
log() {
printf '[devtool] %s\n' "$*"
}
normalize_build_version() {
local raw="${1:-}"
if [[ -z "$raw" ]]; then
return 1
fi
if [[ "$raw" =~ ^([0-9]+\.[0-9]+\.[0-9]+)\.[0-9]+\.([A-Za-z0-9._-]+)$ ]]; then
echo "${BASH_REMATCH[1]}.${BASH_REMATCH[2]}"
elif [[ "$raw" =~ ^[0-9]+\.[0-9]+\.[0-9]+(\.[A-Za-z0-9._-]+)?$ ]]; then
echo "$raw"
else
echo "${VERSION_BASE}.${raw}"
fi
}
git_short_rev() {
local hash
hash="$(git -C "$ROOT_DIR" rev-parse --short=5 HEAD 2>/dev/null || true)"
if [[ "$hash" =~ ^[A-Fa-f0-9]{5}$ ]]; then
echo "$hash"
else
echo "dev00"
fi
}
build_version_generate() {
echo "${VERSION_BASE}.$(git_short_rev)"
}
build_version_write_file() {
local version="$1"
printf '%s\n' "$version" > "$BUILD_VERSION_FILE"
}
build_version_for_build() {
local explicit="${WBEAM_BUILD_REV:-}"
local version
if [[ -n "$explicit" ]]; then
version="$(normalize_build_version "$explicit")"
else
version="$(build_version_generate)"
fi
build_version_write_file "$version"
echo "$version"
}
run_gui() {
exec "$ROOT_DIR/desktop.sh" "$@"
}
as_root() {
if [[ "$EUID" -eq 0 ]]; then
"$@"
else
sudo "$@"
fi
}
run_deps_install() {
local sdk_root="${SDK_ROOT:-/opt/android-sdk}"
if [[ ! -f /etc/arch-release ]]; then
echo "[deps] unsupported OS: this command currently supports Arch Linux only." >&2
return 1
fi
local need_pacman=(
rust gcc protobuf cmake pkgconf python python-dbus python-gobject
ffmpeg gstreamer gst-plugins-base gst-plugins-good gst-plugins-bad gst-plugins-ugly
gst-plugin-pipewire openh264 grim kscreen android-tools android-sdk
android-sdk-cmdline-tools-latest dotnet-runtime-8.0 dotnet-sdk-8.0 jdk17-openjdk
)
local missing_pkgs=()
local p
for p in "${need_pacman[@]}"; do
if ! pacman -Q "$p" >/dev/null 2>&1; then
missing_pkgs+=("$p")
fi
done
if [[ "${#missing_pkgs[@]}" -gt 0 ]]; then
echo "[deps] installing missing pacman packages: ${missing_pkgs[*]}"
as_root pacman -S --needed --noconfirm "${missing_pkgs[@]}"
else
echo "[deps] pacman packages already installed."
fi
local sdkmanager="${sdk_root}/cmdline-tools/latest/bin/sdkmanager"
if [[ ! -x "$sdkmanager" ]]; then
echo "[deps] sdkmanager not found at $sdkmanager" >&2
return 1
fi
local missing_sdk=()
[[ -x "${sdk_root}/platform-tools/adb" ]] || missing_sdk+=("platform-tools")
[[ -x "${sdk_root}/emulator/emulator" ]] || missing_sdk+=("emulator")
[[ -d "${sdk_root}/platforms/android-34" ]] || missing_sdk+=("platforms;android-34")
[[ -d "${sdk_root}/build-tools/30.0.3" ]] || missing_sdk+=("build-tools;30.0.3")
[[ -d "${sdk_root}/ndk/23.1.7779620" ]] || missing_sdk+=("ndk;23.1.7779620")
[[ -d "${sdk_root}/cmake/3.22.1" ]] || missing_sdk+=("cmake;3.22.1")
if [[ "${#missing_sdk[@]}" -gt 0 ]]; then
echo "[deps] installing missing Android SDK components into ${sdk_root}: ${missing_sdk[*]}"
as_root bash -lc "yes | \"$sdkmanager\" --sdk_root=\"$sdk_root\" ${missing_sdk[*]@Q}"
else
echo "[deps] Android SDK components already installed."
fi
local fail=0
local cmd
for cmd in cargo rustc protoc adb java dotnet python3 ffmpeg gst-inspect-1.0; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "[deps] missing command in PATH: $cmd"
fail=1
fi
done
local el
for el in pipewiresrc openh264enc h264parse videoconvert videoscale videorate appsink jpegenc; do
if ! gst-inspect-1.0 "$el" >/dev/null 2>&1; then
echo "[deps] missing gstreamer element: $el"
fail=1
fi
done
if ! dotnet --list-runtimes 2>/dev/null | grep -q '^Microsoft\.NETCore\.App 8\.'; then
echo "[deps] missing .NET runtime 8.x"
fail=1
fi
if [[ "$fail" -ne 0 ]]; then
echo "[deps] dependency install finished with missing items." >&2
return 1
fi
echo "[deps] OK: dependencies for main + archived experimental lanes are installed."
}
run_host_build() {
local build_version="${1:-}"
if [[ -z "$build_version" ]]; then
build_version="$(build_version_for_build)"
fi
log "build version: $build_version"
log "build host runtime"
WBEAM_BUILD_REV="$build_version" cargo build --release -p wbeamd-server -p wbeamd-streamer --manifest-path "$HOST_WORKSPACE_DIR/Cargo.toml"
}
run_desktop_build() {
log "build desktop-tauri frontend + backend"
(
cd "$TAURI_DIR"
if [[ ! -d node_modules ]]; then
npm install
fi
npm run build
)
cargo build --manifest-path "$TAURI_MANIFEST"
}
run_android_build() {
local min_sdk="$1"
local build_version="${2:-}"
# Use canonical WBEAM_ANDROID_* env names (matching ./wbeam); fall back to legacy
# WBEAM_HOST/WBEAM_API_HOST/WBEAM_STREAM_HOST for backwards compat.
local host="${WBEAM_ANDROID_HOST:-${WBEAM_HOST:-127.0.0.1}}"
local api_host="${WBEAM_ANDROID_API_HOST:-${WBEAM_API_HOST:-$host}}"
local stream_host="${WBEAM_ANDROID_STREAM_HOST:-${WBEAM_STREAM_HOST:-$host}}"
if [[ -z "$build_version" ]]; then
build_version="$(build_version_for_build)"
fi
log "build android debug (minSdk=$min_sdk)"
(
cd "$ROOT_DIR/android"
bash ./gradlew \
"-PWBEAM_MIN_SDK=$min_sdk" \
"-PWBEAM_BUILD_REV=$build_version" \
"-PWBEAM_HOST=$host" \
"-PWBEAM_API_HOST=$api_host" \
"-PWBEAM_STREAM_HOST=$stream_host" \
:app:assembleDebug
)
}
run_build_all() {
local build_version
build_version="$(build_version_for_build)"
run_host_build "$build_version"
run_desktop_build
run_android_build 17 "$build_version"
run_android_build 19 "$build_version"
log "build complete"
}
adb_connected_serials() {
adb devices | awk 'NR>1 && $2=="device" { print $1 }'
}
adb_api_level() {
local serial="$1"
local api
api="$(adb -s "$serial" shell getprop ro.build.version.sdk 2>/dev/null | tr -cd '0-9' || true)"
if [[ "$api" =~ ^[0-9]+$ ]]; then
echo "$api"
return 0
fi
return 1
}
required_min_sdk_for_device() {
local api="$1"
# modern pipeline requires minSdk=19, so API <=18 must use legacy — matches wbeam.
if (( api <= 18 )); then
echo "17"
else
echo "19"
fi
}
build_android_apk_variant() {
local min_sdk="$1"
local build_version="$2"
local out_apk="$TARGET_DIR/apk/app-debug-min${min_sdk}.apk"
mkdir -p "$TARGET_DIR/apk"
if [[ -f "$out_apk" ]]; then
echo "$out_apk"
return 0
fi
run_android_build "$min_sdk" "$build_version"
local built_apk="$ROOT_DIR/android/app/build/outputs/apk/debug/app-debug.apk"
if [[ ! -f "$built_apk" ]]; then
echo "APK not found after build: $built_apk" >&2
return 1
fi
cp "$built_apk" "$out_apk"
echo "$out_apk"
}
run_deploy_all() {
if ! command -v adb >/dev/null 2>&1; then
echo "adb not found on PATH." >&2
return 1
fi
local serial_filter=""
local skip_build=0
while [[ $# -gt 0 ]]; do
case "$1" in
--serial)
serial_filter="${2:-}"
if [[ -z "$serial_filter" ]]; then
echo "missing value for --serial" >&2
return 1
fi
shift 2
;;
--skip-build)
skip_build=1
shift
;;
*)
echo "unknown deploy option: $1" >&2
return 1
;;
esac
done
local build_version
build_version="$(build_version_for_build)"
mapfile -t serials < <(adb_connected_serials)
if [[ -n "$serial_filter" ]]; then
local filtered=()
local s
for s in "${serials[@]}"; do
if [[ "$s" == "$serial_filter" ]]; then
filtered+=("$s")
fi
done
serials=("${filtered[@]}")
fi
if [[ "${#serials[@]}" -eq 0 ]]; then
echo "no connected adb devices in 'device' state" >&2
return 1
fi
declare -A apk_by_min_sdk=()
local serial
for serial in "${serials[@]}"; do
local api min_sdk apk
if ! api="$(adb_api_level "$serial")"; then
echo "[$serial] cannot resolve API level" >&2
return 1
fi
min_sdk="$(required_min_sdk_for_device "$api")"
if [[ "$skip_build" -eq 1 ]]; then
apk="$ROOT_DIR/android/app/build/outputs/apk/debug/app-debug.apk"
if [[ ! -f "$apk" ]]; then
echo "APK not found: $apk (skip-build mode)." >&2
return 1
fi
else
if [[ -z "${apk_by_min_sdk[$min_sdk]:-}" ]]; then
apk_by_min_sdk[$min_sdk]="$(build_android_apk_variant "$min_sdk" "$build_version")"
fi
apk="${apk_by_min_sdk[$min_sdk]}"
fi
if (( api <= 20 )) && [[ "${WBEAM_STREAM_HOST:-127.0.0.1}" == "127.0.0.1" ]]; then
log "[$serial] api=$api uses legacy transport mode; loopback host may require tether/LAN host override (WBEAM_STREAM_HOST/WBEAM_API_HOST)."
fi
log "[$serial] api=$api minSdk=$min_sdk install + reverse + launch"
adb -s "$serial" install -r "$apk"
adb -s "$serial" reverse "tcp:$STREAM_PORT" "tcp:$STREAM_PORT" || true
adb -s "$serial" reverse "tcp:$CONTROL_PORT" "tcp:$CONTROL_PORT" || true
adb -s "$serial" shell am start -n "com.wbeam/.MainActivity"
done
log "deploy complete for ${#serials[@]} device(s)"
}
run_ip_up() {
local serial_filter="${1:-}"
if ! command -v adb >/dev/null 2>&1; then
echo "adb not found on PATH." >&2
return 1
fi
mapfile -t serials < <(adb_connected_serials)
if [[ -n "$serial_filter" ]]; then
local filtered=()
local s
for s in "${serials[@]}"; do
[[ "$s" == "$serial_filter" ]] && filtered+=("$s")
done
serials=("${filtered[@]}")
fi
if [[ "${#serials[@]}" -eq 0 ]]; then
echo "no connected adb devices in 'device' state" >&2
return 1
fi
local serial
for serial in "${serials[@]}"; do
log "[$serial] adb reverse tcp:$STREAM_PORT tcp:$STREAM_PORT"
adb -s "$serial" reverse "tcp:$STREAM_PORT" "tcp:$STREAM_PORT"
log "[$serial] adb reverse tcp:$CONTROL_PORT tcp:$CONTROL_PORT"
adb -s "$serial" reverse "tcp:$CONTROL_PORT" "tcp:$CONTROL_PORT"
done
}
run_ip_down() {
local serial_filter="${1:-}"
if ! command -v adb >/dev/null 2>&1; then
echo "adb not found on PATH." >&2
return 1
fi
mapfile -t serials < <(adb_connected_serials)
if [[ -n "$serial_filter" ]]; then
local filtered=()
local s
for s in "${serials[@]}"; do
[[ "$s" == "$serial_filter" ]] && filtered+=("$s")
done
serials=("${filtered[@]}")
fi
if [[ "${#serials[@]}" -eq 0 ]]; then
echo "no connected adb devices in 'device' state" >&2
return 1
fi
local serial
for serial in "${serials[@]}"; do
log "[$serial] adb reverse --remove tcp:$STREAM_PORT"
adb -s "$serial" reverse --remove "tcp:$STREAM_PORT" || true
log "[$serial] adb reverse --remove tcp:$CONTROL_PORT"
adb -s "$serial" reverse --remove "tcp:$CONTROL_PORT" || true
done
}
run_ip_status() {
local serial_filter="${1:-}"
if ! command -v adb >/dev/null 2>&1; then
echo "adb not found on PATH." >&2
return 1
fi
mapfile -t serials < <(adb_connected_serials)
if [[ -n "$serial_filter" ]]; then
local filtered=()
local s
for s in "${serials[@]}"; do
[[ "$s" == "$serial_filter" ]] && filtered+=("$s")
done
serials=("${filtered[@]}")
fi
if [[ "${#serials[@]}" -eq 0 ]]; then
echo "no connected adb devices in 'device' state" >&2
return 1
fi
local serial
for serial in "${serials[@]}"; do
echo "[$serial]"
adb -s "$serial" reverse --list | sed 's/^/ /'
done
}
if [[ $# -eq 0 ]]; then
run_gui
fi
case "${1:-}" in
gui)
shift
run_gui "$@"
;;
build)
shift
run_build_all "$@"
;;
deps)
case "${2:-}" in
install) run_deps_install ;;
*) print_help; exit 1 ;;
esac
;;
deploy|deploy-all)
shift
run_deploy_all "$@"
;;
host)
case "${2:-}" in
build) run_host_build ;;
*) print_help; exit 1 ;;
esac
;;
logs)
case "${2:-}" in
live) ./wbeam logs host ;;
*) print_help; exit 1 ;;
esac
;;
project)
case "${2:-}" in
build) run_build_all ;;
deploy) run_deploy_all ;;
*) print_help; exit 1 ;;
esac
;;
android)
case "${2:-}" in
build) run_build_all ;;
deploy|deploy-all) run_deploy_all ;;
*) print_help; exit 1 ;;
esac
;;
ip)
case "${2:-}" in
up) run_ip_up "${3:-}" ;;
down) run_ip_down "${3:-}" ;;
status) run_ip_status "${3:-}" ;;
*) print_help; exit 1 ;;
esac
;;
help|-h|--help)
print_help
;;
*)
print_help
exit 1
;;
esac