Skip to content

Commit 29ec25b

Browse files
Refactor into a separate module/function
1 parent 787e45a commit 29ec25b

File tree

2 files changed

+100
-85
lines changed

2 files changed

+100
-85
lines changed

scripts/device-test.ps1

Lines changed: 5 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ param(
1111

1212
Set-StrictMode -Version latest
1313
$ErrorActionPreference = 'Stop'
14+
. $PSScriptRoot/ios-simulator-utils.ps1
1415

1516
if (!$Build -and !$Run)
1617
{
@@ -60,93 +61,12 @@ try
6061
'--set-env', "CI=$envValue"
6162
)
6263

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)
8667
} 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"
11169
}
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)
15070
}
15171

15272
if ($Build)

scripts/ios-simulator-utils.ps1

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)