-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathrun-demo.sh
More file actions
executable file
·259 lines (211 loc) · 5.95 KB
/
run-demo.sh
File metadata and controls
executable file
·259 lines (211 loc) · 5.95 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
#!/usr/bin/env bash
set -euo pipefail
APP_ID="com.firebaseui.android.demo"
MAIN_ACTIVITY="com.firebaseui.android.demo.MainActivity"
APK_RELATIVE_PATH="app/build/outputs/apk/debug/app-debug.apk"
EMULATOR_LOG="${TMPDIR:-/tmp}/firebaseui-android-emulator.log"
EMULATOR_PID=""
LAUNCHED_EMULATOR=0
CONNECTED_DEVICES=()
AVAILABLE_AVDS=()
KNOWN_DEVICE_SERIALS=()
usage() {
cat <<'EOF'
Usage: run-demo.sh [--help]
Builds, installs, and launches the FirebaseUI Android demo app.
The script lets you:
1. Use an already connected Android device or emulator.
2. Start an AVD selected from `emulator -list-avds`.
EOF
}
require_command() {
local command_name="$1"
if ! command -v "$command_name" >/dev/null 2>&1; then
echo "Missing required command: $command_name" >&2
exit 1
fi
}
collect_connected_devices() {
CONNECTED_DEVICES=()
while IFS= read -r serial; do
if [[ -n "$serial" ]]; then
CONNECTED_DEVICES+=("$serial")
fi
done < <(adb devices | awk 'NR > 1 && $2 == "device" { print $1 }')
}
collect_available_avds() {
AVAILABLE_AVDS=()
while IFS= read -r avd_name; do
if [[ -n "$avd_name" ]]; then
AVAILABLE_AVDS+=("$avd_name")
fi
done < <(emulator -list-avds 2>/dev/null)
}
prompt_for_choice() {
local prompt="$1"
shift
local options=("$@")
local selection
local index=1
echo "$prompt"
for option in "${options[@]}"; do
printf " %d) %s\n" "$index" "$option"
index=$((index + 1))
done
while true; do
printf "Select an option [1-%d]: " "${#options[@]}"
read -r selection
if [[ "$selection" =~ ^[0-9]+$ ]] && (( selection >= 1 && selection <= ${#options[@]} )); then
CHOICE_INDEX=$((selection - 1))
return 0
fi
echo "Please enter a number between 1 and ${#options[@]}."
done
}
is_known_device() {
local candidate="$1"
local known
local index
for (( index=0; index<${#KNOWN_DEVICE_SERIALS[@]}; index++ )); do
known="${KNOWN_DEVICE_SERIALS[$index]}"
if [[ "$known" == "$candidate" ]]; then
return 0
fi
done
return 1
}
cleanup_on_exit() {
local exit_code="$?"
if (( exit_code != 0 )) && (( LAUNCHED_EMULATOR == 1 )) && [[ -n "$EMULATOR_PID" ]]; then
echo "Stopping emulator started by this script..." >&2
kill "$EMULATOR_PID" 2>/dev/null || true
fi
exit "$exit_code"
}
wait_for_device_boot() {
local serial="$1"
local attempt
local boot_completed
adb -s "$serial" wait-for-device >/dev/null
echo "Waiting for $serial to finish booting..."
for (( attempt=1; attempt<=120; attempt++ )); do
boot_completed="$(adb -s "$serial" shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')"
if [[ "$boot_completed" == "1" ]]; then
echo "$serial is ready."
return 0
fi
sleep 2
done
echo "Timed out waiting for $serial to boot." >&2
exit 1
}
start_selected_avd() {
local avd_name="$1"
local attempt
local serial
local index
collect_connected_devices
KNOWN_DEVICE_SERIALS=()
for (( index=0; index<${#CONNECTED_DEVICES[@]}; index++ )); do
KNOWN_DEVICE_SERIALS+=("${CONNECTED_DEVICES[$index]}")
done
echo "Starting emulator '$avd_name'..."
echo "Emulator logs: $EMULATOR_LOG"
emulator -avd "$avd_name" >"$EMULATOR_LOG" 2>&1 &
EMULATOR_PID=$!
LAUNCHED_EMULATOR=1
for (( attempt=1; attempt<=120; attempt++ )); do
sleep 2
collect_connected_devices
for (( index=0; index<${#CONNECTED_DEVICES[@]}; index++ )); do
serial="${CONNECTED_DEVICES[$index]}"
case "$serial" in
emulator-*)
if ! is_known_device "$serial"; then
TARGET_SERIAL="$serial"
wait_for_device_boot "$TARGET_SERIAL"
return 0
fi
;;
esac
done
done
echo "Failed to detect the new emulator for AVD '$avd_name'." >&2
exit 1
}
choose_target_device() {
local option_labels=()
local option_types=()
local option_values=()
local serial
local avd_name
local index
collect_connected_devices
collect_available_avds
for (( index=0; index<${#CONNECTED_DEVICES[@]}; index++ )); do
serial="${CONNECTED_DEVICES[$index]}"
option_labels+=("Use connected device: $serial")
option_types+=("device")
option_values+=("$serial")
done
for (( index=0; index<${#AVAILABLE_AVDS[@]}; index++ )); do
avd_name="${AVAILABLE_AVDS[$index]}"
option_labels+=("Start emulator: $avd_name")
option_types+=("avd")
option_values+=("$avd_name")
done
if (( ${#option_labels[@]} == 0 )); then
cat >&2 <<'EOF'
No connected Android devices were found, and no AVDs are available.
Connect a device or create an emulator first, then rerun this script.
EOF
exit 1
fi
prompt_for_choice "Choose a target for the demo app:" "${option_labels[@]}"
case "${option_types[$CHOICE_INDEX]}" in
device)
TARGET_SERIAL="${option_values[$CHOICE_INDEX]}"
;;
avd)
start_selected_avd "${option_values[$CHOICE_INDEX]}"
;;
esac
}
main() {
local script_dir
local repo_root
local apk_path
local launch_output
if [[ "${1:-}" == "--help" ]]; then
usage
exit 0
fi
trap cleanup_on_exit EXIT
require_command adb
require_command emulator
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
repo_root="$(cd "$script_dir/../.." && pwd)"
apk_path="$repo_root/$APK_RELATIVE_PATH"
if [[ ! -x "$repo_root/gradlew" ]]; then
echo "gradlew not found or not executable at $repo_root/gradlew" >&2
exit 1
fi
choose_target_device
echo "Building debug APK..."
"$repo_root/gradlew" :app:assembleDebug
if [[ ! -f "$apk_path" ]]; then
echo "APK not found at $apk_path" >&2
exit 1
fi
echo "Installing app on $TARGET_SERIAL..."
adb -s "$TARGET_SERIAL" install -r "$apk_path"
echo "Launching demo app on $TARGET_SERIAL..."
launch_output="$(adb -s "$TARGET_SERIAL" shell am start -n "$APP_ID/$MAIN_ACTIVITY")"
echo "$launch_output"
if [[ "$launch_output" == *"Error:"* ]]; then
echo "Failed to launch the demo app." >&2
exit 1
fi
trap - EXIT
}
main "$@"