|
11 | 11 |
|
12 | 12 | Set-StrictMode -Version latest |
13 | 13 | $ErrorActionPreference = 'Stop' |
| 14 | +. $PSScriptRoot/ios-simulator-utils.ps1 |
14 | 15 |
|
15 | 16 | if (!$Build -and !$Run) |
16 | 17 | { |
|
60 | 61 | '--set-env', "CI=$envValue" |
61 | 62 | ) |
62 | 63 |
|
63 | | - # Version you want to pin (make this a param later if needed) |
64 | | - $IosVersion = '18.5' |
65 | | - |
66 | | - $udid = $null |
67 | | - $simDevices = & xcrun simctl list devices --json | ConvertFrom-Json |
68 | | - $devicesByRuntime = $simDevices.devices |
69 | | - |
70 | | - # Preferred device types (ordered) |
71 | | - $preferredTypes = @( |
72 | | - 'com.apple.CoreSimulator.SimDeviceType.iPhone-XS', |
73 | | - 'com.apple.CoreSimulator.SimDeviceType.iPhone-16', |
74 | | - 'com.apple.CoreSimulator.SimDeviceType.iPhone-15' |
75 | | - ) |
76 | | - $preferredIndex = @{} |
77 | | - for ($i = 0; $i -lt $preferredTypes.Count; $i++) { $preferredIndex[$preferredTypes[$i]] = $i } |
78 | | - |
79 | | - # Build exact runtime key (e.g. com.apple.CoreSimulator.SimRuntime.iOS-18-0) |
80 | | - $dashVer = $IosVersion -replace '\.','-' |
81 | | - $exactKey = "com.apple.CoreSimulator.SimRuntime.iOS-$dashVer" |
82 | | - |
83 | | - $runtimeKey = $null |
84 | | - if ($devicesByRuntime.PSObject.Properties.Name -contains $exactKey) { |
85 | | - $runtimeKey = $exactKey |
| 64 | + $udid = Get-IosSimulatorUdid -IosVersion '18.5' -Verbose |
| 65 | + if ($udid) { |
| 66 | + $arguments += @('--device', $udid) |
86 | 67 | } else { |
87 | | - # Fallback: pick highest patch for requested major |
88 | | - $major = ($IosVersion.Split('.')[0]) |
89 | | - $candidates = $devicesByRuntime.PSObject.Properties.Name | |
90 | | - Where-Object { $_ -match "com\.apple\.CoreSimulator\.SimRuntime\.iOS-$major-" } |
91 | | - if ($candidates) { |
92 | | - $runtimeKey = $candidates | |
93 | | - Sort-Object { |
94 | | - # Extract trailing iOS-x-y -> x.y |
95 | | - $v = ($_ -replace '.*iOS-','') -replace '-','.' |
96 | | - try { [Version]$v } catch { [Version]'0.0' } |
97 | | - } -Descending | |
98 | | - Select-Object -First 1 |
99 | | - Write-Host "Exact runtime $exactKey not found. Using fallback runtime $runtimeKey" |
100 | | - } else { |
101 | | - throw "No simulator runtime found for iOS major $major" |
102 | | - } |
103 | | - } |
104 | | - |
105 | | - $runtimeDevices = $devicesByRuntime.PSObject.Properties | |
106 | | - Where-Object { $_.Name -eq $runtimeKey } | |
107 | | - Select-Object -ExpandProperty Value |
108 | | - |
109 | | - if (-not $runtimeDevices) { |
110 | | - throw "Runtime key $runtimeKey present but no devices listed." |
| 68 | + Write-Host "No suitable simulator found; proceeding without a specific --device" |
111 | 69 | } |
112 | | - |
113 | | - # Filter usable devices |
114 | | - $usable = $runtimeDevices | Where-Object { $_.isAvailable -and $_.state -in @('Shutdown','Booted') } |
115 | | - if (-not $usable) { throw "No available devices in runtime $runtimeKey" } |
116 | | - |
117 | | - # Compute weights |
118 | | - $ranked = $usable | ForEach-Object { |
119 | | - $dt = $_.deviceTypeIdentifier |
120 | | - $weightPref = if ($preferredIndex.ContainsKey($dt)) { $preferredIndex[$dt] } else { 9999 } |
121 | | - $isIphone = ($dt -match 'iPhone') ? 0 : 1 # prefer iPhone over iPad if not explicitly preferred |
122 | | - [PSCustomObject]@{ |
123 | | - Device = $_ |
124 | | - WeightPref = $weightPref |
125 | | - WeightFamily = $isIphone |
126 | | - WeightBoot = if ($_.state -eq 'Booted') { 0 } else { 1 } |
127 | | - SortName = $_.name |
128 | | - } |
129 | | - } |
130 | | - |
131 | | - $selected = $ranked | |
132 | | - Sort-Object WeightPref, WeightFamily, WeightBoot, SortName | |
133 | | - Select-Object -First 1 |
134 | | - |
135 | | - Write-Host "Candidate devices (top 5 by weight):" |
136 | | - $ranked | |
137 | | - Sort-Object WeightPref, WeightFamily, WeightBoot, SortName | |
138 | | - Select-Object -First 5 | |
139 | | - ForEach-Object { |
140 | | - Write-Host (" {0} | {1} | pref={2} fam={3}" -f $_.Device.name, $_.Device.deviceTypeIdentifier, $_.WeightPref, $_.WeightFamily) |
141 | | - } |
142 | | - |
143 | | - if (-not $selected) { |
144 | | - throw "Failed to select a simulator." |
145 | | - } |
146 | | - |
147 | | - $udid = $selected.Device.udid |
148 | | - Write-Host "Selected simulator: $($selected.Device.name) ($($selected.Device.deviceTypeIdentifier)) [$udid]" |
149 | | - $arguments += @('--device', $udid) |
150 | 70 | } |
151 | 71 |
|
152 | 72 | if ($Build) |
|
0 commit comments