2021-04-26 10:27:45 -07:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Gets a random file path in the temp directory.
|
|
|
|
|
|
|
|
.DESCRIPTION
|
|
|
|
Get-TempFilePath takes an extension, and returns a path with a random
|
|
|
|
filename component in the temporary directory with that extension.
|
|
|
|
|
|
|
|
.PARAMETER Extension
|
|
|
|
The extension to use for the path.
|
|
|
|
#>
|
|
|
|
Function Get-TempFilePath {
|
|
|
|
Param(
|
|
|
|
[String]$Extension
|
|
|
|
)
|
|
|
|
|
|
|
|
$tempPath = [System.IO.Path]::GetTempPath()
|
2024-07-03 20:01:35 -07:00
|
|
|
$tempName = [System.IO.Path]::GetRandomFileName()
|
|
|
|
if (-not [String]::IsNullOrWhiteSpace($Extension)) {
|
|
|
|
$tempName = $tempName + '.' + $Extension
|
2021-04-26 10:27:45 -07:00
|
|
|
}
|
2024-07-03 20:01:35 -07:00
|
|
|
return Join-Path $tempPath $tempName
|
2021-04-26 10:27:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
2024-07-03 20:01:35 -07:00
|
|
|
Download and install a component.
|
2021-04-26 10:27:45 -07:00
|
|
|
|
|
|
|
.DESCRIPTION
|
2024-07-03 20:01:35 -07:00
|
|
|
DownloadAndInstall downloads an executable from the given URL, and runs it with the given command-line arguments.
|
2021-04-26 10:27:45 -07:00
|
|
|
|
|
|
|
.PARAMETER Name
|
2024-07-03 20:01:35 -07:00
|
|
|
The name of the component, to be displayed in logging messages.
|
2021-04-26 10:27:45 -07:00
|
|
|
|
|
|
|
.PARAMETER Url
|
2024-07-03 20:01:35 -07:00
|
|
|
The URL of the installer.
|
|
|
|
|
|
|
|
.PARAMETER Args
|
|
|
|
The command-line arguments to pass to the installer.
|
2021-04-26 10:27:45 -07:00
|
|
|
#>
|
2024-07-03 20:01:35 -07:00
|
|
|
Function DownloadAndInstall {
|
|
|
|
[CmdletBinding(PositionalBinding=$false)]
|
2021-04-26 10:27:45 -07:00
|
|
|
Param(
|
2024-07-03 20:01:35 -07:00
|
|
|
[Parameter(Mandatory)][String]$Name,
|
|
|
|
[Parameter(Mandatory)][String]$Url,
|
|
|
|
[Parameter(Mandatory)][String[]]$Args,
|
|
|
|
[String]$LocalName = $null
|
2021-04-26 10:27:45 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
try {
|
2024-07-03 20:01:35 -07:00
|
|
|
if ([string]::IsNullOrWhiteSpace($LocalName)) {
|
|
|
|
$LocalName = [uri]::new($Url).Segments[-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
[bool]$doRemove = $false
|
|
|
|
[string]$LocalPath = Join-Path $PSScriptRoot $LocalName
|
|
|
|
if (Test-Path $LocalPath) {
|
|
|
|
Write-Host "Using local $Name..."
|
|
|
|
} else {
|
|
|
|
Write-Host "Downloading $Name..."
|
|
|
|
$tempPath = Get-TempFilePath
|
|
|
|
New-Item -ItemType Directory -Path $tempPath -Force | Out-Null
|
|
|
|
$LocalPath = Join-Path $tempPath $LocalName
|
2024-11-19 14:42:29 -08:00
|
|
|
curl.exe -L -o $LocalPath $Url
|
2024-07-03 20:01:35 -07:00
|
|
|
$doRemove = $true
|
|
|
|
}
|
|
|
|
|
2021-04-26 10:27:45 -07:00
|
|
|
Write-Host "Installing $Name..."
|
2024-07-03 20:01:35 -07:00
|
|
|
$proc = Start-Process -FilePath $LocalPath -ArgumentList $Args -Wait -PassThru
|
|
|
|
$exitCode = $proc.ExitCode
|
|
|
|
|
|
|
|
if ($exitCode -eq 0) {
|
|
|
|
Write-Host 'Installation successful!'
|
|
|
|
} elseif ($exitCode -eq 3010) {
|
|
|
|
Write-Host 'Installation successful! Exited with 3010 (ERROR_SUCCESS_REBOOT_REQUIRED).'
|
|
|
|
} else {
|
|
|
|
Write-Error "Installation failed! Exited with $exitCode."
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($doRemove) {
|
|
|
|
Remove-Item -Path $LocalPath -Force
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
Write-Error "Installation failed! Exception: $($_.Exception.Message)"
|
2021-04-26 10:27:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
2024-07-03 20:01:35 -07:00
|
|
|
Download and install a zip file component.
|
2021-04-26 10:27:45 -07:00
|
|
|
|
|
|
|
.DESCRIPTION
|
2024-07-03 20:01:35 -07:00
|
|
|
DownloadAndUnzip downloads a zip from the given URL, and extracts it to the indicated path.
|
2021-04-26 10:27:45 -07:00
|
|
|
|
|
|
|
.PARAMETER Name
|
2024-07-03 20:01:35 -07:00
|
|
|
The name of the component, to be displayed in logging messages.
|
2021-04-26 10:27:45 -07:00
|
|
|
|
|
|
|
.PARAMETER Url
|
2024-07-03 20:01:35 -07:00
|
|
|
The URL of the zip to download.
|
2021-04-26 10:27:45 -07:00
|
|
|
|
2024-07-03 20:01:35 -07:00
|
|
|
.PARAMETER Destination
|
|
|
|
The location to which the zip should be extracted
|
2021-04-26 10:27:45 -07:00
|
|
|
#>
|
2024-07-03 20:01:35 -07:00
|
|
|
Function DownloadAndUnzip {
|
|
|
|
[CmdletBinding(PositionalBinding=$false)]
|
2021-04-26 10:27:45 -07:00
|
|
|
Param(
|
2024-07-03 20:01:35 -07:00
|
|
|
[Parameter(Mandatory)][String]$Name,
|
|
|
|
[Parameter(Mandatory)][String]$Url,
|
|
|
|
[Parameter(Mandatory)][String]$Destination
|
2021-04-26 10:27:45 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
try {
|
2024-07-03 20:01:35 -07:00
|
|
|
$fileName = [uri]::new($Url).Segments[-1]
|
|
|
|
if ([string]::IsNullOrWhiteSpace($LocalName)) {
|
|
|
|
$LocalName = $fileName
|
|
|
|
}
|
|
|
|
|
|
|
|
[string]$zipPath
|
|
|
|
[bool]$doRemove = $false
|
|
|
|
[string]$LocalPath = Join-Path $PSScriptRoot $LocalName
|
|
|
|
if (Test-Path $LocalPath) {
|
|
|
|
Write-Host "Using local $Name..."
|
|
|
|
$zipPath = $LocalPath
|
|
|
|
} else {
|
|
|
|
$tempPath = Get-TempFilePath
|
|
|
|
New-Item -ItemType Directory -Path $tempPath -Force | Out-Null
|
|
|
|
$zipPath = Join-Path $tempPath $LocalName
|
|
|
|
Write-Host "Downloading $Name ( $Url -> $zipPath )..."
|
2024-11-19 14:42:29 -08:00
|
|
|
curl.exe -L -o $zipPath $Url
|
2024-07-03 20:01:35 -07:00
|
|
|
$doRemove = $true
|
|
|
|
}
|
|
|
|
|
|
|
|
Write-Host "Installing $Name to $Destination..."
|
|
|
|
& tar.exe -xvf $zipPath --strip 1 --directory $Destination
|
|
|
|
if ($LASTEXITCODE -eq 0) {
|
|
|
|
Write-Host 'Installation successful!'
|
|
|
|
} else {
|
|
|
|
Write-Error "Installation failed! Exited with $LASTEXITCODE."
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($doRemove) {
|
|
|
|
Remove-Item -Path $zipPath -Force
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
Write-Error "Installation failed! Exception: $($_.Exception.Message)"
|
2021-04-26 10:27:45 -07:00
|
|
|
}
|
|
|
|
}
|