YrXtals/scripts/ios/select.sh

93 lines
2.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
STATE="$ROOT/.yrxtls-ios-target"
case "$(uname -s)" in
Darwin) ;;
*) echo "select-ios only works on macOS" >&2; exit 1;;
esac
ENTRIES=()
while IFS= read -r line; do
line="${line%"${line##*[![:space:]]}"}"
udid=$(echo "$line" | grep -oE '[A-F0-9-]{36}' | head -1)
[ -z "$udid" ] && continue
name=$(echo "$line" | awk -F' +' '{print $1}')
model=$(echo "$line" | awk -F' +' '{print $5}')
ENTRIES+=("device|$udid|$name ($model)")
done < <(xcrun devicectl list devices 2>/dev/null | grep "available (paired)")
declare -A SEEN_SIMS
runtime_re='^-- (.*) --$'
sim_re='^[[:space:]]+(.*)[[:space:]]\(([A-F0-9-]{36})\)[[:space:]]\(([^)]+)\)'
while IFS= read -r line; do
if [[ "$line" =~ $runtime_re ]]; then
runtime="${BASH_REMATCH[1]}"
continue
fi
if [[ "$line" =~ $sim_re ]]; then
name="${BASH_REMATCH[1]}"
udid="${BASH_REMATCH[2]}"
[ -n "${SEEN_SIMS[$name]:-}" ] && continue
SEEN_SIMS[$name]=1
ENTRIES+=("sim|$udid|$name ($runtime)")
fi
done < <(xcrun simctl list devices available 2>/dev/null)
if [ ${#ENTRIES[@]} -eq 0 ]; then
echo "no devices or simulators found" >&2
exit 1
fi
echo "Pick an iOS target (saved to .yrxtls-ios-target):"
echo
i=1
for e in "${ENTRIES[@]}"; do
kind="${e%%|*}"
rest="${e#*|}"
udid="${rest%%|*}"
label="${rest#*|}"
case "$kind" in
device) tag="📱 device" ;;
sim) tag="🖥️ sim " ;;
esac
printf " %2d) %s %s\n" "$i" "$tag" "$label"
i=$((i+1))
done
echo " 0) clear selection (auto-pick)"
echo
read -r -p "> " choice
if [ "$choice" = "0" ]; then
rm -f "$STATE"
echo "selection cleared, scripts will auto-pick again."
exit 0
fi
if ! [[ "$choice" =~ ^[0-9]+$ ]] || [ "$choice" -lt 1 ] || [ "$choice" -gt ${#ENTRIES[@]} ]; then
echo "not a valid choice" >&2
exit 1
fi
picked="${ENTRIES[$((choice-1))]}"
KIND="${picked%%|*}"
rest="${picked#*|}"
UDID="${rest%%|*}"
LABEL="${rest#*|}"
cat > "$STATE" <<EOF
YRXTALS_IOS_KIND=$KIND
YRXTALS_IOS_UDID=$UDID
YRXTALS_IOS_LABEL="$LABEL"
EOF
echo
echo "saved: $KIND $UDID"
echo "label: $LABEL"
echo
echo "next: cargo xtask debug-ios (kind no longer needs to be passed)"