Skip to content

Commit d748ff7

Browse files
committed
Extract cross-platform editor discovery and use to cull paths for unity setup instances
1 parent 8046ebf commit d748ff7

File tree

2 files changed

+61
-25
lines changed

2 files changed

+61
-25
lines changed

UnitySetup/UnitySetup.psd1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
'Install-UnitySetupInstance',
8585
'Select-UnitySetupInstance',
8686
'Uninstall-UnitySetupInstance',
87+
'Get-UnityEditor',
8788
'Start-UnityEditor',
8889
'ConvertTo-UnitySetupComponent',
8990
'Get-UnityLicense'
@@ -100,6 +101,7 @@
100101
'gusi',
101102
'gupi',
102103
'susi',
104+
'gue',
103105
'sue'
104106
)
105107

UnitySetup/UnitySetup.psm1

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,56 @@ function Get-OperatingSystem {
232232
}
233233
}
234234

235+
<#
236+
.Synopsis
237+
Get the Unity Editor application
238+
.PARAMETER Path
239+
Path of a UnitySetupInstance
240+
.EXAMPLE
241+
Get-UnityEditor -Path $unitySetupInstance.Path
242+
#>
243+
function Get-UnityEditor {
244+
[CmdletBinding()]
245+
param(
246+
[ValidateScript( {Test-Path $_ -PathType Container} )]
247+
[Parameter(Mandatory = $false, ValueFromPipeline = $true, Position = 0, ParameterSetName = "Path")]
248+
[string[]]$Path = $PWD,
249+
250+
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0, ParameterSetName = "Instance")]
251+
[ValidateNotNull()]
252+
[UnitySetupInstance[]]$Instance
253+
)
254+
255+
process {
256+
257+
if ( $PSCmdlet.ParameterSetName -eq "Instance" ) {
258+
$Path = $Instance.Path
259+
}
260+
261+
$currentOS = Get-OperatingSystem
262+
foreach ($p in $Path) {
263+
switch ($currentOS) {
264+
([OperatingSystem]::Windows) {
265+
$editor = Get-ChildItem "$p" -Filter 'Unity.exe' -Recurse |
266+
Select-Object -First 1 -ExpandProperty FullName
267+
268+
Write-Output $editor
269+
}
270+
([OperatingSystem]::Linux) {
271+
throw "Get-UnityEditor has not been implemented on the Linux platform. Contributions welcomed!";
272+
}
273+
([OperatingSystem]::Mac) {
274+
$editor = Join-Path "$p" "Unity.app/Contents/MacOS/Unity"
275+
276+
if (Test-Path $editor) {
277+
Write-Output (Resolve-Path $editor).Path
278+
}
279+
}
280+
}
281+
}
282+
}
283+
}
284+
235285
<#
236286
.Synopsis
237287
Help to create UnitySetupComponent
@@ -1088,9 +1138,10 @@ function Get-UnitySetupInstance {
10881138
}
10891139
}
10901140

1091-
Get-ChildItem $BasePath -Directory | Where-Object {
1092-
Test-Path (Join-Path $_.FullName 'Editor\Unity.exe') -PathType Leaf
1093-
} | ForEach-Object {
1141+
1142+
$BasePath | Where-Object { Test-Path $_ -PathType Container } |
1143+
Get-ChildItem -Directory | Where-Object { (Get-UnityEditor $_.FullName).Count -gt 0 } |
1144+
ForEach-Object {
10941145
$path = $_.FullName
10951146
try {
10961147
Write-Verbose "Creating UnitySetupInstance for $path"
@@ -1528,32 +1579,14 @@ function Start-UnityEditor {
15281579
$setupInstances += , $setupInstance
15291580
}
15301581

1531-
$currentOS = Get-OperatingSystem
15321582

15331583
for ($i = 0; $i -lt $setupInstances.Length; $i++) {
15341584
$setupInstance = $setupInstances[$i]
15351585

1536-
switch ($currentOS) {
1537-
([OperatingSystem]::Windows) {
1538-
$editor = Get-ChildItem "$($setupInstance.Path)" -Filter 'Unity.exe' -Recurse |
1539-
Select-Object -First 1 -ExpandProperty FullName
1540-
1541-
if ([string]::IsNullOrEmpty($editor)) {
1542-
Write-Error "Could not find Unity.exe under setup instance path: $($setupInstance.Path)"
1543-
continue
1544-
}
1545-
}
1546-
([OperatingSystem]::Linux) {
1547-
throw "Start-UnityEditor has not been implemented on the Linux platform. Contributions welcomed!";
1548-
}
1549-
([OperatingSystem]::Mac) {
1550-
$editor = [io.path]::Combine("$($setupInstance.Path)", "Unity.app/Contents/MacOS/Unity")
1551-
1552-
if ([string]::IsNullOrEmpty($editor)) {
1553-
Write-Error "Could not find Unity app under setup instance path: $($setupInstance.Path)"
1554-
continue
1555-
}
1556-
}
1586+
$editor = Get-UnityEditor "$($setupInstance.Path)"
1587+
if ( -not $editor ) {
1588+
Write-Error "Could not find Unity Editor under setup instance path: $($setupInstance.Path)"
1589+
continue
15571590
}
15581591

15591592
# clone the shared args list
@@ -1695,6 +1728,7 @@ function Get-UnityLicense {
16951728
@{ 'Name' = 'gusi'; 'Value' = 'Get-UnitySetupInstance' },
16961729
@{ 'Name' = 'gupi'; 'Value' = 'Get-UnityProjectInstance' },
16971730
@{ 'Name' = 'susi'; 'Value' = 'Select-UnitySetupInstance' },
1731+
@{ 'Name' = 'gue'; 'Value' = 'Get-UnityEditor' }
16981732
@{ 'Name' = 'sue'; 'Value' = 'Start-UnityEditor' }
16991733
) | ForEach-Object {
17001734

0 commit comments

Comments
 (0)