Skip to content

Commit bc79f16

Browse files
committed
Shows progress of WebClient downloads. Creates separate install method for installing a single package.
1 parent b0529f9 commit bc79f16

File tree

1 file changed

+114
-42
lines changed

1 file changed

+114
-42
lines changed

UnitySetup/UnitySetup.psm1

Lines changed: 114 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ enum OperatingSystem {
2929
Mac
3030
}
3131

32+
class UnitySetupResource {
33+
[UnitySetupComponent] $ComponentType
34+
[string] $Path
35+
}
36+
3237
class UnitySetupInstaller {
3338
[UnitySetupComponent] $ComponentType
3439
[UnityVersion] $Version
@@ -523,9 +528,25 @@ function Request-UnitySetupInstaller {
523528
($destinationItem.LastWriteTime -eq $_.LastModified) ) {
524529
Write-Verbose "Skipping download because it's already in the cache: $($_.DownloadUrl)"
525530

526-
$downloads += ,$destination
531+
$resource = New-Object UnitySetupResource -Property @{
532+
'ComponentType' = $_.ComponentType
533+
'Path' = $destination
534+
}
535+
$downloads += , $resource
527536
return
528537
}
538+
elseif ($destinationItem.Length -eq $_.Length ) {
539+
Write-Verbose "Skipping download because file size is the same: $($_.DownloadUrl)"
540+
541+
# Re-writes the last modified time for ensuring downloads cache.
542+
$downloadedFile = Get-Item $destination
543+
$downloadedFile.LastWriteTime = $_.LastModified
544+
545+
$downloadedFile = Get-Item $destination
546+
if ( ($downloadedFile.LastWriteTime -ne $_.LastModified) ) {
547+
Write-Verbose "Modified time not set for $destination"
548+
}
549+
}
529550
}
530551

531552
$destinationDirectory = [io.path]::GetDirectoryName($destination)
@@ -536,13 +557,37 @@ function Request-UnitySetupInstaller {
536557
try
537558
{
538559
Write-Verbose "Downloading $($_.DownloadUrl) to $destination"
539-
(New-Object System.Net.WebClient).DownloadFile($_.DownloadUrl, $destination)
560+
561+
$webClient = New-Object System.Net.WebClient
562+
Register-ObjectEvent -InputObject $webClient -EventName DownloadFileCompleted `
563+
-SourceIdentifier Web.DownloadFileCompleted -Action {
564+
$global:isDownloaded = $true
565+
}
566+
Register-ObjectEvent -InputObject $webClient -EventName DownloadProgressChanged `
567+
-SourceIdentifier Web.DownloadProgressChanged -Action {
568+
$global:DownloadProgressEvent = $event
569+
}
570+
$webClient.DownloadFileAsync($_.DownloadUrl, $destination)
571+
572+
$totalBytes = $_.Length
573+
while (-not $isDownloaded) {
574+
$receivedBytes = $global:DownloadProgressEvent.SourceArgs.BytesReceived
575+
$progress = [int](($receivedBytes / [double]$totalBytes) * 100)
576+
577+
Write-Progress -Activity "Downloading $($_.DownloadUrl)..." -Status "$receivedBytes bytes \ $totalBytes bytes" -PercentComplete $progress
578+
[System.Threading.Thread]::Sleep(500)
579+
}
580+
Write-Progress -Activity "Downloading $($_.DownloadUrl)..." -Status "$receivedBytes bytes \ $totalBytes bytes" -Completed
540581

541582
# Re-writes the last modified time for ensuring downloads cache.
542583
$downloadedFile = Get-Item $destination
543584
$downloadedFile.LastWriteTime = $_.LastModified
544585

545-
$downloads += ,$destination
586+
$resource = New-Object UnitySetupResource -Property @{
587+
'ComponentType' = $_.ComponentType
588+
'Path' = $destination
589+
}
590+
$downloads += , $resource
546591
}
547592
catch [System.Net.WebException]
548593
{
@@ -555,6 +600,53 @@ function Request-UnitySetupInstaller {
555600
}
556601
}
557602

603+
function Install-UnitySetupPackage {
604+
[CmdletBinding()]
605+
param(
606+
[parameter(Mandatory = $true)]
607+
[UnitySetupResource] $Package,
608+
609+
[parameter(Mandatory = $false)]
610+
[string]$Destination
611+
)
612+
613+
$currentOS = Get-OperatingSystem
614+
switch ($currentOS) {
615+
([OperatingSystem]::Windows) {
616+
$startProcessArgs = @{
617+
'FilePath' = $Package.Path;
618+
'ArgumentList' = @("/S", "/D=$Destination");
619+
'PassThru' = $true;
620+
'Wait' = $true;
621+
}
622+
}
623+
([OperatingSystem]::Linux) {
624+
throw "Install-UnitySetupPackage has not been implemented on the Linux platform. Contributions welcomed!";
625+
}
626+
([OperatingSystem]::Mac) {
627+
# Note, ignores $Destination on Mac.
628+
# sudo installer -package $Package.Path -target /
629+
$startProcessArgs = @{
630+
'FilePath' = 'sudo';
631+
'ArgumentList' = @("installer", "-package", $Package.Path, "-target", "/");
632+
'PassThru' = $true;
633+
'Wait' = $true;
634+
}
635+
}
636+
}
637+
638+
Write-Verbose "$(Get-Date): Installing $($Package.ComponentType) to $Destination."
639+
$process = Start-Process @startProcessArgs
640+
if ( $process ) {
641+
if ( $process.ExitCode -ne 0) {
642+
Write-Error "$(Get-Date): Failed with exit code: $($process.ExitCode)"
643+
}
644+
else {
645+
Write-Verbose "$(Get-Date): Succeeded."
646+
}
647+
}
648+
}
649+
558650
<#
559651
.Synopsis
560652
Installs a UnitySetup instance.
@@ -637,8 +729,6 @@ function Install-UnitySetupInstance {
637729
# TODO: Work in a `$host.ui.PromptForChoice` / -Force param for resolving this.
638730
throw "Install-UnitySetupInstance has not yet handled working around the base install directory already existing. Please move this manually and try again. Contributions welcomed!";
639731
}
640-
641-
642732
}
643733

