Skip to content

Commit 519d1a9

Browse files
LVL DevLVL Dev
authored andcommitted
Fixes Start-UnityEditor on Mac OS.
1 parent e624b8e commit 519d1a9

File tree

1 file changed

+68
-54
lines changed

1 file changed

+68
-54
lines changed

UnitySetup/UnitySetup.psm1

Lines changed: 68 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Licensed under the MIT License.
33
Import-Module powershell-yaml -MinimumVersion '0.3' -ErrorAction Stop
44

5-
[Flags()]
5+
[Flags()]
66
enum UnitySetupComponent {
77
Setup = (1 -shl 0)
88
Documentation = (1 -shl 1)
@@ -18,6 +18,7 @@ enum UnitySetupComponent {
1818
Mac = (1 -shl 11)
1919
Vuforia = (1 -shl 12)
2020
WebGL = (1 -shl 13)
21+
Windows = (1 -shl 14)
2122
All = (1 -shl 14) - 1
2223
}
2324

@@ -33,7 +34,7 @@ class UnitySetupInstance {
3334
[UnityVersion]$Version
3435
[UnitySetupComponent]$Components
3536
[string]$Path
36-
37+
3738
UnitySetupInstance([string]$path) {
3839

3940
# Windows
@@ -44,14 +45,14 @@ class UnitySetupInstance {
4445
else {
4546
$ivyPath = 'Unity.app/Contents/UnityExtensions/Unity/Networking/ivy.xml'
4647
}
47-
48+
4849
$ivyPath = [io.path]::Combine("$path", $ivyPath);
4950
if (!(Test-Path $ivyPath)) { throw "Path is not a Unity setup: $path"}
5051
[xml]$xmlDoc = Get-Content $ivyPath
5152

5253
if ( !($xmlDoc.'ivy-module'.info.unityVersion)) {
5354
throw "Unity setup ivy is missing version: $ivyPath"
54-
}
55+
}
5556

5657
$this.Path = $path
5758
$this.Version = $xmlDoc.'ivy-module'.info.unityVersion
@@ -105,16 +106,16 @@ class UnitySetupInstance {
105106
}
106107

107108
class UnityProjectInstance {
108-
[UnityVersion]$Version
109+
[UnityVersion]$Version
109110
[string]$Path
110-
111+
111112
UnityProjectInstance([string]$path) {
112113
$versionFile = [io.path]::Combine($path, "ProjectSettings\ProjectVersion.txt")
113114
if (!(Test-Path $versionFile)) { throw "Path is not a Unity project: $path"}
114115

115116
$fileVersion = (Get-Content $versionFile -Raw | ConvertFrom-Yaml)['m_EditorVersion'];
116117
if (!$fileVersion) { throw "Project is missing a version in: $versionFile"}
117-
118+
118119
$this.Path = $path
119120
$this.Version = $fileVersion
120121
}
@@ -138,13 +139,13 @@ class UnityVersion : System.IComparable {
138139
$parts = $version.Split('-')
139140

140141
$parts[0] -match "(\d+)\.(\d+)\.(\d+)([fpb])(\d+)" | Out-Null
141-
if ( $Matches.Count -ne 6 ) { throw "Invalid unity version: $version" }
142+
if ( $Matches.Count -ne 6 ) { throw "Invalid unity version: $version" }
142143
$this.Major = [int]($Matches[1]);
143144
$this.Minor = [int]($Matches[2]);
144145
$this.Revision = [int]($Matches[3]);
145146
$this.Release = [char]($Matches[4]);
146147
$this.Build = [int]($Matches[5]);
147-
148+
148149
if ($parts.Length -gt 1) {
149150
$this.Suffix = $parts[1];
150151
}
@@ -153,20 +154,20 @@ class UnityVersion : System.IComparable {
153154
[int] CompareTo([object]$obj) {
154155
if ($null -eq $obj) { return 1 }
155156
if ($obj -isnot [UnityVersion]) { throw "Object is not a UnityVersion"}
156-
157+
157158
return [UnityVersion]::Compare($this, $obj)
158159
}
159160

160161
static [int] Compare([UnityVersion]$a, [UnityVersion]$b) {
161162
if ($a.Major -lt $b.Major) { return -1 }
162163
if ($a.Major -gt $b.Major) { return 1 }
163-
164+
164165
if ($a.Minor -lt $b.Minor) { return -1 }
165166
if ($a.Minor -gt $b.Minor) { return 1 }
166-
167+
167168
if ($a.Revision -lt $b.Revision) { return -1 }
168169
if ($a.Revision -gt $b.Revision) { return 1 }
169-
170+
170171
if ($a.Release -lt $b.Release) { return -1 }
171172
if ($a.Release -gt $b.Release) { return 1 }
172173

@@ -182,7 +183,7 @@ class UnityVersion : System.IComparable {
182183

183184
<#
184185
.Synopsis
185-
Help to create UnitySetupComponent
186+
Help to create UnitySetupComponent
186187
.PARAMETER Components
187188
What components would you like included?
188189
.EXAMPLE
@@ -210,7 +211,7 @@ function ConvertTo-UnitySetupComponent {
210211
.EXAMPLE
211212
Find-UnitySetupInstaller -Version 2017.3.0f3
212213
.EXAMPLE
213-
Find-UnitySetupInstaller -Version 2017.3.0f3 -Components Setup,Documentation
214+
Find-UnitySetupInstaller -Version 2017.3.0f3 -Components Setup,Documentation
214215
#>
215216
function Find-UnitySetupInstaller {
216217
[CmdletBinding()]
@@ -263,8 +264,8 @@ function Find-UnitySetupInstaller {
263264
$patchPage = "https://unity3d.com/unity/qa/patch-releases?version=$($Version.Major).$($Version.Minor)"
264265
$searchPages += $patchPage
265266

266-
$webResult = Invoke-WebRequest $patchPage -UseBasicParsing
267-
$searchPages += $webResult.Links | Where-Object {
267+
$webResult = Invoke-WebRequest $patchPage -UseBasicParsing
268+
$searchPages += $webResult.Links | Where-Object {
268269
$_.href -match "\/unity\/qa\/patch-releases\?version=$($Version.Major)\.$($Version.Minor)&amp;page=(\d+)" -and $Matches[1] -gt 1
269270
} | ForEach-Object {
270271
"https://unity3d.com/unity/qa/patch-releases?version=$($Version.Major).$($Version.Minor)&page=$($Matches[1])"
@@ -274,13 +275,13 @@ function Find-UnitySetupInstaller {
274275

275276
foreach ($page in $searchPages) {
276277
$webResult = Invoke-WebRequest $page -UseBasicParsing
277-
$prototypeLink = $webResult.Links | Select-Object -ExpandProperty href -ErrorAction SilentlyContinue | Where-Object {
278-
$_ -match "$($installerTemplates[[UnitySetupComponent]::Setup])$"
278+
$prototypeLink = $webResult.Links | Select-Object -ExpandProperty href -ErrorAction SilentlyContinue | Where-Object {
279+
$_ -match "$($installerTemplates[[UnitySetupComponent]::Setup])$"
279280
}
280281

281282
if ($null -ne $prototypeLink) { break }
282283
}
283-
284+
284285
if ($null -eq $prototypeLink) {
285286
throw "Could not find archives for Unity version $Version"
286287
}
@@ -293,7 +294,7 @@ function Find-UnitySetupInstaller {
293294
else {
294295
$knownBaseUrls = $knownBaseUrls | Sort-Object -Property @{ Expression = {[math]::Abs(($_.CompareTo($linkComponents[0])))}; Ascending = $true}
295296
}
296-
297+
297298
$installerTemplates.Keys | Where-Object { $Components -band $_ } | ForEach-Object {
298299
$templates = $installerTemplates.Item($_);
299300
$result = $null
@@ -383,7 +384,7 @@ function Install-UnitySetupInstance {
383384
$localDestinations += , "C:\Program Files\Unity-$($i.Version)"
384385
}
385386

386-
if ( Test-Path $destPath ) {
387+
if ( Test-Path $destPath ) {
387388
$destItem = Get-Item $destPath
388389
if ( ($destItem.Length -eq $i.Length ) -and ($destItem.LastWriteTime -eq $i.LastModified) ) {
389390
Write-Verbose "Skipping download because it's already in the cache: $($i.DownloadUrl)"
@@ -403,10 +404,10 @@ function Install-UnitySetupInstance {
403404
New-Item "$destDirectory" -ItemType Directory | Out-Null
404405
}
405406
}
406-
407+
407408
Start-BitsTransfer -Source $downloadSource -Destination $downloadDest
408409
}
409-
410+
410411
$spins = @('|', '/', '-', '\')
411412
for ($i = 0; $i -lt $localInstallers.Length; $i++) {
412413
$installer = $localInstallers[$i]
@@ -421,7 +422,7 @@ function Install-UnitySetupInstance {
421422
if ($Verb) {
422423
$startProcessArgs['Verb'] = $Verb
423424
}
424-
425+
425426
$spinnerIndex = 0
426427
$process = Start-Process @startProcessArgs
427428
while (!$process.HasExited) {
@@ -433,7 +434,7 @@ function Install-UnitySetupInstance {
433434
Write-Host "`bFailed."
434435
Write-Error "Installing $installer failed with exit code: $($process.ExitCode)"
435436
}
436-
else {
437+
else {
437438
Write-Host "`bSucceeded."
438439
}
439440
}
@@ -444,7 +445,7 @@ function Install-UnitySetupInstance {
444445
.Synopsis
445446
Uninstall Unity Setup Instances
446447
.DESCRIPTION
447-
Uninstall the specified Unity Setup Instances
448+
Uninstall the specified Unity Setup Instances
448449
.PARAMETER Instance
449450
What instances of UnitySetup should be uninstalled
450451
.EXAMPLE
@@ -462,7 +463,7 @@ function Uninstall-UnitySetupInstance {
462463
$uninstaller = Get-ChildItem "$($setupInstance.Path)" -Filter 'Uninstall.exe' -Recurse |
463464
Select-Object -First 1 -ExpandProperty FullName
464465

465-
if ($null -eq $uninstaller) {
466+
if ($null -eq $uninstaller) {
466467
Write-Error "Could not find Uninstaller.exe under $($setupInstance.Path)"
467468
continue
468469
}
@@ -520,7 +521,7 @@ function Get-UnitySetupInstance {
520521
foreach ( $folder in $BasePath ) {
521522
$path = [io.path]::Combine("$folder", $ivyPath);
522523

523-
Get-ChildItem $path -Recurse -ErrorAction Ignore |
524+
Get-ChildItem $path -Recurse -ErrorAction Ignore |
524525
ForEach-Object {
525526
[UnitySetupInstance]::new((Join-Path $_.Directory "..\..\..\..\..\" | Convert-Path))
526527
}
@@ -557,17 +558,17 @@ function Select-UnitySetupInstance {
557558
[parameter(Mandatory = $true, ValueFromPipeline = $true)]
558559
[UnitySetupInstance[]] $Instances
559560
)
560-
561+
561562
process {
562-
if ( $Version ) {
563+
if ( $Version ) {
563564
$Instances = $Instances | Where-Object { [UnityVersion]::Compare($_.Version, $Version) -eq 0 }
564565
}
565566

566567
if ( $Latest ) {
567-
foreach ( $i in $Instances ) {
568-
if ( $null -eq $latestInstance -or [UnityVersion]::Compare($i.Version, $latestInstance.Version) -gt 0) {
568+
foreach ( $i in $Instances ) {
569+
if ( $null -eq $latestInstance -or [UnityVersion]::Compare($i.Version, $latestInstance.Version) -gt 0) {
569570
$latestInstance = $i
570-
}
571+
}
571572
}
572573
}
573574
elseif ( $Instances.Count -gt 0 ) { $Instances }
@@ -583,7 +584,7 @@ function Select-UnitySetupInstance {
583584
.DESCRIPTION
584585
Recursively discovers Unity projects and their Unity version
585586
.PARAMETER BasePath
586-
Under what base pattern should we look for Unity projects? Defaults to '$PWD'.
587+
Under what base pattern should we look for Unity projects? Defaults to '$PWD'.
587588
.EXAMPLE
588589
Get-UnityProjectInstance
589590
.EXAMPLE
@@ -708,7 +709,7 @@ function Start-UnityEditor {
708709
[parameter(Mandatory = $false)]
709710
[switch]$PassThru
710711
)
711-
process {
712+
process {
712713
switch -wildcard ( $PSCmdlet.ParameterSetName ) {
713714
'Context' {
714715
$projectInstances = [UnityProjectInstance[]]@()
@@ -725,17 +726,17 @@ function Start-UnityEditor {
725726
}
726727
}
727728
}
728-
'Projects*' {
729+
'Projects*' {
729730
$projectInstances = $Project
730731
$setupInstances = [UnitySetupInstance[]]@()
731732
}
732-
'Setups' {
733+
'Setups' {
733734
$projectInstances = [UnityProjectInstance[]]@()
734735
$setupInstances = $Setup
735736
}
736737
'Latest' {
737738
$projectInstances = [UnityProjectInstance[]]@()
738-
739+
739740
$currentFolderProject = if (!$IgnoreProjectContext) { Get-UnityProjectInstance $PWD.Path }
740741
if ($null -ne $currentFolderProject) {
741742
$projectInstances += , $currentFolderProject
@@ -778,8 +779,8 @@ function Start-UnityEditor {
778779
if ( $ImportPackage ) { $sharedArgs += "-importPackage", "$ImportPackage" }
779780

780781
$instanceArgs = @()
781-
foreach ( $p in $projectInstances ) {
782-
782+
foreach ( $p in $projectInstances ) {
783+
783784
if ( $Latest ) {
784785
$setupInstance = Get-UnitySetupInstance | Select-UnitySetupInstance -Latest
785786
if ($setupInstance.Count -eq 0) {
@@ -794,29 +795,42 @@ function Start-UnityEditor {
794795
continue
795796
}
796797
}
797-
else {
798+
else {
798799
$setupInstance = Get-UnitySetupInstance | Select-UnitySetupInstance -Version $p.Version
799800
if ($setupInstance.Count -eq 0) {
800801
Write-Error "Could not find Unity Editor for version $($p.Version)"
801802
continue
802803
}
803804
}
804-
805+
805806
$projectPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($($p.Path))
806807
$instanceArgs += , ("-projectPath", $projectPath)
807808
$setupInstances += , $setupInstance
808809
}
809810

810811
for ($i = 0; $i -lt $setupInstances.Length; $i++) {
811812
$setupInstance = $setupInstances[$i]
812-
$editor = Get-ChildItem "$($setupInstance.Path)" -Filter 'Unity.exe' -Recurse |
813-
Select-Object -First 1 -ExpandProperty FullName
814813

815-
if ([string]::IsNullOrEmpty($editor)) {
816-
Write-Error "Could not find Unity.exe under setup instance path: $($setupInstance.Path)"
817-
continue
814+
# Windows
815+
if ((-not $PSVersionTable.Platform) -or ($PSVersionTable.Platform -eq "Win32NT")) {
816+
$editor = Get-ChildItem "$($setupInstance.Path)" -Filter 'Unity.exe' -Recurse |
817+
Select-Object -First 1 -ExpandProperty FullName
818+
819+
if ([string]::IsNullOrEmpty($editor)) {
820+
Write-Error "Could not find Unity.exe under setup instance path: $($setupInstance.Path)"
821+
continue
822+
}
823+
}
824+
# Mac or Linux
825+
else {
826+
$editor = [io.path]::Combine("$($setupInstance.Path)", "Unity.app/Contents/MacOS/Unity")
827+
828+
if ([string]::IsNullOrEmpty($editor)) {
829+
Write-Error "Could not find Unity app under setup instance path: $($setupInstance.Path)"
830+
continue
831+
}
818832
}
819-
833+
820834
# clone the shared args list
821835
$unityArgs = $sharedArgs | ForEach-Object { $_ }
822836
if ( $instanceArgs[$i] ) { $unityArgs += $instanceArgs[$i] }
@@ -855,16 +869,16 @@ function Start-UnityEditor {
855869
@{ 'Name' = 'gusi'; 'Value' = 'Get-UnitySetupInstance' },
856870
@{ 'Name' = 'gupi'; 'Value' = 'Get-UnityProjectInstance' },
857871
@{ 'Name' = 'susi'; 'Value' = 'Select-UnitySetupInstance' },
858-
@{ 'Name' = 'sue'; 'Value' = 'Start-UnityEditor' }
859-
) | ForEach-Object {
872+
@{ 'Name' = 'sue'; 'Value' = 'Start-UnityEditor' }
873+
) | ForEach-Object {
860874

861875
$alias = Get-Alias -Name $_.Name -ErrorAction 'SilentlyContinue'
862876
if( -not $alias ) {
863-
Write-Verbose "Creating new alias $($_.Name) for $($_.Value)"
864-
New-Alias @_
877+
Write-Verbose "Creating new alias $($_.Name) for $($_.Value)"
878+
New-Alias @_
865879
}
866880
elseif( $alias.ModuleName -eq 'UnitySetup' ) {
867-
Write-Verbose "Setting alias $($_.Name) to $($_.Value)"
881+
Write-Verbose "Setting alias $($_.Name) to $($_.Value)"
868882
Set-Alias @_
869883
}
870884
else {

0 commit comments

Comments
 (0)