Skip to content

Commit c422a1a

Browse files
committed
Download UnitySetup executable for Linux
1 parent b909d1b commit c422a1a

File tree

1 file changed

+130
-19
lines changed

1 file changed

+130
-19
lines changed

UnitySetup/UnitySetup.psm1

Lines changed: 130 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,10 @@ function ConvertTo-UnitySetupComponent {
366366
Manually specify the build hash, to select a private build.
367367
.PARAMETER Components
368368
What components would you like to search for? Defaults to All
369+
.PARAMETER Cache
370+
File path where installer and configuration for Linux will be downloaded to.
371+
Preliminary installer and configuration download is necessary to retrieve actual paths of Unity components for Linux
372+
and to allow subsequent components install. Defaults to ~/.unitysetup
369373
.EXAMPLE
370374
Find-UnitySetupInstaller -Version 2017.3.0f3
371375
.EXAMPLE
@@ -381,9 +385,27 @@ function Find-UnitySetupInstaller {
381385
[UnitySetupComponent] $Components = [UnitySetupComponent]::All,
382386

383387
[parameter(Mandatory = $false)]
384-
[string] $Hash = ""
388+
[string] $Hash = "",
389+
390+
[parameter(Mandatory = $false)]
391+
[string]$Cache = [io.Path]::Combine("~", ".unitysetup")
392+
393+
# ,
394+
# [parameter(Mandatory = $false)]
395+
# [switch]$Verbose = $false
385396
)
386397

398+
# Note that this has to happen before calculating the full path since
399+
# Resolve-Path throws an exception on missing paths.
400+
if (!(Test-Path $Cache -PathType Container)) {
401+
New-Item $Cache -ItemType Directory -ErrorAction Stop | Out-Null
402+
}
403+
404+
# Expanding '~' to the absolute path on the system. `WebClient` on macOS asumes
405+
# relative path. macOS also treats alt directory separators as part of the file
406+
# name and this corrects the separators to current environment.
407+
$fullCachePath = (Resolve-Path -Path $Cache).Path
408+
387409
$Components = ConvertTo-UnitySetupComponent -Component $Components -Version $Version
388410

389411
$currentOS = Get-OperatingSystem
@@ -451,6 +473,9 @@ function Find-UnitySetupInstaller {
451473
}
452474
([OperatingSystem]::Linux) {
453475
$setupComponent = [UnitySetupComponent]::Linux
476+
477+
# Note: This is the main installer small executable that is used to install all other components.
478+
# It will be replaced later by Linux Editor component
454479
$installerTemplates[$setupComponent] = , "UnitySetup-$Version";
455480
}
456481
([OperatingSystem]::Mac) {
@@ -541,16 +566,34 @@ function Find-UnitySetupInstaller {
541566

542567
$linkComponents = $prototypeLink -split "/$hashRegEx/" -ne ""
543568

544-
$iniFileName = "unity-$Version-linux.ini"
545-
$baseUrl = $linkComponents[0] + "/" + $linkComponents[1]
546-
$iniLink = "$baseUrl/$iniFileName"
569+
$unityPath = $linkComponents[0]
570+
$unityHash = $linkComponents[1]
571+
$unityVersion = $linkComponents[2] # simnilar to UnitySetup-2021.1.23f1
572+
573+
$baseUrl = "$unityPath/$unityHash"
574+
575+
Write-Verbose "Ready to download installer from $baseUrl"
576+
577+
# also download installer that will be used to install all other components
578+
$installerName = $installerTemplates[[UnitySetupComponent]::Linux]
579+
$installerUrl = "$baseUrl/$installerName"
580+
$cachedInstallerBasePath = "$fullCachePath/Installers/Unity-$Version"
581+
New-Item $cachedInstallerBasePath -ItemType Directory -Force | Out-Null
547582

548-
$linuxIni = Invoke-WebRequest $iniLink -UseBasicParsing
583+
$localCachedInstaller = "$cachedInstallerBasePath/$installerName"
584+
585+
Write-Verbose "Saving installer to $localCachedInstaller"
586+
587+
Invoke-WebRequest $installerUrl -UseBasicParsing | Set-Content -Path $localCachedInstaller
588+
589+
# Configuration file for installer
590+
$iniFileName = "unity-$Version-linux.ini"
591+
$iniUrl = "$baseUrl/$iniFileName"
549592

550593
# PsIni can read only from files and from stdin
551-
$localCachedIni = "/tmp/$iniFileName"
552-
553-
Set-Content -Path $localCachedIni -Value $linuxIni
594+
$localCachedIni = "$Cache/$iniFileName"
595+
596+
Invoke-WebRequest $iniUrl -UseBasicParsing | Set-Content -Path $localCachedIni
554597

555598
$iniContent = Get-IniContent $localCachedIni
556599

@@ -947,6 +990,19 @@ function Request-UnitySetupInstaller {
947990
}
948991
}
949992

993+
function StartProcessWithArgs($startProcessArgs)
994+
{
995+
$process = Start-Process @startProcessArgs
996+
if ( $process ) {
997+
if ( $process.ExitCode -ne 0) {
998+
Write-Error "$(Get-Date): Failed with exit code: $($process.ExitCode)"
999+
}
1000+
else {
1001+
Write-Verbose "$(Get-Date): Succeeded."
1002+
}
1003+
}
1004+
}
1005+
9501006
function Install-UnitySetupPackage {
9511007
[CmdletBinding()]
9521008
param(
@@ -968,7 +1024,70 @@ function Install-UnitySetupPackage {
9681024
}
9691025
}
9701026
([OperatingSystem]::Linux) {
971-
throw "Install-UnitySetupPackage has not been implemented on the Linux platform. Contributions welcomed!";
1027+
1028+
$VerbosePreference = "Continue"
1029+
1030+
# TODO: assert that tar and 7z are installed
1031+
1032+
Write-Verbose "Package path: $($Package.Path)"
1033+
Write-Verbose "Destination: $Destination"
1034+
1035+
$isTarXz = $Package.Path -match ".*\.tar\.xz"
1036+
$isPkg = $Package.Path -match ".*\.pkg"
1037+
1038+
if ($isTarXz) {
1039+
Write-Verbose ".tar.xz"
1040+
1041+
$unpackedDir = $(Resolve-Path "$($Package.Path)") -replace '\.tar\.xz$',''
1042+
New-Item -ItemType Directory -Force "$unpackedDir" | Out-Null
1043+
1044+
Write-Verbose "Unpack $($Package.Path) to $unpackedDir"
1045+
1046+
$startProcessArgs = @{
1047+
'FilePath' = 'tar';
1048+
'ArgumentList' = @("xf", $Package.Path, "-C", "$unpackedDir", "-v");
1049+
'PassThru' = $true;
1050+
'Wait' = $true;
1051+
}
1052+
1053+
StartProcessWithArgs($startProcessArgs)
1054+
}
1055+
elseif ($isPkg) {
1056+
Write-Verbose ".pkg"
1057+
1058+
$unpackedDir = $(Resolve-Path "$($Package.Path)") -replace '\.pkg$',''
1059+
New-Item -ItemType Directory -Force "$unpackedDir" | Out-Null
1060+
1061+
Write-Verbose "Unpack $($Package.Path) to $unpackedDir"
1062+
1063+
$startProcessArgs = @{
1064+
'FilePath' = '7z';
1065+
'ArgumentList' = @("x", $Package.Path, "-o$unpackedDir/", "-t*", "-y");
1066+
'PassThru' = $true;
1067+
'Wait' = $true;
1068+
}
1069+
1070+
StartProcessWithArgs($startProcessArgs)
1071+
}
1072+
else {
1073+
throw "$($Package.Path) has unsupported archive format"
1074+
}
1075+
1076+
$pkgInfo = [xml](Get-Content ./TargetSupport.pkg.tmp/PackageInfo)
1077+
$targetName = basename $($pkgInfo.'pkg-info'.'install-location')
1078+
1079+
Write-Verbose "targetName: $targetName"
1080+
1081+
return
1082+
1083+
$payloadPath = "$unpackedDir/TargetSupport.pkg.tmp/Payload"
1084+
# extract /TargetSupport.pkg.tmp/Payload to destination
1085+
$startProcessArgs = @{
1086+
'FilePath' = '7z';
1087+
'ArgumentList' = @("x", $Package.Path, "-o$unpackedDir", "-t*");
1088+
'PassThru' = $true;
1089+
'Wait' = $true;
1090+
}
9721091
}
9731092
([OperatingSystem]::Mac) {
9741093
# Note that $Destination has to be a disk path.
@@ -983,15 +1102,7 @@ function Install-UnitySetupPackage {
9831102
}
9841103

9851104
Write-Verbose "$(Get-Date): Installing $($Package.ComponentType) to $Destination."
986-
$process = Start-Process @startProcessArgs
987-
if ( $process ) {
988-
if ( $process.ExitCode -ne 0) {
989-
Write-Error "$(Get-Date): Failed with exit code: $($process.ExitCode)"
990-
}
991-
else {
992-
Write-Verbose "$(Get-Date): Succeeded."
993-
}
994-
}
1105+
StartProcessWithArgs($startProcessArgs)
9951106
}
9961107

9971108
<#
@@ -1033,7 +1144,7 @@ function Install-UnitySetupInstance {
10331144
)
10341145
begin {
10351146
$currentOS = Get-OperatingSystem
1036-
1147+
10371148
if ( -not $PSBoundParameters.ContainsKey('BasePath') ) {
10381149
$defaultInstallPath = switch ($currentOS) {
10391150
([OperatingSystem]::Windows) {

0 commit comments

Comments
 (0)