|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Script to reliably launch iOS Simulator |
| 4 | +# Usage: ./launch-ios-simulator.sh <device_name> <os_version> [timeout_seconds] |
| 5 | +# |
| 6 | +# Examples: |
| 7 | +# ./launch-ios-simulator.sh "iPhone 17" "26.0" |
| 8 | +# ./launch-ios-simulator.sh "iPhone 17" "26.0" 120 |
| 9 | +# |
| 10 | +# Exit codes: |
| 11 | +# 0 - Success |
| 12 | +# 1 - Device not found |
| 13 | +# 2 - Boot timeout |
| 14 | +# 3 - Invalid arguments |
| 15 | + |
| 16 | +set -euo pipefail |
| 17 | + |
| 18 | +# Configuration |
| 19 | +DEVICE_NAME="${1:-}" |
| 20 | +OS_VERSION="${2:-}" |
| 21 | +TIMEOUT="${3:-120}" |
| 22 | +CHECK_INTERVAL=2 |
| 23 | + |
| 24 | +# Colors for output |
| 25 | +RED='\033[0;31m' |
| 26 | +GREEN='\033[0;32m' |
| 27 | +YELLOW='\033[1;33m' |
| 28 | +BLUE='\033[0;34m' |
| 29 | +NC='\033[0m' # No Color |
| 30 | + |
| 31 | +# Helper functions |
| 32 | +log_info() { |
| 33 | + printf "${BLUE}ℹ${NC} %s\n" "$1" |
| 34 | +} |
| 35 | + |
| 36 | +log_success() { |
| 37 | + printf "${GREEN}✓${NC} %s\n" "$1" |
| 38 | +} |
| 39 | + |
| 40 | +log_warning() { |
| 41 | + printf "${YELLOW}⚠${NC} %s\n" "$1" |
| 42 | +} |
| 43 | + |
| 44 | +log_error() { |
| 45 | + printf "${RED}✗${NC} %s\n" "$1" >&2 |
| 46 | +} |
| 47 | + |
| 48 | +# Validate arguments |
| 49 | +if [ -z "$DEVICE_NAME" ] || [ -z "$OS_VERSION" ]; then |
| 50 | + log_error "Usage: $0 <device_name> <os_version> [timeout_seconds]" |
| 51 | + log_error "Example: $0 \"iPhone 17\" \"26.0\" 120" |
| 52 | + exit 3 |
| 53 | +fi |
| 54 | + |
| 55 | +log_info "Starting iOS Simulator setup..." |
| 56 | +log_info "Device: $DEVICE_NAME" |
| 57 | +log_info "OS Version: $OS_VERSION" |
| 58 | +log_info "Timeout: ${TIMEOUT}s" |
| 59 | + |
| 60 | +# Kill any stuck simulators |
| 61 | +log_info "Checking for existing Simulator processes..." |
| 62 | +if pgrep -x "Simulator" > /dev/null; then |
| 63 | + log_warning "Found existing Simulator process, shutting down..." |
| 64 | + killall Simulator 2>/dev/null || true |
| 65 | + sleep 2 |
| 66 | +fi |
| 67 | + |
| 68 | +# Find the device UDID |
| 69 | +log_info "Finding device UDID for '$DEVICE_NAME' with iOS $OS_VERSION..." |
| 70 | + |
| 71 | +# Convert version like "26.0" to runtime key format "iOS-26-0" |
| 72 | +IOS_RUNTIME_KEY=$(echo "$OS_VERSION" | sed 's/\./-/g') |
| 73 | + |
| 74 | +# Always show available devices |
| 75 | +log_info "" |
| 76 | +log_info "Available devices:" |
| 77 | +xcrun simctl list devices available --json | \ |
| 78 | + jq -r '.devices | to_entries[] | select(.key | contains("iOS")) | .key as $runtime | .value[] | |
| 79 | + ($runtime | capture("iOS-(?<major>[0-9]+)-(?<minor>[0-9]+)") | "iOS \(.major).\(.minor)") as $version | |
| 80 | + " \(.name) - \($version) - \(.udid)"' |
| 81 | +log_info "" |
| 82 | + |
| 83 | +# Search for device in the specific iOS version runtime |
| 84 | +DEVICE_UDID=$(xcrun simctl list devices available --json | \ |
| 85 | + jq -r --arg name "$DEVICE_NAME" --arg runtime_suffix "iOS-${IOS_RUNTIME_KEY}" \ |
| 86 | + '.devices | to_entries[] | select(.key | endswith($runtime_suffix)) | .value[] | select(.name == $name and .isAvailable == true) | .udid' | \ |
| 87 | + head -n 1) |
| 88 | + |
| 89 | +if [ -z "$DEVICE_UDID" ]; then |
| 90 | + log_error "Device '$DEVICE_NAME' with iOS $OS_VERSION not found or not available" |
| 91 | + log_info "Looking for runtime matching: iOS-${IOS_RUNTIME_KEY}" |
| 92 | + exit 1 |
| 93 | +fi |
| 94 | + |
| 95 | +log_success "Found device: $DEVICE_NAME (UDID: $DEVICE_UDID)" |
| 96 | + |
| 97 | +# Check current state |
| 98 | +CURRENT_STATE=$(xcrun simctl list devices --json | \ |
| 99 | + jq -r --arg udid "$DEVICE_UDID" \ |
| 100 | + '.devices[] | .[] | select(.udid == $udid) | .state') |
| 101 | + |
| 102 | +log_info "Current state: $CURRENT_STATE" |
| 103 | + |
| 104 | +# Shutdown if already booted to ensure clean state |
| 105 | +if [ "$CURRENT_STATE" = "Booted" ]; then |
| 106 | + log_warning "Device already booted, shutting down for clean start..." |
| 107 | + xcrun simctl shutdown "$DEVICE_UDID" 2>/dev/null || true |
| 108 | + sleep 2 |
| 109 | +fi |
| 110 | + |
| 111 | +# Boot the simulator |
| 112 | +log_info "Booting simulator..." |
| 113 | +xcrun simctl boot "$DEVICE_UDID" 2>/dev/null || { |
| 114 | + # Check if it's already booting or booted |
| 115 | + CURRENT_STATE=$(xcrun simctl list devices --json | \ |
| 116 | + jq -r --arg udid "$DEVICE_UDID" \ |
| 117 | + '.devices[] | .[] | select(.udid == $udid) | .state') |
| 118 | + |
| 119 | + if [ "$CURRENT_STATE" != "Booted" ] && [ "$CURRENT_STATE" != "Booting" ]; then |
| 120 | + log_error "Failed to boot simulator" |
| 121 | + exit 2 |
| 122 | + fi |
| 123 | +} |
| 124 | + |
| 125 | +# Wait for simulator to boot |
| 126 | +log_info "Waiting for simulator to boot (timeout: ${TIMEOUT}s)..." |
| 127 | +ELAPSED=0 |
| 128 | +while [ $ELAPSED -lt $TIMEOUT ]; do |
| 129 | + STATE=$(xcrun simctl list devices --json | \ |
| 130 | + jq -r --arg udid "$DEVICE_UDID" \ |
| 131 | + '.devices[] | .[] | select(.udid == $udid) | .state') |
| 132 | + |
| 133 | + if [ "$STATE" = "Booted" ]; then |
| 134 | + log_success "Simulator booted successfully!" |
| 135 | + break |
| 136 | + fi |
| 137 | + |
| 138 | + if [ $((ELAPSED % 10)) -eq 0 ] && [ $ELAPSED -gt 0 ]; then |
| 139 | + log_info "Still waiting... (${ELAPSED}s elapsed)" |
| 140 | + fi |
| 141 | + |
| 142 | + sleep $CHECK_INTERVAL |
| 143 | + ELAPSED=$((ELAPSED + CHECK_INTERVAL)) |
| 144 | +done |
| 145 | + |
| 146 | +# Verify boot completed |
| 147 | +FINAL_STATE=$(xcrun simctl list devices --json | \ |
| 148 | + jq -r --arg udid "$DEVICE_UDID" \ |
| 149 | + '.devices[] | .[] | select(.udid == $udid) | .state') |
| 150 | + |
| 151 | +if [ "$FINAL_STATE" != "Booted" ]; then |
| 152 | + log_error "Simulator boot timeout after ${TIMEOUT}s (state: $FINAL_STATE)" |
| 153 | + exit 2 |
| 154 | +fi |
| 155 | + |
| 156 | +# Open Simulator.app (optional, for visibility) |
| 157 | +log_info "Opening Simulator.app..." |
| 158 | +open -a Simulator --args -CurrentDeviceUDID "$DEVICE_UDID" 2>/dev/null || { |
| 159 | + log_warning "Failed to open Simulator.app (non-critical)" |
| 160 | +} |
| 161 | + |
| 162 | +# Wait for SpringBoard to be ready (optional check, may not work on all systems) |
| 163 | +log_info "Waiting for SpringBoard to be ready..." |
| 164 | +ELAPSED=0 |
| 165 | +SPRINGBOARD_READY=false |
| 166 | +while [ $ELAPSED -lt 10 ]; do |
| 167 | + if xcrun simctl spawn "$DEVICE_UDID" launchctl print system 2>/dev/null | grep -q "com.apple.springboard" 2>/dev/null; then |
| 168 | + SPRINGBOARD_READY=true |
| 169 | + break |
| 170 | + fi |
| 171 | + sleep 1 |
| 172 | + ELAPSED=$((ELAPSED + 1)) |
| 173 | +done |
| 174 | + |
| 175 | +if [ "$SPRINGBOARD_READY" = true ]; then |
| 176 | + log_success "SpringBoard is ready!" |
| 177 | +else |
| 178 | + log_warning "SpringBoard check timed out (non-critical, simulator may still be usable)" |
| 179 | +fi |
| 180 | + |
| 181 | +# Final verification |
| 182 | +log_info "Verifying simulator status..." |
| 183 | +xcrun simctl bootstatus "$DEVICE_UDID" -b 2>/dev/null || { |
| 184 | + log_warning "bootstatus check failed (non-critical)" |
| 185 | +} |
| 186 | + |
| 187 | +# Print simulator info |
| 188 | +log_success "Simulator is ready!" |
| 189 | +echo "" |
| 190 | +echo "Device Information:" |
| 191 | +echo " Name: $DEVICE_NAME" |
| 192 | +echo " OS Version: iOS $OS_VERSION" |
| 193 | +echo " UDID: $DEVICE_UDID" |
| 194 | +echo " State: $(xcrun simctl list devices --json | jq -r --arg udid "$DEVICE_UDID" '.devices[] | .[] | select(.udid == $udid) | .state')" |
| 195 | +echo "" |
| 196 | + |
| 197 | +# Export UDID for use in subsequent steps |
| 198 | +echo "SIMULATOR_UDID=$DEVICE_UDID" >> "${GITHUB_ENV:-/dev/null}" |
| 199 | +log_success "Simulator UDID exported to GITHUB_ENV" |
| 200 | + |
| 201 | +log_success "Setup complete! Simulator is ready for testing." |
| 202 | +exit 0 |
0 commit comments