Fix Add-Path helper function and start using it in scripts
[mirror/qt/qt5.git] / coin / provisioning / common / helpers.ps1
1 function Verify-Checksum
2 {
3     Param (
4         [string]$File=$(throw("You must specify a filename to get the checksum of.")),
5         [string]$Expected=$(throw("Checksum required")),
6         [ValidateSet("sha1","md5")][string]$Algorithm="sha1"
7     )
8     $fs = new-object System.IO.FileStream $File, "Open"
9     $algo = [type]"System.Security.Cryptography.$Algorithm"
10     $crypto = $algo::Create()
11     $hash = [BitConverter]::ToString($crypto.ComputeHash($fs)).Replace("-", "")
12     $fs.Close()
13     if ($hash -ne $Expected) {
14         Write-Error "Checksum verification failed, got: '$hash' expected: '$Expected'"
15     }
16 }
17
18 function Extract-Zip
19 {
20     Param (
21         [string]$Source,
22         [string]$Destination
23     )
24     echo "Extracting '$Source' to '$Destination'..."
25
26     New-Item -ItemType Directory -Force -Path $Destination
27     $shell = new-object -com shell.application
28     $zipfile = $shell.Namespace($Source)
29     $destinationFolder = $shell.Namespace($Destination)
30     $destinationFolder.CopyHere($zipfile.Items(), 16)
31 }
32
33 function Extract-Dev-Folders-From-Zip
34 {
35     Param (
36         [string]$package,
37         [string]$zipDir,
38         [string]$installPath
39     )
40
41     $shell = new-object -com shell.application
42
43     echo "Extracting contents of $package"
44     foreach ($subDir in "lib", "include", "bin", "share") {
45         $zip = $shell.Namespace($package + "\" + $zipDir + "\" + $subDir)
46         if ($zip) {
47             Write-Host "Extracting $subDir from zip archive"
48         } else {
49             Write-Host "$subDir is missing from zip archive - skipping"
50             continue
51         }
52         $destDir = $installPath + "\" + $subdir
53         New-Item $destDir -type directory
54         $destinationFolder = $shell.Namespace($destDir)
55         $destinationFolder.CopyHere($zip.Items(), 16)
56     }
57 }
58
59 function BadParam
60 {
61     Param ([string]$Description)
62     throw("You must specify $Description")
63 }
64
65 function Download
66 {
67     Param (
68         [string] $OfficialUrl = $(BadParam("the official download URL")),
69         [string] $CachedUrl   = $(BadParam("the locally cached URL")),
70         [string] $Destination = $(BadParam("a download target location"))
71     )
72     try {
73         if ($CachedUrl.StartsWith("http")) {
74             Invoke-WebRequest -UseBasicParsing $CachedUrl -OutFile $Destination
75         } else {
76             Copy-Item $CachedUrl $Destination
77         }
78     } catch {
79         Invoke-WebRequest -UseBasicParsing $OfficialUrl -OutFile $Destination
80     }
81 }
82
83 function Add-Path
84 {
85     Param (
86         [string]$Path
87     )
88     echo "Adding $Path to Path"
89
90     $oldPath = [System.Environment]::GetEnvironmentVariable('Path', 'Machine')
91     [Environment]::SetEnvironmentVariable("Path", $oldPath + ";$Path", [EnvironmentVariableTarget]::Machine)
92 }
93
94 function is64bitWinHost
95 {
96     if(($env:PROCESSOR_ARCHITECTURE -eq "AMD64") -or ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64")) {
97         return 1
98     }
99     else {
100         return 0
101     }
102 }