diff --git a/images/macos/scripts/build/Configure-Xcode-Simulators.ps1 b/images/macos/scripts/build/Configure-Xcode-Simulators.ps1 new file mode 100644 index 000000000..d4bcd8cb7 --- /dev/null +++ b/images/macos/scripts/build/Configure-Xcode-Simulators.ps1 @@ -0,0 +1,68 @@ +################################################################################ +## File: Configure-Xcode-Simulators.ps1 +## Team: CI-Build +## Desc: CHeck and remove duplicate simulators +################################################################################ + +Import-Module "~/image-generation/helpers/Common.Helpers.psm1" +Import-Module "~/image-generation/helpers/Xcode.Helpers.psm1" +$arch = Get-Architecture +$xcodeVersions = (Get-ToolsetContent).xcode.${arch}.versions + +# Switch to each Xcode version +foreach ($xcodeVersion in $xcodeVersions.link) { + write-host "Switching to Xcode $xcodeVersion" + Switch-Xcode -Version $XcodeVersion + + # Make object of all simulators + $devicesList = $(xcrun simctl list -j devices | ConvertFrom-Json) + $devicesObject = [System.Collections.ArrayList]@() + foreach ($runtime in $devicesList.devices.psobject.Properties.name) { + foreach ($device in $devicesList.devices.$runtime) { + $devicesObject += [PSCustomObject]@{ + runtime = $runtime + DeviceName = $($device.name) + DeviceId = $($device.udid) + DeviceCreationTime = (Get-Item $HOME/Library/Developer/CoreSimulator/Devices/$($device.udid)).CreationTime + } + } + } + + # Remove duplicates + foreach ($simRuntume in $devicesObject.runtime | Sort-Object -Unique) { + [System.Collections.ArrayList]$sameRuntimeDevices = [array]$($devicesObject | Where-Object {$_.runtime -eq $simRuntume} | Sort-Object -Property DeviceName) + Write-Host "///////////////////////////////////////////////////////////////////" + Write-Host "// Checking for duplicates in $simRuntume " + $devicesAsHashTable = $sameRuntimeDevices | Group-Object -Property DeviceName -AsHashTable -AsString + foreach ($key in $devicesAsHashTable.Keys) { + if ( $devicesAsHashTable[$key].count -gt 1) { + Write-Host "// Duplicates for $key - $($devicesAsHashTable[$key].count)" + } + } + Write-Host "///////////////////////////////////////////////////////////////////" + for ($i = 0; $i -lt $sameRuntimeDevices.Count; $i++) { + if ( [string]::IsNullOrEmpty($($sameRuntimeDevices[$i+1].DeviceName)) ){ + Write-Host "No more devices to compare in $simRuntume" + Write-Host "-------------------------------------------------------------------" + continue + } + Write-Host "$($sameRuntimeDevices[$i].DeviceName) - DeviceId $($sameRuntimeDevices[$i].DeviceId) comparing with" + Write-Host "$($sameRuntimeDevices[$i+1].DeviceName) - DeviceId $($sameRuntimeDevices[$i+1].DeviceId)" + Write-Host "-------------------------------------------------------------------" + if ($sameRuntimeDevices[$i].DeviceName -eq $sameRuntimeDevices[$i+1].DeviceName) { + write-host "*******************************************************************" + write-host "** Duplicate found" + if ($sameRuntimeDevices[$i].DeviceCreationTime -lt $sameRuntimeDevices[$i+1].DeviceCreationTime) { + Write-Host "** will be removed $($sameRuntimeDevices[$i+1].DeviceName) with id $($sameRuntimeDevices[$i+1].DeviceId)" + xcrun simctl delete $sameRuntimeDevices[$i+1].DeviceId + $sameRuntimeDevices.RemoveAt($i+1) + } else { + Write-Host "** will be removed $($sameRuntimeDevices[$i].DeviceName) with id $($sameRuntimeDevices[$i].DeviceId)" + xcrun simctl delete $sameRuntimeDevices[$i].DeviceId + $sameRuntimeDevices.RemoveAt($i) + } + write-host "*******************************************************************" + } + } + } +} diff --git a/images/macos/templates/macOS-13.anka.pkr.hcl b/images/macos/templates/macOS-13.anka.pkr.hcl index 9039d4d7a..ee57ed83d 100644 --- a/images/macos/templates/macOS-13.anka.pkr.hcl +++ b/images/macos/templates/macOS-13.anka.pkr.hcl @@ -260,8 +260,9 @@ build { } provisioner "shell" { - execute_command = "source $HOME/.bash_profile; ruby {{ .Path }}" - script = "${path.root}/../scripts/build/configure-xcode-simulators.rb" + environment_vars = ["IMAGE_FOLDER=${local.image_folder}"] + execute_command = "chmod +x {{ .Path }}; source $HOME/.bash_profile; {{ .Vars }} pwsh -f {{ .Path }}" + script = "${path.root}/../scripts/build/Configure-Xcode-Simulators.ps1" } provisioner "shell" { diff --git a/images/macos/templates/macOS-13.arm64.anka.pkr.hcl b/images/macos/templates/macOS-13.arm64.anka.pkr.hcl index 5c12e8960..be53d27a6 100644 --- a/images/macos/templates/macOS-13.arm64.anka.pkr.hcl +++ b/images/macos/templates/macOS-13.arm64.anka.pkr.hcl @@ -255,8 +255,9 @@ build { } provisioner "shell" { - execute_command = "source $HOME/.bash_profile; ruby {{ .Path }}" - script = "${path.root}/../scripts/build/configure-xcode-simulators.rb" + environment_vars = ["IMAGE_FOLDER=${local.image_folder}"] + execute_command = "chmod +x {{ .Path }}; source $HOME/.bash_profile; {{ .Vars }} pwsh -f {{ .Path }}" + script = "${path.root}/../scripts/build/Configure-Xcode-Simulators.ps1" } provisioner "shell" { diff --git a/images/macos/templates/macOS-14.anka.pkr.hcl b/images/macos/templates/macOS-14.anka.pkr.hcl index 015d306f1..ed93c149e 100644 --- a/images/macos/templates/macOS-14.anka.pkr.hcl +++ b/images/macos/templates/macOS-14.anka.pkr.hcl @@ -259,8 +259,9 @@ build { } provisioner "shell" { - execute_command = "source $HOME/.bash_profile; ruby {{ .Path }}" - script = "${path.root}/../scripts/build/configure-xcode-simulators.rb" + environment_vars = ["IMAGE_FOLDER=${local.image_folder}"] + execute_command = "chmod +x {{ .Path }}; source $HOME/.bash_profile; {{ .Vars }} pwsh -f {{ .Path }}" + script = "${path.root}/../scripts/build/Configure-Xcode-Simulators.ps1" } provisioner "shell" { diff --git a/images/macos/templates/macOS-14.arm64.anka.pkr.hcl b/images/macos/templates/macOS-14.arm64.anka.pkr.hcl index 85ea62da6..693fbae36 100644 --- a/images/macos/templates/macOS-14.arm64.anka.pkr.hcl +++ b/images/macos/templates/macOS-14.arm64.anka.pkr.hcl @@ -255,8 +255,9 @@ build { } provisioner "shell" { - execute_command = "source $HOME/.bash_profile; ruby {{ .Path }}" - script = "${path.root}/../scripts/build/configure-xcode-simulators.rb" + environment_vars = ["IMAGE_FOLDER=${local.image_folder}"] + execute_command = "chmod +x {{ .Path }}; source $HOME/.bash_profile; {{ .Vars }} pwsh -f {{ .Path }}" + script = "${path.root}/../scripts/build/Configure-Xcode-Simulators.ps1" } provisioner "shell" {