Skip to content

Commit 5ea07b5

Browse files
Create Xcode.psm1
1 parent 0d25e54 commit 5ea07b5

1 file changed

Lines changed: 240 additions & 0 deletions

File tree

helpers/Doc-gen/Xcode.psm1

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
Import-Module "$PSScriptRoot/../helpers/Common.Helpers.psm1"
2+
Import-Module "$PSScriptRoot/../helpers/Xcode.Helpers.psm1"
3+
4+
function Get-XcodePaths {
5+
$xcodePaths = Get-ChildItem -Path "/Applications" -Filter "Xcode_*.app" | Where-Object { !$_.LinkType }
6+
return $xcodePaths | Select-Object -ExpandProperty Fullname
7+
}
8+
9+
function Get-XcodeSDKList {
10+
param(
11+
[Parameter(Mandatory)]
12+
[string]$XcodeRootPath
13+
)
14+
15+
$versionInfo = Get-XcodeVersionInfo -XcodeRootPath $XcodeRootPath
16+
$xcodebuildPath = Get-XcodeToolPath -XcodeRootPath $XcodeRootPath -ToolName "xcodebuild"
17+
if ($versionInfo.Version -le [System.Version]::Parse("9.4.1")) {
18+
$output = Invoke-Expression "$xcodebuildPath -showsdks"
19+
$sdkList = $output | Where-Object { $_ -Match "-sdk" }
20+
21+
return $sdkList | ForEach-Object {
22+
$displayName, $canonicalName = $_.Split("-sdk")
23+
return @{
24+
canonicalName = $canonicalName.Trim()
25+
displayName = $displayName.Trim()
26+
}
27+
}
28+
}
29+
30+
[string]$output = Invoke-Expression "$xcodebuildPath -showsdks -json"
31+
return $output | ConvertFrom-Json
32+
}
33+
34+
function Get-XcodeInfoList {
35+
$defaultXcodeRootPath = Get-DefaultXcodeRootPath
36+
37+
$xcodeInfo = @{}
38+
Get-XcodePaths | ForEach-Object {
39+
$xcodeRootPath = $_
40+
Switch-Xcode -XcodeRootPath $xcodeRootPath
41+
42+
$versionInfo = Get-XcodeVersionInfo -XcodeRootPath $xcodeRootPath
43+
$versionInfo.Path = $xcodeRootPath
44+
$versionInfo.IsDefault = ($xcodeRootPath -eq $defaultXcodeRootPath)
45+
$versionInfo.IsStable = Test-XcodeStableRelease -XcodeRootPath $xcodeRootPath
46+
47+
$xcodeInfo.Add($xcodeRootPath, [PSCustomObject] @{
48+
VersionInfo = $versionInfo
49+
SDKInfo = Get-XcodeSDKList -XcodeRootPath $xcodeRootPath
50+
SimulatorsInfo = Get-XcodeSimulatorsInfo
51+
})
52+
}
53+
54+
Switch-Xcode -XcodeRootPath $defaultXcodeRootPath
55+
56+
return $xcodeInfo
57+
}
58+
59+
function Get-XcodePlatformOrder {
60+
param (
61+
[Parameter(Mandatory)]
62+
[string] $PlatformName
63+
)
64+
65+
Switch ($PlatformName) {
66+
"macOS" { 1 }
67+
"iOS" { 2 }
68+
"Simulator - iOS" { 3 }
69+
"tvOS" { 4 }
70+
"Simulator - tvOS" { 5 }
71+
"watchOS" { 6 }
72+
"Simulator - watchOS" { 7 }
73+
"visionOS" { 8 }
74+
"Simulator - visionOS" { 9 }
75+
Default { 100 }
76+
}
77+
}
78+
79+
function Get-XcodeCommandLineToolsVersion {
80+
$xcodeCommandLineToolsVersion = Run-Command "pkgutil --pkg-info com.apple.pkg.CLTools_Executables" | Select -Index 1 | Take-Part -Part 1
81+
return $xcodeCommandLineToolsVersion
82+
}
83+
84+
function Build-XcodeTable {
85+
param (
86+
[Parameter(Mandatory)]
87+
[hashtable] $xcodeInfo
88+
)
89+
90+
$sortRules = @{
91+
Expression = { $_.Version }
92+
Descending = $true
93+
}
94+
95+
$xcodeList = $xcodeInfo.Values | ForEach-Object { $_.VersionInfo } | Sort-Object $sortRules
96+
return $xcodeList | ForEach-Object {
97+
$defaultPostfix = if ($_.IsDefault) { " (default)" } else { "" }
98+
$betaPostfix = if ($_.IsStable) { "" } else { " (beta)" }
99+
$targetPath = $_.Path
100+
$symlinks = @()
101+
Get-ChildItem -Path "/Applications" | ForEach-Object {
102+
if ($_.LinkType -eq 'SymbolicLink') {
103+
$linkTarget = & readlink $_.FullName
104+
if ($linkTarget -eq $targetPath) {
105+
$symlinks += $_.FullName
106+
}
107+
}
108+
}
109+
if ($null -eq $symlinks) {
110+
$symlinks = @("N/A")
111+
}
112+
return [PSCustomObject] @{
113+
"Version" = $_.Version.ToString() + $betaPostfix + $defaultPostfix
114+
"Build" = $_.Build
115+
"Path" = $_.Path
116+
"Symlinks" = [String]::Join("<br>", $symlinks)
117+
}
118+
}
119+
}
120+
121+
function Build-XcodeDevicesList {
122+
param (
123+
[Parameter(Mandatory)][object] $XcodeInfo,
124+
[Parameter(Mandatory)][object] $Runtime
125+
)
126+
127+
$runtimeId = $Runtime.identifier
128+
$runtimeName = $Runtime.name
129+
$output = $XcodeInfo.SimulatorsInfo.devices.$runtimeId
130+
if ($null -eq $output) {
131+
$output = $XcodeInfo.SimulatorsInfo.devices.$runtimeName
132+
}
133+
134+
return $output
135+
}
136+
137+
function Build-XcodeSDKTable {
138+
param (
139+
[Parameter(Mandatory)]
140+
[hashtable] $xcodeInfo
141+
)
142+
143+
$sdkNames = @()
144+
$xcodeInfo.Values | ForEach-Object {
145+
$_.SDKInfo | ForEach-Object {
146+
$sdkNames += $_.canonicalName
147+
}
148+
}
149+
150+
$sdkNames = $sdkNames | Select-Object -Unique
151+
return $sdkNames | ForEach-Object {
152+
$sdkName = $_
153+
$sdkDisplayName = ""
154+
$xcodeList = @()
155+
$xcodeInfo.Values | ForEach-Object {
156+
$sdk = $_.SDKInfo | Where-Object { $_.canonicalName -eq $sdkName } | Select-Object -First 1
157+
if ($sdk) {
158+
$sdkDisplayName = $sdk.displayName
159+
$xcodeList += $_.VersionInfo.Version
160+
}
161+
}
162+
163+
$xcodeList = $xcodeList | Sort-Object
164+
165+
return [PSCustomObject] @{
166+
"SDK" = $sdkDisplayName
167+
"SDK Name" = $sdkName
168+
"Xcode Version" = [String]::Join(", ", $xcodeList)
169+
}
170+
} | Sort-Object {
171+
# Sort rule 1
172+
$sdkNameParts = $_."SDK".Split(" ")
173+
$platformName = [String]::Join(" ", $sdkNameParts[0..($sdkNameParts.Length - 2)])
174+
return Get-XcodePlatformOrder $platformName
175+
}, {
176+
# Sort rule 2
177+
$sdkNameParts = $_."SDK".Split(" ")
178+
return [System.Version]::Parse($sdkNameParts[-1])
179+
}
180+
}
181+
182+
function Format-XcodeSimulatorName {
183+
param(
184+
[Parameter(Mandatory)][string] $Device
185+
)
186+
187+
$formattedDeviceName = $Device.Replace("ʀ", "R")
188+
return $formattedDeviceName
189+
}
190+
191+
function Build-XcodeSimulatorsTable {
192+
param (
193+
[Parameter(Mandatory)]
194+
[hashtable] $xcodeInfo
195+
)
196+
197+
$runtimes = @()
198+
$xcodeInfo.Values | ForEach-Object {
199+
$_.SimulatorsInfo.runtimes | ForEach-Object {
200+
$runtimes += $_
201+
}
202+
}
203+
$runtimes = $runtimes | Sort-Object @{ Expression = { $_.identifier } } -Unique
204+
return $runtimes | ForEach-Object {
205+
$runtime = $_
206+
$runtimeDevices = @()
207+
$xcodeInfo.Values | ForEach-Object {
208+
$runtimeFound = $_.SimulatorsInfo.runtimes | Where-Object { $_.identifier -eq $runtime.identifier } | Select-Object -First 1
209+
if ($runtimeFound) {
210+
$devicesToAdd = Build-XcodeDevicesList -XcodeInfo $_ -Runtime $runtimeFound
211+
$runtimeDevices += $devicesToAdd | Select-Object -ExpandProperty name
212+
}
213+
}
214+
$runtimeDevices = $runtimeDevices | ForEach-Object { Format-XcodeSimulatorName $_ } | Select-Object -Unique
215+
If (($runtimeDevices | Where-Object { -not ([string]::IsNullOrWhitespace($_)) }).Count -eq 0) {
216+
$sortedRuntimeDevices = @("N/A")
217+
} else {
218+
$sortedRuntimeDevices = $runtimeDevices | Sort-Object @{
219+
Expression = { $_.Split(" ")[0] };
220+
Descending = $true;
221+
}, {
222+
$_.Split(" ") | Select-Object -Skip 1 | Join-String -Separator " "
223+
}
224+
}
225+
return [PSCustomObject] @{
226+
"Name" = $runtime.name
227+
"OS" = $runtime.version
228+
"Simulators" = [String]::Join("<br>", $sortedRuntimeDevices)
229+
}
230+
} | Sort-Object {
231+
# Sort rule 1
232+
$sdkNameParts = $_."Name".Split(" ")
233+
$platformName = [String]::Join(" ", $sdkNameParts[0..($sdkNameParts.Length - 2)])
234+
return Get-XcodePlatformOrder $platformName
235+
}, {
236+
# Sort rule 2
237+
$sdkNameParts = $_."Name".Split(" ")
238+
return [System.Version]::Parse($sdkNameParts[-1])
239+
}
240+
}

0 commit comments

Comments
 (0)