|
| 1 | +param ( |
| 2 | + [Parameter(Mandatory=$true, HelpMessage="Url to download. Must point to a zip file. Assumes filename is last path element. Assumes zip contains top level directory with same name as zip file.")] |
| 3 | + [ValidateNotNullOrEmpty()] |
| 4 | + [string]$Url, |
| 5 | + |
| 6 | + [Parameter(Mandatory=$true, HelpMessage="Destination directory to unzip downloaded file.")] |
| 7 | + [ValidateNotNullOrEmpty()] |
| 8 | + [System.IO.FileInfo]$Destination, |
| 9 | + |
| 10 | + [Parameter(HelpMessage="When used, adds bin directory to PATH.")] |
| 11 | + [switch]$UpdatePath = $false, |
| 12 | + |
| 13 | + [Parameter(HelpMessage="When used, downloads directory will be cleaned and temp directories cleaned.")] |
| 14 | + [switch]$CleanOnFinish = $false, |
| 15 | + |
| 16 | + [Parameter(Mandatory=$false, HelpMessage="7Zip location. Should point to 7z.exe.")] |
| 17 | + [ValidateNotNullOrEmpty()] |
| 18 | + [System.IO.FileInfo]$PathTo7Zip, |
| 19 | + |
| 20 | + [Parameter(Mandatory=$false, HelpMessage="Skips download step.")] |
| 21 | + [switch]$SkipDownload = $false, |
| 22 | + |
| 23 | + [Parameter(Mandatory=$false, HelpMessage="Skips unzip.")] |
| 24 | + [switch]$SkipUnzip = $false |
| 25 | +) |
| 26 | + |
| 27 | +$fileName, $dirName = ($Url | Select-String -Pattern ".+/(?<filename>(?<dirname>[^/]+)\..+)$").Matches[0].Groups['filename', 'dirname'].Value |
| 28 | +$Source = [System.IO.Path]::Combine("C:\Downloads", $fileName) |
| 29 | + |
| 30 | +$ErrorActionPreference = "Stop" |
| 31 | +Import-Module "$PSScriptRoot\win-installer-helper.psm1" -DisableNameChecking |
| 32 | + |
| 33 | +Start-Setup |
| 34 | + |
| 35 | +$PathNodes=@() |
| 36 | +try |
| 37 | +{ |
| 38 | + if (-not $SkipDownload) { |
| 39 | + Write-Host "Downloading '$fileName' from '$Url' to '$Source'" |
| 40 | + Get-File -Url $url -FileName $fileName |
| 41 | + Write-Host "Download finished: $Source" |
| 42 | + } |
| 43 | + else |
| 44 | + { |
| 45 | + Write-Host "Skipping download." |
| 46 | + if (-not (Test-Path $Source)) { |
| 47 | + Write-Error "$Source does not exist" |
| 48 | + exit |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if ($SkipUnzip) |
| 53 | + { |
| 54 | + if($Destination.ToString() -eq "C:\7-Zip") { |
| 55 | + Start-Process -Wait -FilePath "$Source" -ArgumentList "/S" |
| 56 | + } |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + if ($PathTo7Zip) |
| 61 | + { |
| 62 | + Write-Host "***" |
| 63 | + Write-Host $PathTo7Zip.ToString() |
| 64 | + Write-Host "***" |
| 65 | + if (-not (Test-Path $PathTo7Zip)) |
| 66 | + { |
| 67 | + Write-Error "7zip location still does not exist $PathTo7Zip" |
| 68 | + exit |
| 69 | + } |
| 70 | + Expand-ArchiveWith7Zip -Source $Source -Destination $destination -ToolPath $PathTo7Zip |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + Write-Host "$PathTo7Zip does not exist" |
| 75 | + Expand-ArchiveWith7Zip -Source $Source -Destination $destination |
| 76 | + } |
| 77 | + Write-Host "Finished unzipping to $destination" |
| 78 | + } |
| 79 | +} |
| 80 | +finally |
| 81 | +{ |
| 82 | + if (!$PathNodes -eq "") |
| 83 | + { |
| 84 | + Write-Host "Appending to PATH: '$PathNodes'" |
| 85 | + Update-Path -PathNodes $PathNodes |
| 86 | + } |
| 87 | + if ($CleanOnFinish) |
| 88 | + { |
| 89 | + Stop-Setup |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + Stop-Setup -PreserveTemp -PreserveDownloads |
| 94 | + } |
| 95 | +} |
0 commit comments