|
| 1 | +set -euo pipefail |
| 2 | + |
| 3 | +# DEVICE_NAME="iPhone 16 Pro" |
| 4 | +# RUNTIME_SDK="iphonesimulator18.1" |
| 5 | +# Allow overriding device type by ID; fallback to name for display purposes |
| 6 | +DEVICE_TYPE_ID="${DEVICE_TYPE_ID:-}" |
| 7 | +DEVICE_NAME="${DEVICE_NAME:-${DEVICE_TYPE_ID:-iPhone 16 Pro}}" |
| 8 | + |
| 9 | +# Build the runtime ID (must use SimRuntime namespace) |
| 10 | +RUNTIME_ID="com.apple.CoreSimulator.SimRuntime.iOS-$(echo "$RUNTIME_SDK" | sed -E 's/[^0-9]*([0-9]+)\.([0-9]+).*/\1-\2/')" |
| 11 | + |
| 12 | +# Validate that the requested device type exists |
| 13 | +if [ -n "${DEVICE_TYPE_ID}" ]; then |
| 14 | + xcrun simctl list devicetypes | grep -q "(${DEVICE_TYPE_ID})" || { echo "Device type ID '${DEVICE_TYPE_ID}' not found"; exit 1; } |
| 15 | +else |
| 16 | + xcrun simctl list devicetypes | grep -q "^${DEVICE_NAME} " || { echo "Device type '${DEVICE_NAME}' not found"; exit 1; } |
| 17 | +fi |
| 18 | + |
| 19 | +# Validate that the requested runtime exists |
| 20 | +xcrun simctl list runtimes | grep -q "${RUNTIME_ID}" || { echo "Runtime '${RUNTIME_ID}' not installed on this runner"; exit 1; } |
| 21 | + |
| 22 | +# Try to locate an existing device of the given type and runtime, otherwise create it |
| 23 | +UDID=$(xcrun simctl list devices "${RUNTIME_ID}" | awk -v dev="${DEVICE_NAME}" -F'[()]' ' |
| 24 | + $0 ~ dev { |
| 25 | + cand=$(NF-1) |
| 26 | + gsub(/^[[:space:]]+|[[:space:]]+$/, "", cand) |
| 27 | + if (cand ~ /^[0-9A-Fa-f-]{36}$/) { print cand; exit } |
| 28 | + }') |
| 29 | +if [ -z "${UDID:-}" ]; then |
| 30 | + TYPE_ARG="${DEVICE_TYPE_ID:-${DEVICE_NAME}}" |
| 31 | + UDID=$(xcrun simctl create "CI ${DEVICE_NAME}" "${TYPE_ARG}" "${RUNTIME_ID}") |
| 32 | + echo "Created ${DEVICE_NAME}: ${UDID}" |
| 33 | +else |
| 34 | + echo "Reusing ${DEVICE_NAME}: ${UDID}" |
| 35 | +fi |
| 36 | +
|
| 37 | +xcrun simctl boot "${UDID}" || true |
| 38 | +xcrun simctl bootstatus "${UDID}" -b |
| 39 | +
|
| 40 | +defaults write com.apple.iphonesimulator CurrentDeviceUDID -string "${UDID}" || echo "Warning: failed to set CurrentDeviceUDID via defaults (non-fatal)" |
0 commit comments