|
| 1 | +function Get-IosSimulatorUdid { |
| 2 | + [CmdletBinding()] |
| 3 | + param( |
| 4 | + [string]$IosVersion = '18.5', |
| 5 | + [string[]]$PreferredDeviceTypes = @( |
| 6 | + 'com.apple.CoreSimulator.SimDeviceType.iPhone-XS', |
| 7 | + 'com.apple.CoreSimulator.SimDeviceType.iPhone-16', |
| 8 | + 'com.apple.CoreSimulator.SimDeviceType.iPhone-15' |
| 9 | + ) |
| 10 | + ) |
| 11 | + |
| 12 | + try { |
| 13 | + $simDevices = & xcrun simctl list devices --json | ConvertFrom-Json |
| 14 | + } catch { |
| 15 | + Write-Verbose "Failed to query simctl: $($_.Exception.Message)" |
| 16 | + return $null |
| 17 | + } |
| 18 | + if (-not $simDevices -or -not $simDevices.devices) { |
| 19 | + Write-Verbose "No devices structure returned." |
| 20 | + return $null |
| 21 | + } |
| 22 | + |
| 23 | + $devicesByRuntime = $simDevices.devices |
| 24 | + $preferredIndex = @{} |
| 25 | + for ($i = 0; $i -lt $PreferredDeviceTypes.Count; $i++) { |
| 26 | + $preferredIndex[$PreferredDeviceTypes[$i]] = $i |
| 27 | + } |
| 28 | + |
| 29 | + $dashVer = $IosVersion -replace '\.','-' |
| 30 | + $exactKey = "com.apple.CoreSimulator.SimRuntime.iOS-$dashVer" |
| 31 | + $runtimeKey = $null |
| 32 | + |
| 33 | + $allRuntimeNames = $devicesByRuntime.PSObject.Properties.Name |
| 34 | + if ($allRuntimeNames -contains $exactKey) { |
| 35 | + $runtimeKey = $exactKey |
| 36 | + Write-Verbose "Found exact runtime: $runtimeKey" |
| 37 | + } else { |
| 38 | + $major = ($IosVersion.Split('.')[0]) |
| 39 | + $candidates = $allRuntimeNames | Where-Object { $_ -match "com\.apple\.CoreSimulator\.SimRuntime\.iOS-$major-" } |
| 40 | + if ($candidates) { |
| 41 | + $runtimeKey = $candidates | |
| 42 | + Sort-Object { |
| 43 | + $v = ($_ -replace '.*iOS-','') -replace '-','.' |
| 44 | + try { [Version]$v } catch { [Version]'0.0' } |
| 45 | + } -Descending | |
| 46 | + Select-Object -First 1 |
| 47 | + Write-Verbose "Exact runtime $exactKey not found. Using fallback runtime $runtimeKey" |
| 48 | + } else { |
| 49 | + Write-Verbose "No simulator runtime found for iOS major $major" |
| 50 | + return $null |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + $runtimeDevices = $devicesByRuntime.PSObject.Properties | |
| 55 | + Where-Object { $_.Name -eq $runtimeKey } | |
| 56 | + Select-Object -ExpandProperty Value |
| 57 | + |
| 58 | + if (-not $runtimeDevices) { |
| 59 | + Write-Verbose "Runtime key $runtimeKey present but no devices listed." |
| 60 | + return $null |
| 61 | + } |
| 62 | + |
| 63 | + $usable = $runtimeDevices | Where-Object { $_.isAvailable -and $_.state -in @('Shutdown','Booted') } |
| 64 | + if (-not $usable) { |
| 65 | + Write-Verbose "No available devices in runtime $runtimeKey" |
| 66 | + return $null |
| 67 | + } |
| 68 | + |
| 69 | + $ranked = $usable | ForEach-Object { |
| 70 | + $dt = $_.deviceTypeIdentifier |
| 71 | + $weightPref = if ($preferredIndex.ContainsKey($dt)) { $preferredIndex[$dt] } else { 9999 } |
| 72 | + $weightFamily = if ($dt -match 'iPhone') { 0 } else { 1 } # prefer iPhone if not explicitly listed |
| 73 | + [PSCustomObject]@{ |
| 74 | + Device = $_ |
| 75 | + WeightPref = $weightPref |
| 76 | + WeightFamily = $weightFamily |
| 77 | + WeightBoot = if ($_.state -eq 'Booted') { 0 } else { 1 } |
| 78 | + SortName = $_.name |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + $sorted = $ranked | Sort-Object WeightPref, WeightFamily, WeightBoot, SortName |
| 83 | + $sorted | Select-Object -First 5 | ForEach-Object { |
| 84 | + Write-Verbose ("Candidate: {0} | {1} | pref={2} fam={3} bootW={4}" -f $_.Device.name, $_.Device.deviceTypeIdentifier, $_.WeightPref, $_.WeightFamily, $_.WeightBoot) |
| 85 | + } |
| 86 | + |
| 87 | + $selected = $sorted | Select-Object -First 1 |
| 88 | + if (-not $selected) { |
| 89 | + Write-Verbose "Failed to select a simulator." |
| 90 | + return $null |
| 91 | + } |
| 92 | + |
| 93 | + Write-Verbose ("Selected simulator: {0} ({1}) [{2}]" -f $selected.Device.name, $selected.Device.deviceTypeIdentifier, $selected.Device.udid) |
| 94 | + return $selected.Device.udid |
| 95 | +} |
0 commit comments