Skip to content

Commit c910aa4

Browse files
[macOS] Add PowerShell version of Configure-Xcode-Simulators script (#9496)
1 parent aa58a23 commit c910aa4

File tree

5 files changed

+80
-8
lines changed

5 files changed

+80
-8
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
################################################################################
2+
## File: Configure-Xcode-Simulators.ps1
3+
## Team: CI-Build
4+
## Desc: CHeck and remove duplicate simulators
5+
################################################################################
6+
7+
Import-Module "~/image-generation/helpers/Common.Helpers.psm1"
8+
Import-Module "~/image-generation/helpers/Xcode.Helpers.psm1"
9+
$arch = Get-Architecture
10+
$xcodeVersions = (Get-ToolsetContent).xcode.${arch}.versions
11+
12+
# Switch to each Xcode version
13+
foreach ($xcodeVersion in $xcodeVersions.link) {
14+
write-host "Switching to Xcode $xcodeVersion"
15+
Switch-Xcode -Version $XcodeVersion
16+
17+
# Make object of all simulators
18+
$devicesList = $(xcrun simctl list -j devices | ConvertFrom-Json)
19+
$devicesObject = [System.Collections.ArrayList]@()
20+
foreach ($runtime in $devicesList.devices.psobject.Properties.name) {
21+
foreach ($device in $devicesList.devices.$runtime) {
22+
$devicesObject += [PSCustomObject]@{
23+
runtime = $runtime
24+
DeviceName = $($device.name)
25+
DeviceId = $($device.udid)
26+
DeviceCreationTime = (Get-Item $HOME/Library/Developer/CoreSimulator/Devices/$($device.udid)).CreationTime
27+
}
28+
}
29+
}
30+
31+
# Remove duplicates
32+
foreach ($simRuntume in $devicesObject.runtime | Sort-Object -Unique) {
33+
[System.Collections.ArrayList]$sameRuntimeDevices = [array]$($devicesObject | Where-Object {$_.runtime -eq $simRuntume} | Sort-Object -Property DeviceName)
34+
Write-Host "///////////////////////////////////////////////////////////////////"
35+
Write-Host "// Checking for duplicates in $simRuntume "
36+
$devicesAsHashTable = $sameRuntimeDevices | Group-Object -Property DeviceName -AsHashTable -AsString
37+
foreach ($key in $devicesAsHashTable.Keys) {
38+
if ( $devicesAsHashTable[$key].count -gt 1) {
39+
Write-Host "// Duplicates for $key - $($devicesAsHashTable[$key].count)"
40+
}
41+
}
42+
Write-Host "///////////////////////////////////////////////////////////////////"
43+
for ($i = 0; $i -lt $sameRuntimeDevices.Count; $i++) {
44+
if ( [string]::IsNullOrEmpty($($sameRuntimeDevices[$i+1].DeviceName)) ){
45+
Write-Host "No more devices to compare in $simRuntume"
46+
Write-Host "-------------------------------------------------------------------"
47+
continue
48+
}
49+
Write-Host "$($sameRuntimeDevices[$i].DeviceName) - DeviceId $($sameRuntimeDevices[$i].DeviceId) comparing with"
50+
Write-Host "$($sameRuntimeDevices[$i+1].DeviceName) - DeviceId $($sameRuntimeDevices[$i+1].DeviceId)"
51+
Write-Host "-------------------------------------------------------------------"
52+
if ($sameRuntimeDevices[$i].DeviceName -eq $sameRuntimeDevices[$i+1].DeviceName) {
53+
write-host "*******************************************************************"
54+
write-host "** Duplicate found"
55+
if ($sameRuntimeDevices[$i].DeviceCreationTime -lt $sameRuntimeDevices[$i+1].DeviceCreationTime) {
56+
Write-Host "** will be removed $($sameRuntimeDevices[$i+1].DeviceName) with id $($sameRuntimeDevices[$i+1].DeviceId)"
57+
xcrun simctl delete $sameRuntimeDevices[$i+1].DeviceId
58+
$sameRuntimeDevices.RemoveAt($i+1)
59+
} else {
60+
Write-Host "** will be removed $($sameRuntimeDevices[$i].DeviceName) with id $($sameRuntimeDevices[$i].DeviceId)"
61+
xcrun simctl delete $sameRuntimeDevices[$i].DeviceId
62+
$sameRuntimeDevices.RemoveAt($i)
63+
}
64+
write-host "*******************************************************************"
65+
}
66+
}
67+
}
68+
}

images/macos/templates/macOS-13.anka.pkr.hcl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,9 @@ build {
260260
}
261261

262262
provisioner "shell" {
263-
execute_command = "source $HOME/.bash_profile; ruby {{ .Path }}"
264-
script = "${path.root}/../scripts/build/configure-xcode-simulators.rb"
263+
environment_vars = ["IMAGE_FOLDER=${local.image_folder}"]
264+
execute_command = "chmod +x {{ .Path }}; source $HOME/.bash_profile; {{ .Vars }} pwsh -f {{ .Path }}"
265+
script = "${path.root}/../scripts/build/Configure-Xcode-Simulators.ps1"
265266
}
266267

267268
provisioner "shell" {

images/macos/templates/macOS-13.arm64.anka.pkr.hcl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,9 @@ build {
255255
}
256256

257257
provisioner "shell" {
258-
execute_command = "source $HOME/.bash_profile; ruby {{ .Path }}"
259-
script = "${path.root}/../scripts/build/configure-xcode-simulators.rb"
258+
environment_vars = ["IMAGE_FOLDER=${local.image_folder}"]
259+
execute_command = "chmod +x {{ .Path }}; source $HOME/.bash_profile; {{ .Vars }} pwsh -f {{ .Path }}"
260+
script = "${path.root}/../scripts/build/Configure-Xcode-Simulators.ps1"
260261
}
261262

262263
provisioner "shell" {

images/macos/templates/macOS-14.anka.pkr.hcl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,9 @@ build {
259259
}
260260

261261
provisioner "shell" {
262-
execute_command = "source $HOME/.bash_profile; ruby {{ .Path }}"
263-
script = "${path.root}/../scripts/build/configure-xcode-simulators.rb"
262+
environment_vars = ["IMAGE_FOLDER=${local.image_folder}"]
263+
execute_command = "chmod +x {{ .Path }}; source $HOME/.bash_profile; {{ .Vars }} pwsh -f {{ .Path }}"
264+
script = "${path.root}/../scripts/build/Configure-Xcode-Simulators.ps1"
264265
}
265266

266267
provisioner "shell" {

images/macos/templates/macOS-14.arm64.anka.pkr.hcl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,9 @@ build {
255255
}
256256

257257
provisioner "shell" {
258-
execute_command = "source $HOME/.bash_profile; ruby {{ .Path }}"
259-
script = "${path.root}/../scripts/build/configure-xcode-simulators.rb"
258+
environment_vars = ["IMAGE_FOLDER=${local.image_folder}"]
259+
execute_command = "chmod +x {{ .Path }}; source $HOME/.bash_profile; {{ .Vars }} pwsh -f {{ .Path }}"
260+
script = "${path.root}/../scripts/build/Configure-Xcode-Simulators.ps1"
260261
}
261262

262263
provisioner "shell" {

0 commit comments

Comments
 (0)