aboutsummaryrefslogtreecommitdiffstats
path: root/coin/provisioning/common/windows/helpers.ps1
diff options
context:
space:
mode:
authorOliver Wolff <oliver.wolff@qt.io>2018-02-14 07:46:11 +0100
committerLiang Qi <liang.qi@qt.io>2018-02-26 19:52:45 +0000
commitcb6709ce5c48a31ba1170c71494c1a3869ffa5bb (patch)
tree8fdb8721a71cba868c2fbfe7c9fbc237d1f990a8 /coin/provisioning/common/windows/helpers.ps1
parent08c88de5c8d531fb4fee03a0c337dd93739d365a (diff)
Unify Windows provisioning scripts & improve error handlingv5.11.0-beta1
While Coin should also see exit codes != 0 as error, we should stick to one way of handling script errors. As Power Shell cmdlets signal an error by throwing an exception we should do the same (and that approach also works in Coin). Additionally extracting 7zip files was unified across scripts by using the existing helper function instead of reinventing the wheel again and again. A similar helper function was introduced for starting an external application (and handling its errors). Also echo and other "cmd" commands were replaced by their PowerShell equivalents to have a unified approach across our Windows provisioning scripts. Change-Id: I70129ce38692f1396c33c13b33a2918485fa5271 Reviewed-by: Frederik Gladhorn <frederik.gladhorn@qt.io>
Diffstat (limited to 'coin/provisioning/common/windows/helpers.ps1')
-rw-r--r--coin/provisioning/common/windows/helpers.ps172
1 files changed, 40 insertions, 32 deletions
diff --git a/coin/provisioning/common/windows/helpers.ps1 b/coin/provisioning/common/windows/helpers.ps1
index 9c6706a2..45779c04 100644
--- a/coin/provisioning/common/windows/helpers.ps1
+++ b/coin/provisioning/common/windows/helpers.ps1
@@ -5,13 +5,32 @@ function Verify-Checksum
[string]$Expected=$(throw("Checksum required")),
[ValidateSet("sha1","md5")][string]$Algorithm="sha1"
)
+ Write-Host "Verifying checksum of $File"
$fs = new-object System.IO.FileStream $File, "Open"
$algo = [type]"System.Security.Cryptography.$Algorithm"
$crypto = $algo::Create()
$hash = [BitConverter]::ToString($crypto.ComputeHash($fs)).Replace("-", "")
$fs.Close()
if ($hash -ne $Expected) {
- Write-Error "Checksum verification failed, got: '$hash' expected: '$Expected'"
+ throw "Checksum verification failed, got: '$hash' expected: '$Expected'"
+ }
+}
+
+function Run-Executable
+{
+ Param (
+ [string]$Executable=$(throw("You must specify a program to run.")),
+ [string[]]$Arguments
+ )
+ if ([string]::IsNullOrEmpty($Arguments)) {
+ Write-Host "Running `"$Executable`""
+ $p = Start-Process -FilePath "$Executable" -Wait -PassThru
+ } else {
+ Write-Host "Running `"$Executable`" with arguments `"$Arguments`""
+ $p = Start-Process -FilePath "$Executable" -ArgumentList $Arguments -Wait -PassThru
+ }
+ if ($p.ExitCode -ne 0) {
+ throw "Process $($Executable) exited with exit code $($p.ExitCode)"
}
}
@@ -21,24 +40,21 @@ function Extract-7Zip
[string]$Source,
[string]$Destination
)
- echo "Extracting '$Source' to '$Destination'..."
+ Write-Host "Extracting '$Source' to '$Destination'..."
if ((Get-Command "7z.exe" -ErrorAction SilentlyContinue) -eq $null) {
- $zipExe = join-path (${env:ProgramFiles(x86)}, ${env:ProgramFiles} -ne $null)[0] '7-zip\7z.exe'
+ $zipExe = join-path (${env:ProgramFiles(x86)}, ${env:ProgramFiles}, ${env:ProgramW6432} -ne $null)[0] '7-zip\7z.exe'
if (-not (test-path $zipExe)) {
- $zipExe = join-path ${env:ProgramW6432} '7-zip\7z.exe'
+ $zipExe = "C:\Utils\sevenzip\7z.exe"
if (-not (test-path $zipExe)) {
- $zipExe = "C:\Utils\sevenzip\7z.exe"
- if (-not (test-path $zipExe)) {
- throw "Could not find 7-zip."
- }
+ throw "Could not find 7-zip."
}
}
} else {
$zipExe = "7z.exe"
}
- & $zipExe x $Source "-o$Destination" -y
+ Run-Executable "$zipExe" "x -y `"-o$Destination`" `"$Source`""
}
function Extract-Zip
@@ -47,7 +63,7 @@ function Extract-Zip
[string]$Source,
[string]$Destination
)
- echo "Extracting '$Source' to '$Destination'..."
+ Write-Host "Extracting '$Source' to '$Destination'..."
New-Item -ItemType Directory -Force -Path $Destination
$shell = new-object -com shell.application
@@ -66,7 +82,7 @@ function Extract-Dev-Folders-From-Zip
$shell = new-object -com shell.application
- echo "Extracting contents of $package"
+ Write-Host "Extracting contents of $package"
foreach ($subDir in "lib", "include", "bin", "share") {
$zip = $shell.Namespace($package + "\" + $zipDir + "\" + $subDir)
if ($zip) {
@@ -97,12 +113,14 @@ function Download
)
$ProgressPreference = 'SilentlyContinue'
try {
+ Write-Host "Downloading from cached location ($CachedUrl) to $Destination"
if ($CachedUrl.StartsWith("http")) {
Invoke-WebRequest -UseBasicParsing $CachedUrl -OutFile $Destination
} else {
Copy-Item $CachedUrl $Destination
}
} catch {
+ Write-Host "Cached download failed: Downloading from official location: $OfficialUrl"
Invoke-WebRequest -UseBasicParsing $OfficialUrl -OutFile $Destination
}
}
@@ -112,37 +130,27 @@ function Add-Path
Param (
[string]$Path
)
- echo "Adding $Path to Path"
+ Write-Host "Adding $Path to Path"
$oldPath = [System.Environment]::GetEnvironmentVariable('Path', 'Machine')
[Environment]::SetEnvironmentVariable("Path", $oldPath + ";$Path", [EnvironmentVariableTarget]::Machine)
$Env:PATH = [System.Environment]::GetEnvironmentVariable('Path', 'Machine')
}
-function is64bitWinHost
+function Set-EnvironmentVariable
{
- if(($env:PROCESSOR_ARCHITECTURE -eq "AMD64") -or ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64")) {
- return 1
- }
- else {
- return 0
- }
+ Param (
+ [string]$Key = $(BadParam("a key")),
+ [string]$Value = $(BadParam("a value."))
+ )
+ Write-Host "Setting environment variable `"$($Key)`" to `"$($Value)`""
+
+ [Environment]::SetEnvironmentVariable($Key, $Value, [EnvironmentVariableTarget]::Machine)
}
-Function Execute-Command
+function Is64BitWinHost
{
- Param (
- [string]$command
- )
- Try {
- echo "Executing command '$command'..."
- $process = Start-Process -FilePath "cmd" -ArgumentList "/c $command" -PassThru -Wait -WindowStyle Hidden
- if ($process.ExitCode) {throw "Error running command: '$command'"}
- }
- Catch {
- $_.Exception.Message
- exit 1
- }
+ return [environment]::Is64BitOperatingSystem
}
function isProxyEnabled {