644734
if ( $PSBoundParameters.ContainsKey('Destination') ) {
@@ -654,48 +744,30 @@ function Install-UnitySetupInstance {
654744
$installPath = "$defaultInstallPath-$installVersion"
655745
}
656746

747+
# TODO: Strip out components already installed in the destination.
748+
657749
$installerPaths = $installerInstances | Request-UnitySetupInstaller -Cache $Cache
658750

659-
# TODO: Install Unity component first
660-
661-
foreach ($componentInstallerPath in $installerPaths) {
662-
switch ($currentOS) {
663-
([OperatingSystem]::Windows) {
664-
$startProcessArgs = @{
665-
'FilePath' = $_;
666-
'ArgumentList' = @("/S", "/D=$installPath");
667-
'PassThru' = $true;
668-
'Wait' = $true;
669-
}
670-
}
671-
([OperatingSystem]::Linux) {
672-
throw "Install-UnitySetupInstance has not been implemented on the Linux platform. Contributions welcomed!";
673-
}
674-
([OperatingSystem]::Mac) {
675-
$startProcessArgs = @{
676-
'FilePath' = $installer;
677-
'ArgumentList' = @("/S", "/D=$destination");
678-
'PassThru' = $true;
679-
'Wait' = $true;
680-
}
681-
}
682-
}
683-
684-
Write-Verbose "$(Get-Date): Installing $installer to $destination."
685-
$process = Start-Process @startProcessArgs
686-
if ( $process ) {
687-
if ( $process.ExitCode -ne 0) {
688-
Write-Error "$(Get-Date): Failed with exit code: $($process.ExitCode)"
689-
}
690-
else {
691-
Write-Verbose "$(Get-Date): Succeeded."
692-
}
693-
}
751+
# First install the Unity editor before other components.
752+
$editorComponent = switch ($currentOS) {
753+
([OperatingSystem]::Windows) { [UnitySetupComponent]::Windows }
754+
([OperatingSystem]::Linux) { [UnitySetupComponent]::Linux }
755+
([OperatingSystem]::Mac) { [UnitySetupComponent]::Mac }
756+
}
757+
$editorInstaller = $installerPaths | Where-Object { $_.ComponentType -band $editorComponent }
758+
if ($null -ne $editorInstaller) {
759+
Write-Verbose "Installing $($editorInstaller.ComponentType)"
760+
# Install-UnitySetupPackage -Package $editorInstaller -Destination $destination
761+
}
762+
763+
$installerPaths | ForEach-Object {
764+
Write-Verbose "Installing $($editorInstaller.ComponentType)"
765+
# Install-UnitySetupPackage -Package $_ -Destination $destination
694766
}
695767

696768
# Move the install from the staging area to the desired destination
697769
if ($currentOS == [OperatingSystem]::Mac) {
698-
Move-Item -Path /Applications/Unity/ -Destination $installPath
770+
#Move-Item -Path /Applications/Unity/ -Destination $installPath
699771
}
700772
}
701773
}
@@ -775,7 +847,7 @@ function Get-UnitySetupInstance {
775847
}
776848
([OperatingSystem]::Mac) {
777849
if (-not $BasePath) {
778-
$BasePath = @('/Applications/Unity*')
850+
$BasePath = @('/Applications/Unity*', '/Applications/Unity/Hub/Editor/*')
779851
}
780852
$ivyPath = 'Unity.app/Contents/UnityExtensions/Unity/Networking/ivy.xml'
781853
}

0 commit comments

Comments
 (0)