Repository navigation
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·501 lines (425 loc) · 15.4 KB
/
Copy pathrun.sh
File metadata and controls
executable file
·501 lines (425 loc) · 15.4 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
#!/bin/bash
# Helper script to run Flutter with environment variables from .env
set -e
show_usage() {
cat <<EOF
Usage: ./run.sh [device|android-vm] [avd_name]
Examples:
./run.sh linux
./run.sh android
./run.sh android-x64
./run.sh android-vm
./run.sh android-vm Pixel_7_API_31
Tip:
./run.sh android resolves to connected android-x64 first.
Set ANDROID_AVD_NAME to choose a default emulator:
ANDROID_AVD_NAME=Pixel_7_API_31 ./run.sh android-vm
Set ANDROID_VM_GPU_MODE to override emulator rendering mode
(Linux defaults to swiftshader_indirect for stability):
ANDROID_VM_GPU_MODE=host ./run.sh android-vm
EOF
}
resolve_android_tool() {
local tool_name="$1"
local sdk_path=""
local tool_path=""
if command -v "$tool_name" >/dev/null 2>&1; then
command -v "$tool_name"
return 0
fi
for sdk_path in "${ANDROID_SDK_ROOT:-}" "${ANDROID_HOME:-}" "$HOME/Android/Sdk"; do
if [ -z "$sdk_path" ]; then
continue
fi
case "$tool_name" in
adb)
tool_path="$sdk_path/platform-tools/adb"
;;
emulator)
tool_path="$sdk_path/emulator/emulator"
;;
*)
return 1
;;
esac
if [ -x "$tool_path" ]; then
printf '%s\n' "$tool_path"
return 0
fi
done
return 1
}
is_android_vm_booted() {
local adb_bin="$1"
local emulator_id="$2"
local sys_boot=""
local dev_boot=""
local boot_anim=""
sys_boot=$("$adb_bin" -s "$emulator_id" shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')
dev_boot=$("$adb_bin" -s "$emulator_id" shell getprop dev.bootcomplete 2>/dev/null | tr -d '\r')
boot_anim=$("$adb_bin" -s "$emulator_id" shell getprop init.svc.bootanim 2>/dev/null | tr -d '\r')
[ "$sys_boot" = "1" ] && [ "$dev_boot" = "1" ] && [ "$boot_anim" = "stopped" ]
}
resolve_device_alias() {
local requested="$1"
local devices_json=""
local line=""
local trimmed_line=""
local current_id=""
local current_platform=""
local current_supported=""
local first_android_device=""
local android_x64_device=""
local android_arm64_device=""
case "$requested" in
android|android-x64|android-arm64)
;;
*)
printf '%s\n' "$requested"
return 0
;;
esac
if ! devices_json="$(flutter devices --machine 2>/dev/null)"; then
printf '%s\n' "$requested"
return 0
fi
while IFS= read -r line; do
trimmed_line="${line#"${line%%[![:space:]]*}"}"
case "$trimmed_line" in
'"id": "'*'",')
current_id="${trimmed_line#\"id\": \"}"
current_id="${current_id%%\",*}"
;;
'"isSupported": true,')
current_supported="true"
;;
'"isSupported": false,')
current_supported="false"
;;
'"targetPlatform": "'*'",')
current_platform="${trimmed_line#\"targetPlatform\": \"}"
current_platform="${current_platform%%\",*}"
if [ "$current_supported" = "true" ] && [[ "$current_platform" == android-* ]] && [ -n "$current_id" ]; then
if [ -z "$first_android_device" ]; then
first_android_device="$current_id"
fi
if [ "$current_platform" = "android-x64" ] && [ -z "$android_x64_device" ]; then
android_x64_device="$current_id"
fi
if [ "$current_platform" = "android-arm64" ] && [ -z "$android_arm64_device" ]; then
android_arm64_device="$current_id"
fi
fi
;;
esac
done <<<"$devices_json"
case "$requested" in
android)
if [ -n "$android_x64_device" ]; then
printf '%s\n' "$android_x64_device"
elif [ -n "$first_android_device" ]; then
printf '%s\n' "$first_android_device"
else
printf '%s\n' "$requested"
fi
;;
android-x64)
if [ -n "$android_x64_device" ]; then
printf '%s\n' "$android_x64_device"
elif [ -n "$first_android_device" ]; then
printf '%s\n' "$first_android_device"
else
printf '%s\n' "$requested"
fi
;;
android-arm64)
if [ -n "$android_arm64_device" ]; then
printf '%s\n' "$android_arm64_device"
elif [ -n "$first_android_device" ]; then
printf '%s\n' "$first_android_device"
else
printf '%s\n' "$requested"
fi
;;
esac
}
is_connected_adb_device() {
local adb_bin="$1"
local device_id="$2"
"$adb_bin" devices | awk -v id="$device_id" '$1 == id && $2 == "device" { found = 1 } END { exit found ? 0 : 1 }'
}
configure_android_local_api_access() {
local device_id="$1"
local adb_bin=""
# Respect explicit LOCAL_API_URL from .env or shell.
if [ -n "$LOCAL_API_URL" ]; then
return 0
fi
if ! adb_bin="$(resolve_android_tool adb)"; then
return 0
fi
if ! is_connected_adb_device "$adb_bin" "$device_id"; then
return 0
fi
# Map device localhost:8000 to host localhost:8000 so debug builds can
# keep using ws://localhost:8000 and http://localhost:8000.
if "$adb_bin" -s "$device_id" reverse tcp:8000 tcp:8000 >/dev/null 2>&1; then
echo "Configured Android local API tunnel on $device_id (localhost:8000 -> host:8000)" >&2
return 0
fi
# Fallbacks when adb reverse is unavailable.
case "$device_id" in
emulator-*)
LOCAL_API_URL="http://10.0.2.2:8000"
;;
127.0.0.1:*)
LOCAL_API_URL="http://10.0.3.2:8000"
;;
esac
if [ -n "$LOCAL_API_URL" ]; then
echo "Using Android fallback LOCAL_API_URL=$LOCAL_API_URL for $device_id" >&2
else
echo "Warning: could not configure Android local API access for $device_id" >&2
echo "Tip: set LOCAL_API_URL=http://<host-ip>:8000" >&2
fi
}
start_android_vm() {
local avd_name="${1:-${ANDROID_AVD_NAME:-}}"
local adb_bin=""
local emulator_bin=""
local emulator_gpu_mode=""
local launch_log=""
local launch_pid=""
local emulator_args=()
local emulators_output=""
local emulator_line=""
local emulator_candidate=""
local first_emulator=""
local preferred_emulator=""
local pixel7_emulator=""
local emulator_id=""
local elapsed=0
local max_wait_seconds=240
if ! adb_bin="$(resolve_android_tool adb)"; then
echo "Error: adb command not found. Install Android platform-tools." >&2
exit 1
fi
"$adb_bin" start-server >/dev/null
emulator_id=$("$adb_bin" devices | awk '/^emulator-[0-9]+[[:space:]]+device$/ {print $1; exit}')
if [ -n "$emulator_id" ]; then
echo "Android VM already running: $emulator_id" >&2
printf '%s\n' "$emulator_id"
return 0
fi
if [ -z "$avd_name" ]; then
if ! emulators_output="$(flutter emulators)"; then
echo "Error: failed to list Android Virtual Devices via Flutter CLI." >&2
exit 1
fi
while IFS= read -r emulator_line; do
case "$emulator_line" in
[A-Za-z0-9._-]*)
emulator_candidate="${emulator_line%% *}"
if [ "$emulator_candidate" = "Id" ] || [ "$emulator_candidate" = "To" ]; then
continue
fi
if [ -z "$first_emulator" ]; then
first_emulator="$emulator_candidate"
fi
if [ "$emulator_candidate" = "Pixel_7_API_31" ]; then
preferred_emulator="$emulator_candidate"
fi
if [ -z "$pixel7_emulator" ] && [[ "$emulator_candidate" == *Pixel_7* ]]; then
pixel7_emulator="$emulator_candidate"
fi
;;
esac
done <<<"$emulators_output"
if [ -n "$preferred_emulator" ]; then
avd_name="$preferred_emulator"
elif [ -n "$pixel7_emulator" ]; then
avd_name="$pixel7_emulator"
else
avd_name="$first_emulator"
fi
fi
if [ -z "$avd_name" ]; then
echo "Error: no Android Virtual Device found." >&2
echo "Create one in Android Studio Device Manager or set ANDROID_AVD_NAME." >&2
exit 1
fi
emulator_gpu_mode="${ANDROID_VM_GPU_MODE:-}"
if [ -z "$emulator_gpu_mode" ] && [ "$(uname -s)" = "Linux" ]; then
emulator_gpu_mode="swiftshader_indirect"
fi
if emulator_bin="$(resolve_android_tool emulator)"; then
launch_log="${ANDROID_EMULATOR_LOG_PATH:-/tmp/android-emulator-${avd_name}.log}"
echo "Starting Android VM (cold boot): $avd_name${emulator_gpu_mode:+ (gpu: $emulator_gpu_mode)}" >&2
emulator_args=("$emulator_bin" -avd "$avd_name" -no-snapshot-load)
if [ -n "$emulator_gpu_mode" ]; then
emulator_args+=(-gpu "$emulator_gpu_mode")
fi
nohup "${emulator_args[@]}" >"$launch_log" 2>&1 &
launch_pid="$!"
else
echo "Starting Android VM (cold boot): $avd_name" >&2
if ! flutter emulators --launch "$avd_name" --cold >/dev/null; then
echo "Error: failed to launch Android VM '$avd_name'." >&2
echo "Try: flutter emulators (list IDs), then rerun with ./run.sh android-vm <id>" >&2
exit 1
fi
fi
echo "Waiting for Android VM to boot..." >&2
while [ "$elapsed" -lt "$max_wait_seconds" ]; do
if [ -n "$launch_pid" ] && ! kill -0 "$launch_pid" 2>/dev/null; then
echo "Error: Android VM exited before it finished booting." >&2
if [ -n "$launch_log" ]; then
echo "Emulator log: $launch_log" >&2
fi
if [ "$emulator_gpu_mode" != "swiftshader_indirect" ]; then
echo "Tip: ANDROID_VM_GPU_MODE=swiftshader_indirect ./run.sh android-vm $avd_name" >&2
fi
exit 1
fi
emulator_id=$("$adb_bin" devices | awk '/^emulator-[0-9]+[[:space:]]+device$/ {print $1; exit}')
if [ -n "$emulator_id" ] && is_android_vm_booted "$adb_bin" "$emulator_id"; then
sleep 3
elapsed=$((elapsed + 3))
if "$adb_bin" -s "$emulator_id" shell getprop ro.build.version.sdk >/dev/null 2>&1; then
echo "Android VM is ready: $emulator_id" >&2
printf '%s\n' "$emulator_id"
return 0
fi
continue
fi
sleep 3
elapsed=$((elapsed + 3))
done
echo "Error: timed out waiting for Android VM to boot." >&2
echo "Check Android Studio Device Manager and run: flutter emulators" >&2
exit 1
}
# Default to linux if no device specified
TARGET="${1:-linux}"
if [ "$TARGET" = "-h" ] || [ "$TARGET" = "--help" ]; then
show_usage
exit 0
fi
# Load .env file if it exists
if [ -f .env ]; then
export $(grep -v '^#' .env | xargs)
fi
# Check if credentials are set
if [ -z "$SUPABASE_URL" ] || [ -z "$SUPABASE_ANON_KEY" ]; then
echo "Error: SUPABASE_URL and SUPABASE_ANON_KEY must be set."
echo "Copy .env.example to .env and fill in your values."
exit 1
fi
case "$TARGET" in
android-vm)
DEVICE="$(start_android_vm "${2:-}")"
;;
*)
DEVICE="$(resolve_device_alias "$TARGET")"
if [ "$DEVICE" != "$TARGET" ]; then
echo "Resolved device alias '$TARGET' to '$DEVICE'" >&2
fi
;;
esac
configure_android_local_api_access "$DEVICE"
# Build dart-define arguments
BUILD_TIMESTAMP="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
DART_DEFINES="--dart-define=SUPABASE_URL=$SUPABASE_URL --dart-define=SUPABASE_ANON_KEY=$SUPABASE_ANON_KEY --dart-define=BUILD_TIMESTAMP=$BUILD_TIMESTAMP"
# Point debug builds at the production API instead of the local dev server.
# Debug builds default to http://localhost:8000; PRODUCTION_API_URL overrides
# that (it sits at the top of the resolution order in api_config_base.dart).
# Override with PRODUCTION_API_URL in .env or the shell to target a different
# backend (set it empty to fall back to the local dev server):
# PRODUCTION_API_URL= ./run.sh linux # use http://localhost:8000
PRODUCTION_API_URL="${PRODUCTION_API_URL-https://api.chuk.chat}"
if [ -n "$PRODUCTION_API_URL" ]; then
DART_DEFINES="$DART_DEFINES --dart-define=PRODUCTION_API_URL=$PRODUCTION_API_URL"
fi
# Override the local API server URL (only used when PRODUCTION_API_URL is empty).
# Set LOCAL_API_URL in .env or export it to change:
# LOCAL_API_URL=http://192.168.1.10:8000 ./run.sh linux
if [ -n "$LOCAL_API_URL" ]; then
DART_DEFINES="$DART_DEFINES --dart-define=LOCAL_API_URL=$LOCAL_API_URL"
fi
# ── Feature flags ────────────────────────────────────────────────────────
# run.sh is the dev entry point: flags are ON here regardless of the
# production defaults in lib/platform_config.dart.
#
# Override any of them from .env or the shell, e.g.:
# FEATURE_SKILLS=false ./run.sh linux
#
# NOTE: FEATURE_PROJECTS used to be passed here and is a no-op — nothing in
# lib/ reads it. The flag that exists is FEATURE_WORKSPACES.
# On by default.
FEATURE_VOICE_MODE="${FEATURE_VOICE_MODE:-false}"
FEATURE_WORKSPACES="${FEATURE_WORKSPACES:-false}"
FEATURE_ARTIFACTS="${FEATURE_ARTIFACTS:-true}"
FEATURE_SERVER_TOOLS="${FEATURE_SERVER_TOOLS:-true}"
FEATURE_SKILLS="${FEATURE_SKILLS:-true}"
FEATURE_PAYMENTS_DIRECT="${FEATURE_PAYMENTS_DIRECT:-true}"
# Off by default on purpose — turning this on makes the app worse, not
# more complete. Set it explicitly if you actually want it:
# FEATURE_LINUX_KEYRING costs 10s+ of startup stall on every single run
FEATURE_LINUX_KEYRING="${FEATURE_LINUX_KEYRING:-false}"
# Agents (= CoWork): off by default, the app is plain chuk_chat. On, the app is
# the Agents messenger. A build flag, not a runtime switch.
FEATURE_AGENTS="${FEATURE_AGENTS:-false}"
# Voice call (owner-only test, lib/voice/): off by default. On, the call button
# shows once VOICE_TOKEN_URL (gitignored .env) names the token server:
# FEATURE_VOICE_CALL=true ./run-hot.sh linux
FEATURE_VOICE_CALL="${FEATURE_VOICE_CALL:-false}"
# Desktop-only; the tray plugin has nothing to attach to on Android/iOS.
case "$TARGET" in
android*|ios*)
FEATURE_SYSTEM_TRAY="${FEATURE_SYSTEM_TRAY:-false}"
;;
*)
FEATURE_SYSTEM_TRAY="${FEATURE_SYSTEM_TRAY:-true}"
;;
esac
# One list, used for both the defines and the startup log — two lists drift,
# and a log that disagrees with what was actually passed is worse than none.
ALL_FEATURE_FLAGS=(
FEATURE_VOICE_MODE FEATURE_WORKSPACES FEATURE_ARTIFACTS
FEATURE_SERVER_TOOLS FEATURE_SKILLS
FEATURE_SYSTEM_TRAY FEATURE_PAYMENTS_DIRECT
FEATURE_LINUX_KEYRING FEATURE_AGENTS FEATURE_VOICE_CALL
)
for _flag in "${ALL_FEATURE_FLAGS[@]}"; do
DART_DEFINES="$DART_DEFINES --dart-define=$_flag=${!_flag}"
done
# The voice token server comes from the gitignored .env (loaded above) or the
# shell. It is a private URL: passed through, never echoed.
if [ -n "$VOICE_TOKEN_URL" ]; then
DART_DEFINES="$DART_DEFINES --dart-define=VOICE_TOKEN_URL=$VOICE_TOKEN_URL"
fi
# PLATFORM_MOBILE / PLATFORM_DESKTOP are deliberately not set: with neither
# defined, kAutoDetectPlatform picks the right layout from the runtime device.
# Setting one here would force the wrong UI on the other target.
# FEATURE_IMAGE_GEN and FEATURE_MEDIA_MANAGER are hardcoded true in
# platform_config.dart — no define to pass.
# A development run must not be turned away by the installed app. The Linux
# runner takes a single-instance lock, and the installed build holds it for as
# long as it sits in the tray — the dev build then exits before its first
# frame and `flutter run` reports a dead log reader, not a busy lock.
export CHUK_MULTI_INSTANCE=1
# Print the assembled --dart-define string and exit. Lets run-hot.sh reuse the
# exact same defines (single source of truth) instead of duplicating them.
if [ -n "$PRINT_DEFINES" ]; then
printf '%s\n' "$DART_DEFINES"
exit 0
fi
# Run Flutter
echo "Running flutter in debug mode with device: $DEVICE"
echo "Features on: $(for _flag in "${ALL_FEATURE_FLAGS[@]}"; do
[ "${!_flag}" = "true" ] && printf '%s ' "${_flag#FEATURE_}"
done)"
echo "Features off: $(for _flag in "${ALL_FEATURE_FLAGS[@]}"; do
[ "${!_flag}" != "true" ] && printf '%s ' "${_flag#FEATURE_}"
done)"
flutter run -d "$DEVICE" $DART_DEFINES