|
| 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 | +} |
0 commit comments