YrXtals/scripts/android/select.sh

33 lines
870 B
Bash
Executable File

#!/usr/bin/env bash
# lists adb devices and persists the chosen serial to .yrxtls-android-target.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/_env.sh"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
TARGET_FILE="$REPO_ROOT/.yrxtls-android-target"
mapfile -t LINES < <(adb devices -l | tail -n +2 | grep -v '^\s*$' || true)
if [[ ${#LINES[@]} -eq 0 ]]; then
echo "no devices attached. plug in a phone with USB debugging enabled or start an emulator." >&2
exit 1
fi
echo "attached devices:"
for i in "${!LINES[@]}"; do
printf " [%d] %s\n" "$i" "${LINES[$i]}"
done
read -rp "pick index: " idx
chosen="${LINES[$idx]}"
serial="$(awk '{print $1}' <<<"$chosen")"
if [[ -z "$serial" ]]; then
echo "no serial parsed." >&2
exit 1
fi
echo "$serial" > "$TARGET_FILE"
echo "saved $serial to $TARGET_FILE"