-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch.bash
More file actions
executable file
·2098 lines (1768 loc) · 62.8 KB
/
launch.bash
File metadata and controls
executable file
·2098 lines (1768 loc) · 62.8 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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DOCKER_DIR="${SCRIPT_DIR}/infrastructure/docker"
DOCKER_BUILD_SCRIPT="${DOCKER_DIR}/build.bash"
DOCKERFILE_RELATIVE="infrastructure/docker/Dockerfile"
COMMAND="${1:-up}"
if [[ "${#}" -gt 0 ]]; then
shift
fi
IMAGE_NAME="${IMAGE_NAME:-chromium-opencv-stream}"
CHROME_APP="${CHROME_APP:-}"
CHROME_PORT="${CHROME_PORT:-9222}"
CHROME_PROFILE_DIR_RAW="${CHROME_PROFILE_DIR:-}"
CHROME_PROFILE_DIR=""
CHROME_REMOTE_ALLOW_ORIGINS="${CHROME_REMOTE_ALLOW_ORIGINS:-*}"
CHROME_VERSION_URL="${CHROME_VERSION_URL:-http://127.0.0.1:${CHROME_PORT}/json/version}"
CHROME_LIST_URL="${CHROME_LIST_URL:-http://127.0.0.1:${CHROME_PORT}/json}"
CHROME_HOST_IN_CONTAINER_RAW="${CHROME_HOST_IN_CONTAINER:-}"
CHROME_HOST_IN_CONTAINER="${CHROME_HOST_IN_CONTAINER_RAW:-host.docker.internal}"
CHROME_STARTUP_RETRIES="${CHROME_STARTUP_RETRIES:-30}"
CHROME_STARTUP_DELAY_SECONDS="${CHROME_STARTUP_DELAY_SECONDS:-1}"
CHROME_HTTP_CONNECT_TIMEOUT_SECONDS="${CHROME_HTTP_CONNECT_TIMEOUT_SECONDS:-1}"
CHROME_HTTP_MAX_TIME_SECONDS="${CHROME_HTTP_MAX_TIME_SECONDS:-2}"
FORCE_REBUILD="${FORCE_REBUILD:-0}"
GAMES_DIR="${GAMES_DIR:-${SCRIPT_DIR}/games}"
APPS_DIR="${APPS_DIR:-${SCRIPT_DIR}/apps}"
APPS_DIR_IN_CONTAINER="${APPS_DIR_IN_CONTAINER:-/workspace/apps}"
APP_NAME="${APP_NAME:-}"
APP_DEFAULT_TARGET_URL="${APP_DEFAULT_TARGET_URL:-}"
TARGET_URL_OVERRIDE="${TARGET_URL_OVERRIDE:-}"
TARGET_URL_OVERRIDE_INPUT="${TARGET_URL_OVERRIDE}"
TARGET_MATCH="${TARGET_MATCH:-}"
TARGET_CLOSE_ACTION="${TARGET_CLOSE_ACTION:-exit}"
STARTUP_TARGET_MODE="${STARTUP_TARGET_MODE:-new-target}"
OUTPUT_DIR_RAW="${OUTPUT_DIR:-}"
SCREENSHOT_DIR_RAW="${SCREENSHOT_DIR:-}"
SAVE_INTERVAL_SECONDS="${SAVE_INTERVAL_SECONDS:-10}"
FRAME_FORMAT="${FRAME_FORMAT:-jpeg}"
FRAME_QUALITY="${FRAME_QUALITY:-70}"
EVERY_NTH_FRAME="${EVERY_NTH_FRAME:-1}"
MAX_WIDTH="${MAX_WIDTH:-1280}"
MAX_HEIGHT="${MAX_HEIGHT:-720}"
LIVE_PREVIEW="${LIVE_PREVIEW:-0}"
VIDEO_OUTPUT="${VIDEO_OUTPUT:-}"
METADATA_OUTPUT="${METADATA_OUTPUT:-}"
PROCESSORS="${PROCESSORS:-}"
RECONNECT_ATTEMPTS="${RECONNECT_ATTEMPTS:-10}"
RECONNECT_DELAY_SECONDS="${RECONNECT_DELAY_SECONDS:-2}"
RECEIVE_TIMEOUT_SECONDS="${RECEIVE_TIMEOUT_SECONDS:-5}"
IDLE_TIMEOUT_SECONDS="${IDLE_TIMEOUT_SECONDS:-20}"
LOG_INTERVAL_SECONDS="${LOG_INTERVAL_SECONDS:-1}"
VIDEO_FPS="${VIDEO_FPS:-12}"
MAX_FRAMES="${MAX_FRAMES:-0}"
ACTION_MODE="${ACTION_MODE:-auto}"
ACTION_DEFAULT_COOLDOWN_MS="${ACTION_DEFAULT_COOLDOWN_MS:-250}"
ACTION_MAX_PER_FRAME="${ACTION_MAX_PER_FRAME:-0}"
ACTION_DRAG_STEP_COUNT="${ACTION_DRAG_STEP_COUNT:-8}"
ACTION_DRAG_STEP_DELAY_MS="${ACTION_DRAG_STEP_DELAY_MS:-16}"
DOCKER_EXTRA_ARGS="${DOCKER_EXTRA_ARGS:-}"
DOCKER_RUN_USER_MODE="${DOCKER_RUN_USER_MODE:-auto}"
SMOKE_MAX_FRAMES="${SMOKE_MAX_FRAMES:-6}"
SMOKE_EXTERNAL_MAX_FRAMES="${SMOKE_EXTERNAL_MAX_FRAMES:-1}"
SMOKE_INTERACTION_MAX_FRAMES="${SMOKE_INTERACTION_MAX_FRAMES:-40}"
SMOKE_EXTERNAL_URL="${SMOKE_EXTERNAL_URL:-https://example.com}"
SMOKE_OUTPUT_DIR_RAW="${SMOKE_OUTPUT_DIR:-}"
GAME_SLUGS=()
APP_NAMES=()
SELECTED_APP_NAME=""
APP_INSPECTION_JSON=""
APP_DEFAULT_START_TARGET=""
APP_DEFAULT_START_FPS=""
RESOLVED_APP_DEFAULT_TARGET=""
SELECTED_TARGET_OVERRIDE=""
SELECTED_EFFECTIVE_TARGET=""
TARGET_SELECTION_MODE="app-default"
TARGET_SELECTION_LABEL="app default"
TARGET_SELECTION_VALUE=""
INTERRUPTED=0
DOCKER_TTY_ARGS=()
HOST_OUTPUT_DIR=""
HOST_SCREENSHOT_DIR=""
SCREENSHOT_SUBPATH="screenshots"
CONTAINER_OUTPUT_DIR="/data/output"
CONTAINER_SAVE_DIR=""
DOCKER_HOST_ARGS=()
WSL_CHROME_LAUNCH_BACKEND=""
WSL_CHROME_LAST_ERROR=""
CHROME_PROFILE_KIND="unresolved"
WSL_HOST_ROUTE_MODE="default"
POWERSHELL_LAST_ERROR=""
WSL_HOST_ROUTE_TRIED=""
WSL_WINDOWS_CHROME_PATH=""
HOST_DEVTOOLS_SOURCE_LABEL="${CHROME_VERSION_URL}"
HOST_BROWSER_WS_URL_OVERRIDE=""
HOST_DEVTOOLS_VERSION_JSON=""
stop_with_interrupt() {
if [[ "${INTERRUPTED}" != "1" ]]; then
INTERRUPTED=1
echo
echo "[info] Stopping."
fi
exit 130
}
trap stop_with_interrupt INT TERM
error_exit() {
echo "[error] $*" >&2
exit 1
}
print_usage() {
cat <<'EOF'
Usage:
./launch.bash [up]
./launch.bash chrome
./launch.bash doctor
./launch.bash smoke
./launch.bash container
./launch.bash help
Commands:
up Run the full launcher flow: build/reuse the image, choose a target and app, ensure Chrome, then run the container.
chrome Start or reuse Google Chrome with DevTools remote debugging enabled.
doctor Run prerequisite and connectivity checks for Docker, Chrome, and the DevTools endpoint.
smoke Run bounded non-interactive smoke checks against one local target, one external target, and one dry-run action flow.
container Run the Docker container directly using the current environment variables.
help Show this help text.
Examples:
./launch.bash
./launch.bash doctor
./launch.bash smoke
APP_NAME=screenshot_capture TARGET_URL_OVERRIDE=game://input-lab ./launch.bash
./launch.bash chrome
APP_NAME=screenshot_capture ./launch.bash container
./infrastructure/docker/build.bash
EOF
}
command_exists() {
command -v "$1" >/dev/null 2>&1
}
require_command() {
local command_name="$1"
local remediation="$2"
if command_exists "${command_name}"; then
return 0
fi
error_exit "${command_name} is required. ${remediation}"
}
trim_carriage_return() {
local value="$1"
value="${value%$'\r'}"
printf '%s\n' "${value}"
}
powershell_quote_literal() {
local value="$1"
value="${value//\'/\'\'}"
printf '%s\n' "${value}"
}
windows_path_to_file_url() {
local windows_path="$1"
local normalized
windows_path="$(trim_carriage_return "${windows_path}")"
if [[ -z "${windows_path}" ]]; then
printf '\n'
return 0
fi
if [[ "${windows_path}" == \\\\* ]]; then
normalized="${windows_path#\\\\}"
normalized="${normalized//\\//}"
printf 'file://%s\n' "${normalized}"
return 0
fi
normalized="${windows_path//\\//}"
printf 'file:///%s\n' "${normalized}"
}
normalize_target_url_for_wsl_windows_chrome() {
local raw_url="$1"
local wsl_path
local windows_path
if [[ -z "${raw_url}" ]]; then
printf '\n'
return 0
fi
if ! is_wsl || [[ "${WSL_CHROME_LAUNCH_BACKEND:-}" != "wsl-windows" ]]; then
printf '%s\n' "${raw_url}"
return 0
fi
if [[ "${raw_url}" != file://* ]]; then
printf '%s\n' "${raw_url}"
return 0
fi
wsl_path="${raw_url#file://}"
if [[ "${wsl_path}" != /* ]]; then
printf '%s\n' "${raw_url}"
return 0
fi
if ! command_exists "wslpath"; then
printf '%s\n' "${raw_url}"
return 0
fi
if ! windows_path="$(wslpath -w "${wsl_path}" 2>/dev/null)"; then
printf '%s\n' "${raw_url}"
return 0
fi
windows_path="$(trim_carriage_return "${windows_path}")"
if [[ -z "${windows_path}" ]]; then
printf '%s\n' "${raw_url}"
return 0
fi
windows_path_to_file_url "${windows_path}"
}
detect_hash_tool() {
if command_exists "sha256sum"; then
printf '%s\n' "sha256sum"
return 0
fi
if command_exists "shasum"; then
printf '%s\n' "shasum"
return 0
fi
return 1
}
ensure_hash_tool() {
if detect_hash_tool >/dev/null 2>&1; then
return 0
fi
error_exit "No SHA-256 tool is available. Install sha256sum or shasum before running WebVisionKit."
}
hash_file_sha256() {
local path="$1"
local tool
tool="$(detect_hash_tool)" || error_exit "No SHA-256 tool is available. Install sha256sum or shasum before running WebVisionKit."
if [[ "${tool}" == "sha256sum" ]]; then
sha256sum "${path}" | awk '{print $1}'
return 0
fi
shasum -a 256 "${path}" | awk '{print $1}'
}
json_extract_string_field() {
local json="$1"
local key="$2"
printf '%s\n' "${json}" | sed -nE "s/.*\"${key}\":[[:space:]]*\"(([^\"\\\\]|\\\\.)*)\".*/\\1/p" | head -n 1
}
json_extract_number_field() {
local json="$1"
local key="$2"
printf '%s\n' "${json}" | sed -nE "s/.*\"${key}\":[[:space:]]*([-0-9.]+).*/\\1/p" | head -n 1
}
is_wsl() {
[[ "$(uname -s)" == "Linux" ]] && { [[ -n "${WSL_DISTRO_NAME:-}" ]] || grep -qi microsoft /proc/version 2>/dev/null; }
}
is_wsl2() {
is_wsl && uname -r | grep -qi 'wsl2'
}
is_macos() {
[[ "$(uname -s)" == "Darwin" ]]
}
is_native_linux() {
[[ "$(uname -s)" == "Linux" ]] && ! is_wsl
}
powershell_is_operational() {
local output
if ! command_exists "powershell.exe"; then
POWERSHELL_LAST_ERROR="powershell.exe is not available on PATH."
return 1
fi
if ! output="$(powershell.exe -NoProfile -Command '[Console]::Out.Write("webvisionkit-ok")' 2>&1)"; then
POWERSHELL_LAST_ERROR="$(trim_carriage_return "${output}")"
return 1
fi
output="$(trim_carriage_return "${output}")"
if [[ "${output}" != *"webvisionkit-ok"* ]]; then
POWERSHELL_LAST_ERROR="PowerShell probe returned unexpected output: ${output}"
return 1
fi
POWERSHELL_LAST_ERROR=""
}
powershell_env_passthrough_works() {
local output
if ! output="$(
WEBVISIONKIT_ENV_CHECK="webvisionkit-env-ok" \
powershell.exe -NoProfile -Command '$value = $env:WEBVISIONKIT_ENV_CHECK; if ($null -eq $value) { [Console]::Out.Write("missing") } else { [Console]::Out.Write($value) }' \
2>&1
)"; then
POWERSHELL_LAST_ERROR="$(trim_carriage_return "${output}")"
return 1
fi
output="$(trim_carriage_return "${output}")"
if [[ "${output}" != "webvisionkit-env-ok" ]]; then
POWERSHELL_LAST_ERROR="PowerShell environment passthrough probe returned '${output}'."
return 1
fi
POWERSHELL_LAST_ERROR=""
}
resolve_windows_local_app_data_dir() {
local output
if ! output="$(powershell.exe -NoProfile -Command '[Environment]::GetFolderPath("LocalApplicationData")' 2>&1)"; then
POWERSHELL_LAST_ERROR="$(trim_carriage_return "${output}")"
return 1
fi
output="$(trim_carriage_return "${output}")"
if [[ -z "${output}" ]]; then
POWERSHELL_LAST_ERROR="PowerShell returned an empty LocalApplicationData path."
return 1
fi
printf '%s\n' "${output}"
}
fetch_windows_local_devtools_version_json_via_powershell() {
local output
if ! output="$(
powershell.exe -NoProfile -Command "\$uri = 'http://127.0.0.1:${CHROME_PORT}/json/version'; try { [Console]::Out.Write((Invoke-WebRequest -UseBasicParsing -Uri \$uri -TimeoutSec 2).Content) } catch { exit 1 }" \
2>&1
)"; then
POWERSHELL_LAST_ERROR="$(trim_carriage_return "${output}")"
return 1
fi
output="$(trim_carriage_return "${output}")"
if [[ -z "${output}" ]]; then
POWERSHELL_LAST_ERROR="PowerShell returned an empty DevTools version payload."
return 1
fi
printf '%s\n' "${output}"
}
fetch_devtools_version_json_from_url() {
local url="$1"
curl \
-fsSL \
--connect-timeout "${CHROME_HTTP_CONNECT_TIMEOUT_SECONDS}" \
--max-time "${CHROME_HTTP_MAX_TIME_SECONDS}" \
"${url}" \
2>/dev/null
}
fetch_host_devtools_version_json_into_state() {
local version_json
HOST_DEVTOOLS_VERSION_JSON=""
if is_wsl && [[ "${WSL_CHROME_LAUNCH_BACKEND:-}" == "wsl-windows" ]] && powershell_is_operational; then
if version_json="$(fetch_windows_local_devtools_version_json_via_powershell)"; then
HOST_DEVTOOLS_SOURCE_LABEL="windows-local-powershell://127.0.0.1:${CHROME_PORT}/json/version"
HOST_DEVTOOLS_VERSION_JSON="${version_json}"
return 0
fi
fi
if version_json="$(fetch_devtools_version_json_from_url "${CHROME_VERSION_URL}")"; then
HOST_DEVTOOLS_SOURCE_LABEL="${CHROME_VERSION_URL}"
HOST_DEVTOOLS_VERSION_JSON="${version_json}"
return 0
fi
return 1
}
fetch_host_devtools_version_json() {
if ! fetch_host_devtools_version_json_into_state; then
return 1
fi
printf '%s\n' "${HOST_DEVTOOLS_VERSION_JSON}"
}
resolve_default_chrome_profile_dir_for_backend() {
local backend="${1:-}"
if is_wsl && [[ "${backend}" == "wsl-windows" ]]; then
if ! powershell_is_operational; then
return 1
fi
local windows_local_app_data
if ! windows_local_app_data="$(resolve_windows_local_app_data_dir)"; then
return 1
fi
printf '%s\n' "${windows_local_app_data}\\WebVisionKit\\chrome-cdp-profile"
return 0
fi
printf '%s\n' "/tmp/webvisionkit-chrome-cdp-profile"
}
resolve_default_chrome_profile_dir() {
resolve_default_chrome_profile_dir_for_backend ""
}
ensure_chrome_profile_dir_resolved_for_backend() {
local backend="${1:-}"
if [[ -n "${CHROME_PROFILE_DIR}" ]]; then
return 0
fi
if [[ -n "${CHROME_PROFILE_DIR_RAW}" ]]; then
CHROME_PROFILE_DIR="${CHROME_PROFILE_DIR_RAW}"
CHROME_PROFILE_KIND="explicit-env"
return 0
fi
if ! CHROME_PROFILE_DIR="$(resolve_default_chrome_profile_dir_for_backend "${backend}")"; then
return 1
fi
if is_wsl && [[ "${backend}" == "wsl-windows" ]]; then
CHROME_PROFILE_KIND="wsl-windows-default"
else
CHROME_PROFILE_KIND="local-default"
fi
}
ensure_chrome_profile_dir_resolved() {
ensure_chrome_profile_dir_resolved_for_backend ""
}
compute_source_hash() {
(
cd "${SCRIPT_DIR}"
{
printf '%s\n' "${DOCKERFILE_RELATIVE}"
find "api/webvisionkit" -type f ! -path '*/__pycache__/*' | LC_ALL=C sort
} | while IFS= read -r path; do
hash_file_sha256 "${path}"
done | hash_file_sha256 "/dev/stdin"
) | hash_file_sha256 "/dev/stdin"
}
get_image_source_hash() {
docker image inspect "${IMAGE_NAME}" \
--format '{{ index .Config.Labels "webvisionkit.source-hash" }}' \
2>/dev/null || true
}
sort_into_array() {
local -a unsorted=("$@")
local value
if [[ "${#unsorted[@]}" -eq 0 ]]; then
return 0
fi
while IFS= read -r value; do
[[ -n "${value}" ]] && printf '%s\0' "${value}"
done < <(printf '%s\n' "${unsorted[@]}" | LC_ALL=C sort -u)
}
discover_game_slugs() {
local -a found=()
local path
shopt -s nullglob
for path in "${GAMES_DIR}"/*/index.html; do
found+=( "$(basename "$(dirname "${path}")")" )
done
shopt -u nullglob
GAME_SLUGS=()
if [[ "${#found[@]}" -eq 0 ]]; then
return 0
fi
while IFS= read -r -d '' path; do
GAME_SLUGS+=( "${path}" )
done < <(sort_into_array "${found[@]}")
}
discover_app_names() {
local -a found=()
local path
shopt -s nullglob
for path in "${APPS_DIR}"/*/app.py; do
found+=( "$(basename "$(dirname "${path}")")" )
done
shopt -u nullglob
APP_NAMES=()
if [[ "${#found[@]}" -eq 0 ]]; then
return 0
fi
while IFS= read -r -d '' path; do
APP_NAMES+=( "${path}" )
done < <(sort_into_array "${found[@]}")
}
array_contains() {
local needle="$1"
shift
local item
for item in "$@"; do
if [[ "${item}" == "${needle}" ]]; then
return 0
fi
done
return 1
}
require_interactive_input() {
if [[ ! -t 0 ]]; then
error_exit "Interactive selection requires a TTY. Set APP_NAME and optional TARGET_URL_OVERRIDE to run non-interactively."
fi
}
ensure_curl_access() {
require_command "curl" "Install curl so the launcher can query the Chrome DevTools endpoints."
}
ensure_wsl_prerequisites() {
if ! is_wsl; then
return 0
fi
if ! is_wsl2; then
error_exit "WSL1 is not supported. Use WSL2 with Docker Desktop integration for WebVisionKit."
fi
}
ensure_browser_launch_prerequisites() {
ensure_curl_access
ensure_wsl_prerequisites
case "$(resolve_platform)" in
wsl)
resolve_wsl_launch_backend >/dev/null
;;
macos)
ensure_chrome_profile_dir_resolved_for_backend "macos" >/dev/null
resolve_macos_chrome_app >/dev/null
;;
linux)
ensure_chrome_profile_dir_resolved_for_backend "linux" >/dev/null
resolve_linux_chrome_app >/dev/null
;;
esac
}
ensure_catalogs() {
discover_game_slugs
discover_app_names
if [[ "${#APP_NAMES[@]}" -eq 0 ]]; then
error_exit "No apps found in ${APPS_DIR}. Add subdirectories containing app.py."
fi
}
ensure_docker_access() {
require_command "docker" "Install Docker Desktop or a compatible Docker engine."
if docker info >/dev/null 2>&1; then
return 0
fi
error_exit "Docker is unavailable or inaccessible. Start Docker Desktop or fix Docker permissions, then retry."
}
ensure_image() {
local expected_hash
local image_hash
expected_hash="$(compute_source_hash)"
image_hash="$(get_image_source_hash)"
if [[ "${FORCE_REBUILD}" == "1" ]]; then
echo "[info] FORCE_REBUILD=1, rebuilding Docker image ${IMAGE_NAME}"
if ! IMAGE_NAME="${IMAGE_NAME}" bash "${DOCKER_BUILD_SCRIPT}"; then
error_exit "Docker image build failed for ${IMAGE_NAME}. Check Docker access and the build output above."
fi
return 0
fi
if [[ -z "${image_hash}" ]]; then
echo "[info] Docker image ${IMAGE_NAME} is missing or unversioned. Building it now."
if ! IMAGE_NAME="${IMAGE_NAME}" bash "${DOCKER_BUILD_SCRIPT}"; then
error_exit "Docker image build failed for ${IMAGE_NAME}. Check Docker access and the build output above."
fi
return 0
fi
if [[ "${image_hash}" != "${expected_hash}" ]]; then
echo "[info] Docker image ${IMAGE_NAME} is stale."
echo "[info] Expected source hash ${expected_hash}, found ${image_hash}."
echo "[info] Rebuilding Docker image ${IMAGE_NAME}."
if ! IMAGE_NAME="${IMAGE_NAME}" bash "${DOCKER_BUILD_SCRIPT}"; then
error_exit "Docker image build failed for ${IMAGE_NAME}. Check Docker access and the build output above."
fi
return 0
fi
echo "[info] Reusing Docker image ${IMAGE_NAME}"
}
resolve_game_url() {
local slug="$1"
if ! array_contains "${slug}" "${GAME_SLUGS[@]}"; then
error_exit "Unknown game slug ${slug}. Valid values: ${GAME_SLUGS[*]}"
fi
printf 'file://%s/%s/index.html\n' "${GAMES_DIR}" "${slug}"
}
resolve_target_value() {
local raw="$1"
if [[ -z "${raw}" ]]; then
printf '\n'
return 0
fi
if [[ "${raw}" == "free" ]]; then
printf 'about:blank\n'
return 0
fi
if [[ "${raw}" == game://* ]]; then
resolve_game_url "${raw#game://}"
return 0
fi
if array_contains "${raw}" "${GAME_SLUGS[@]}"; then
resolve_game_url "${raw}"
return 0
fi
printf '%s\n' "${raw}"
}
set_explicit_target_selection() {
local label="$1"
local raw_value="$2"
TARGET_SELECTION_MODE="explicit"
TARGET_SELECTION_LABEL="${label}"
TARGET_SELECTION_VALUE="$(resolve_target_value "${raw_value}")"
if [[ -z "${TARGET_SELECTION_VALUE}" ]]; then
error_exit "Target selection ${label} did not resolve to a usable URL."
fi
}
prompt_custom_url() {
local input
while true; do
read -r -p "Custom URL: " input
if [[ -z "${input}" ]]; then
echo "[warn] Enter a non-empty URL, about:blank, game://slug, or a game slug." >&2
continue
fi
set_explicit_target_selection "custom URL" "${input}"
return 0
done
}
choose_target_selection() {
local selection
local index
local custom_index
if [[ -n "${TARGET_URL_OVERRIDE_INPUT}" ]]; then
set_explicit_target_selection "${TARGET_URL_OVERRIDE_INPUT}" "${TARGET_URL_OVERRIDE_INPUT}"
return 0
fi
if [[ ! -t 0 ]]; then
TARGET_SELECTION_MODE="app-default"
TARGET_SELECTION_LABEL="app default"
TARGET_SELECTION_VALUE=""
return 0
fi
echo "[info] Choose what Chrome should open:"
echo " 0) app default"
echo " 1) free browse"
index=2
if [[ "${#GAME_SLUGS[@]}" -eq 0 ]]; then
echo "[info] No local games found in ${GAMES_DIR}. Only app default, free browse, and custom URL are available."
else
for selection in "${GAME_SLUGS[@]}"; do
echo " ${index}) ${selection}"
index=$((index + 1))
done
fi
custom_index="${index}"
echo " ${custom_index}) custom URL"
while true; do
read -r -p "Launch target: " selection
if [[ "${selection}" == "0" ]] || [[ "${selection}" == "default" ]]; then
TARGET_SELECTION_MODE="app-default"
TARGET_SELECTION_LABEL="app default"
TARGET_SELECTION_VALUE=""
return 0
fi
if [[ "${selection}" == "1" ]] || [[ "${selection}" == "free" ]]; then
set_explicit_target_selection "free browse" "about:blank"
return 0
fi
if [[ "${selection}" =~ ^[0-9]+$ ]]; then
if [[ "${selection}" == "${custom_index}" ]]; then
prompt_custom_url
return 0
fi
index=$((selection - 2))
if (( index >= 0 && index < ${#GAME_SLUGS[@]} )); then
set_explicit_target_selection "${GAME_SLUGS[index]}" "${GAME_SLUGS[index]}"
return 0
fi
elif [[ "${selection}" == "custom" ]]; then
prompt_custom_url
return 0
elif [[ "${selection}" == game://* ]] || array_contains "${selection}" "${GAME_SLUGS[@]}"; then
set_explicit_target_selection "${selection}" "${selection}"
return 0
elif [[ "${selection}" == "about:blank" ]] || [[ "${selection}" == http://* ]] || [[ "${selection}" == https://* ]] || [[ "${selection}" == file://* ]]; then
set_explicit_target_selection "${selection}" "${selection}"
return 0
fi
echo "[warn] Invalid selection. Choose app default, free browse, a game, or custom URL." >&2
done
}
choose_app() {
local selection
local index
if [[ -n "${APP_NAME}" ]]; then
if array_contains "${APP_NAME}" "${APP_NAMES[@]}"; then
SELECTED_APP_NAME="${APP_NAME}"
return 0
fi
error_exit "Invalid APP_NAME=${APP_NAME}. Valid values: ${APP_NAMES[*]}"
fi
require_interactive_input
echo "[info] Choose the student app to run:"
index=1
for selection in "${APP_NAMES[@]}"; do
echo " ${index}) ${selection}"
index=$((index + 1))
done
while true; do
read -r -p "App: " selection
if [[ "${selection}" =~ ^[0-9]+$ ]]; then
index=$((selection - 1))
if (( index >= 0 && index < ${#APP_NAMES[@]} )); then
SELECTED_APP_NAME="${APP_NAMES[index]}"
return 0
fi
elif array_contains "${selection}" "${APP_NAMES[@]}"; then
SELECTED_APP_NAME="${selection}"
return 0
fi
echo "[warn] Invalid selection. Choose one of: ${APP_NAMES[*]}" >&2
done
}
inspect_app() {
if ! APP_INSPECTION_JSON="$(
docker run --rm \
-v "${APPS_DIR}:${APPS_DIR_IN_CONTAINER}:ro" \
-e "APPS_DIR=${APPS_DIR_IN_CONTAINER}" \
-e "APP_NAME=${SELECTED_APP_NAME}" \
"${IMAGE_NAME}" \
python -m webvisionkit.runner --inspect-app
)"; then
error_exit "Failed to inspect app ${SELECTED_APP_NAME} inside Docker. Check Docker access and the app definition."
fi
if [[ -z "${APP_INSPECTION_JSON}" ]]; then
error_exit "Failed to inspect app ${SELECTED_APP_NAME} inside Docker."
fi
APP_DEFAULT_START_TARGET="$(json_extract_string_field "${APP_INSPECTION_JSON}" "start_target")"
APP_DEFAULT_START_FPS="$(json_extract_number_field "${APP_INSPECTION_JSON}" "fps")"
if [[ -z "${APP_DEFAULT_START_TARGET}" ]]; then
error_exit "Could not extract start_target from Docker app inspection output."
fi
if [[ -z "${APP_DEFAULT_START_FPS}" ]]; then
error_exit "Could not extract fps from Docker app inspection output."
fi
}
resolve_effective_target() {
RESOLVED_APP_DEFAULT_TARGET="$(resolve_target_value "${APP_DEFAULT_START_TARGET}")"
if [[ -z "${RESOLVED_APP_DEFAULT_TARGET}" ]]; then
error_exit "App ${SELECTED_APP_NAME} did not provide a usable default start target."
fi
if [[ "${TARGET_SELECTION_MODE}" == "app-default" ]]; then
SELECTED_TARGET_OVERRIDE=""
SELECTED_EFFECTIVE_TARGET="${RESOLVED_APP_DEFAULT_TARGET}"
return 0
fi
SELECTED_TARGET_OVERRIDE="${TARGET_SELECTION_VALUE}"
SELECTED_EFFECTIVE_TARGET="${TARGET_SELECTION_VALUE}"
}
ensure_profile_dir() {
local path="$1"
local ps_output
local escaped_path
if [[ -z "${path}" ]]; then
echo "[error] Chrome profile directory path is empty." >&2
return 1
fi
if [[ "${path}" == [A-Za-z]:\\* ]] || [[ "${path}" == \\\\* ]]; then
if ! is_wsl; then
return 0
fi
if ! command_exists "powershell.exe"; then
echo "[error] powershell.exe is required in WSL to create Windows-native Chrome profile directories." >&2
return 1
fi
escaped_path="$(powershell_quote_literal "${path}")"
if ! ps_output="$(
powershell.exe -NoProfile -Command "New-Item -ItemType Directory -Force -Path '${escaped_path}' | Out-Null" \
2>&1
)"; then
ps_output="$(trim_carriage_return "${ps_output}")"
echo "[error] Could not create Chrome profile directory ${path} via PowerShell: ${ps_output}" >&2
return 1
fi
return 0
fi
mkdir -p "${path}"
}
resolve_platform() {
if is_wsl; then
printf 'wsl\n'
return 0
fi
if is_macos; then
printf 'macos\n'
return 0
fi
if is_native_linux; then
printf 'linux\n'
return 0
fi
error_exit "launch.bash chrome supports macOS, Linux, and WSL only."
}
resolve_macos_chrome_app() {
local app_path="${CHROME_APP:-/Applications/Google Chrome.app}"
if [[ ! -d "${app_path}" ]]; then
error_exit "Chrome app not found at ${app_path}. Set CHROME_APP to a valid macOS Chrome app bundle."
fi
printf '%s\n' "${app_path}"
}
try_resolve_linux_chrome_app() {
local candidate
local resolved
if [[ -n "${CHROME_APP}" ]]; then
if resolved="$(command -v "${CHROME_APP}" 2>/dev/null)"; then
printf '%s\n' "${resolved}"
return 0
fi
if [[ -x "${CHROME_APP}" ]]; then
printf '%s\n' "${CHROME_APP}"
return 0
fi
return 1
fi
for candidate in google-chrome google-chrome-stable chromium chromium-browser; do
if resolved="$(command -v "${candidate}" 2>/dev/null)"; then
printf '%s\n' "${resolved}"
return 0
fi
done
return 1
}
resolve_linux_chrome_app() {
local resolved
if resolved="$(try_resolve_linux_chrome_app)"; then
printf '%s\n' "${resolved}"
return 0
fi
if [[ -n "${CHROME_APP}" ]]; then
error_exit "Chrome executable not found at ${CHROME_APP}. Set CHROME_APP to a valid Chrome or Chromium executable."
fi
error_exit "Chrome or Chromium was not found on Linux. Install one of them or set CHROME_APP."
}
try_resolve_wsl_chrome_app() {
resolve_wsl_windows_chrome_path
}
resolve_wsl_chrome_app() {
local resolved
if resolved="$(try_resolve_wsl_chrome_app)"; then
printf '%s\n' "${resolved}"
return 0
fi
error_exit "Windows Chrome was not found from WSL. Install Chrome on Windows or set CHROME_APP to chrome.exe."
}
resolve_windows_local_chrome_path_via_powershell() {
local output
if ! output="$(
powershell.exe -NoProfile -Command '$path = Join-Path $env:LOCALAPPDATA "Google\\Chrome\\Application\\chrome.exe"; if (Test-Path $path) { [Console]::Out.Write($path) }' \
2>&1
)"; then
POWERSHELL_LAST_ERROR="$(trim_carriage_return "${output}")"
return 1
fi
output="$(trim_carriage_return "${output}")"
if [[ -z "${output}" ]]; then
return 1
fi
printf '%s\n' "${output}"
}
is_valid_wsl_windows_chrome_candidate() {
local candidate="$1"
local wsl_candidate
if [[ -z "${candidate}" ]]; then
return 1
fi
if [[ "${candidate}" == [A-Za-z]:\\* ]]; then
if ! command_exists "wslpath"; then
return 1