1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
[CmdletBinding()]
param(
[string]$Dependency,
[ValidateNotNullOrEmpty()]
[string]$downloadPromptOverride = "0"
)
$downloadPromptOverride_NO_OVERRIDE= 0
$downloadPromptOverride_DO_NOT_PROMPT = 1
$downloadPromptOverride_ALWAYS_PROMPT = 2
if ($PSVersionTable.PSEdition -ne "Core") {
Import-Module BitsTransfer -Verbose:$false
}
Write-Verbose "Fetching dependency: $Dependency"
$scriptsDir = split-path -parent $MyInvocation.MyCommand.Definition
$vcpkgRootDir = & $scriptsDir\findFileRecursivelyUp.ps1 $scriptsDir .vcpkg-root
$downloadsDir = "$vcpkgRootDir\downloads"
function SelectProgram([Parameter(Mandatory=$true)][string]$Dependency)
{
function promptForDownload([string]$title, [string]$message, [string]$yesDescription, [string]$noDescription, [string]$downloadPromptOverride)
{
$do_not_prompt = ($downloadPromptOverride -eq $downloadPromptOverride_DO_NOT_PROMPT) -Or
(Test-Path "$downloadsDir\AlwaysAllowEverything") -Or
(Test-Path "$downloadsDir\AlwaysAllowDownloads")
if (($downloadPromptOverride -ne $downloadPromptOverride_ALWAYS_PROMPT) -And $do_not_prompt)
{
return $true
}
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", $yesDescription
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", $noDescription
$AlwaysAllowDownloads = New-Object System.Management.Automation.Host.ChoiceDescription "&Always Allow Downloads", ($yesDescription + "(Future download prompts will not be displayed)")
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no, $AlwaysAllowDownloads)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result)
{
0 {return $true}
1 {return $false}
2 {
New-Item "$downloadsDir\AlwaysAllowDownloads" -type file -force | Out-Null
return $true
}
}
throw "Unexpected result"
}
function performDownload( [Parameter(Mandatory=$true)][string]$Dependency,
[Parameter(Mandatory=$true)][string]$url,
[Parameter(Mandatory=$true)][string]$downloadDir,
[Parameter(Mandatory=$true)][string]$downloadPath,
[Parameter(Mandatory=$true)][string]$downloadVersion,
[Parameter(Mandatory=$true)][string]$requiredVersion)
{
if (Test-Path $downloadPath)
{
return
}
$title = "Download " + $Dependency
$message = ("No suitable version of " + $Dependency + " was found (requires $requiredVersion or higher). Download portable version?")
$yesDescription = "Downloads " + $Dependency + " v" + $downloadVersion +" app-locally."
$noDescription = "Does not download " + $Dependency + "."
$userAllowedDownload = promptForDownload $title $message $yesDescription $noDescription $downloadPromptOverride
if (!$userAllowedDownload)
{
throw [System.IO.FileNotFoundException] ("Could not detect suitable version of " + $Dependency + " and download not allowed")
}
if (!(Test-Path $downloadDir))
{
New-Item -ItemType directory -Path $downloadDir | Out-Null
}
if (($PSVersionTable.PSEdition -ne "Core") -and ($Dependency -ne "git")) # git fails with BITS
{
try {
$WC = New-Object System.Net.WebClient
$ProxyAuth = !$WC.Proxy.IsBypassed($url)
If($ProxyAuth){
$ProxyCred = Get-Credential -Message "Enter credentials for Proxy Authentication"
$PSDefaultParameterValues.Add("Start-BitsTransfer:ProxyAuthentication","Basic")
$PSDefaultParameterValues.Add("Start-BitsTransfer:ProxyCredential",$ProxyCred)
}
Start-BitsTransfer -Source $url -Destination $downloadPath -ErrorAction Stop
}
catch [System.Exception] {
# If BITS fails for any reason, delete any potentially partially downloaded files and continue
if (Test-Path $downloadPath)
{
Remove-Item $downloadPath
}
}
}
if (!(Test-Path $downloadPath))
{
Write-Verbose("Downloading $Dependency...")
(New-Object System.Net.WebClient).DownloadFile($url, $downloadPath)
}
}
# Enums (without resorting to C#) are only available on powershell 5+.
$ExtractionType_NO_EXTRACTION_REQUIRED = 0
$ExtractionType_ZIP = 1
$ExtractionType_SELF_EXTRACTING_7Z = 2
# Using this to wait for the execution to finish
function Invoke-Command()
{
param ( [string]$program = $(throw "Please specify a program" ),
[string]$argumentString = "",
[switch]$waitForExit )
$psi = new-object "Diagnostics.ProcessStartInfo"
$psi.FileName = $program
$psi.Arguments = $argumentString
$proc = [Diagnostics.Process]::Start($psi)
if ( $waitForExit )
{
$proc.WaitForExit();
}
}
function Expand-ZIPFile($file, $destination)
{
if (!(Test-Path $destination))
{
New-Item -ItemType Directory -Path $destination | Out-Null
}
$shell = new-object -com shell.application
$zip = $shell.NameSpace($file)
foreach($item in $zip.items())
{
# Piping to Out-Null is used to block until finished
$shell.Namespace($destination).copyhere($item) | Out-Null
}
}
if($Dependency -eq "cmake")
{
$requiredVersion = "3.8.0"
$downloadVersion = "3.8.0"
$url = "https://cmake.org/files/v3.8/cmake-3.8.0-win32-x86.zip"
$downloadPath = "$downloadsDir\cmake-3.8.0-win32-x86.zip"
$expectedDownloadedFileHash = "857fca00974ad6ac12fa042373d85ad1288770d4f09fbd99753c822df76b1c6c"
$executableFromDownload = "$downloadsDir\cmake-3.8.0-win32-x86\bin\cmake.exe"
$extractionType = $ExtractionType_ZIP
$extractionFolder = $downloadsDir
}
elseif($Dependency -eq "nuget")
{
$requiredVersion = "3.3.0"
$downloadVersion = "3.5.0"
$url = "https://dist.nuget.org/win-x86-commandline/v3.5.0/nuget.exe"
$downloadPath = "$downloadsDir\nuget-3.5.0\nuget.exe"
$expectedDownloadedFileHash = "399ec24c26ed54d6887cde61994bb3d1cada7956c1b19ff880f06f060c039918"
$executableFromDownload = $downloadPath
$extractionType = $ExtractionType_NO_EXTRACTION_REQUIRED
}
elseif($Dependency -eq "git")
{
$requiredVersion = "2.0.0"
$downloadVersion = "2.11.1"
$url = "https://github.com/git-for-windows/git/releases/download/v2.11.1.windows.1/MinGit-2.11.1-32-bit.zip" # We choose the 32-bit version
$downloadPath = "$downloadsDir\MinGit-2.11.1-32-bit.zip"
$expectedDownloadedFileHash = "6ca79af09015625f350ef4ad74a75cfb001b340aec095b6963be9d45becb3bba"
# There is another copy of git.exe in MinGit\bin. However, an installed version of git add the cmd dir to the PATH.
# Therefore, choosing the cmd dir here as well.
$executableFromDownload = "$downloadsDir\MinGit-2.11.1-32-bit\cmd\git.exe"
$extractionType = $ExtractionType_ZIP
$extractionFolder = "$downloadsDir\MinGit-2.11.1-32-bit"
}
else
{
throw "Unknown program requested"
}
$downloadSubdir = Split-path $downloadPath -Parent
if (!(Test-Path $downloadSubdir))
{
New-Item -ItemType Directory -Path $downloadSubdir | Out-Null
}
performDownload $Dependency $url $downloadsDir $downloadPath $downloadVersion $requiredVersion
#calculating the hash
if ($PSVersionTable.PSEdition -ne "Core")
{
$hashAlgorithm = [Security.Cryptography.HashAlgorithm]::Create("SHA256")
$fileAsByteArray = [io.File]::ReadAllBytes($downloadPath)
$hashByteArray = $hashAlgorithm.ComputeHash($fileAsByteArray)
$downloadedFileHash = -Join ($hashByteArray | ForEach {"{0:x2}" -f $_})
}
else
{
$downloadedFileHash = (Get-FileHash -Path $downloadPath -Algorithm SHA256).Hash
}
if ($expectedDownloadedFileHash -ne $downloadedFileHash)
{
throw [System.IO.FileNotFoundException] ("Mismatching hash of the downloaded " + $Dependency)
}
if ($extractionType -eq $ExtractionType_NO_EXTRACTION_REQUIRED)
{
# do nothing
}
elseif($extractionType -eq $ExtractionType_ZIP)
{
if (-not (Test-Path $executableFromDownload)) # consider renaming the extraction folder to make sure the extraction finished
{
# Expand-Archive $downloadPath -dest "$extractionFolder" -Force # Requires powershell 5+
Expand-ZIPFile -File $downloadPath -Destination $extractionFolder
}
}
elseif($extractionType -eq $ExtractionType_SELF_EXTRACTING_7Z)
{
if (-not (Test-Path $executableFromDownload))
{
Invoke-Command $downloadPath "-y" -waitForExit:$true
}
}
else
{
throw "Invalid extraction type"
}
if (-not (Test-Path $executableFromDownload))
{
throw [System.IO.FileNotFoundException] ("Could not detect or download " + $Dependency)
}
return $executableFromDownload
}
SelectProgram $Dependency
Write-Verbose "Fetching dependency: $Dependency. Done."
|