2016-09-19 11:50:08 +08:00
|
|
|
[CmdletBinding()]
|
|
|
|
param(
|
2017-08-16 05:41:23 +08:00
|
|
|
[string]$Dependency
|
2016-09-19 11:50:08 +08:00
|
|
|
)
|
|
|
|
|
2017-10-18 04:40:01 +08:00
|
|
|
function Test-Command($commandName)
|
|
|
|
{
|
|
|
|
return [bool](Get-Command -Name $commandName -ErrorAction SilentlyContinue)
|
|
|
|
}
|
|
|
|
|
|
|
|
function Test-Module($moduleName)
|
|
|
|
{
|
|
|
|
return [bool](Get-Module -ListAvailable -Name $moduleName)
|
|
|
|
}
|
|
|
|
|
2017-10-18 04:42:58 +08:00
|
|
|
if (Test-Module -moduleName 'BitsTransfer')
|
|
|
|
{
|
2017-05-24 02:53:20 +08:00
|
|
|
Import-Module BitsTransfer -Verbose:$false
|
2017-05-23 04:57:37 +08:00
|
|
|
}
|
2016-09-19 11:50:08 +08:00
|
|
|
|
2017-05-07 12:37:04 +08:00
|
|
|
Write-Verbose "Fetching dependency: $Dependency"
|
|
|
|
|
2016-09-20 09:46:02 +08:00
|
|
|
$scriptsDir = split-path -parent $MyInvocation.MyCommand.Definition
|
|
|
|
$vcpkgRootDir = & $scriptsDir\findFileRecursivelyUp.ps1 $scriptsDir .vcpkg-root
|
|
|
|
|
|
|
|
$downloadsDir = "$vcpkgRootDir\downloads"
|
2016-09-19 11:50:08 +08:00
|
|
|
|
|
|
|
function SelectProgram([Parameter(Mandatory=$true)][string]$Dependency)
|
|
|
|
{
|
|
|
|
function performDownload( [Parameter(Mandatory=$true)][string]$Dependency,
|
|
|
|
[Parameter(Mandatory=$true)][string]$url,
|
2016-09-20 09:46:02 +08:00
|
|
|
[Parameter(Mandatory=$true)][string]$downloadDir,
|
2016-09-19 11:50:08 +08:00
|
|
|
[Parameter(Mandatory=$true)][string]$downloadPath,
|
|
|
|
[Parameter(Mandatory=$true)][string]$downloadVersion,
|
|
|
|
[Parameter(Mandatory=$true)][string]$requiredVersion)
|
|
|
|
{
|
|
|
|
if (Test-Path $downloadPath)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
}
|
2017-01-24 09:02:43 +08:00
|
|
|
|
2017-08-17 04:11:50 +08:00
|
|
|
# Can't print because vcpkg captures the output and expects only the path that is returned at the end of this script file
|
2017-08-16 10:29:03 +08:00
|
|
|
# Write-Host "A suitable version of $Dependency was not found (required v$requiredVersion). Downloading portable $Dependency v$downloadVersion..."
|
2016-09-19 11:50:08 +08:00
|
|
|
|
2016-09-20 09:46:02 +08:00
|
|
|
if (!(Test-Path $downloadDir))
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
2016-09-20 09:46:02 +08:00
|
|
|
New-Item -ItemType directory -Path $downloadDir | Out-Null
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
2017-10-13 01:52:00 +08:00
|
|
|
$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)
|
|
|
|
$WC.Proxy.Credentials=$ProxyCred
|
|
|
|
}
|
|
|
|
|
2017-10-18 04:45:41 +08:00
|
|
|
# git and installerbase fail with Start-BitsTransfer
|
|
|
|
if ((Test-Command -commandName 'Start-BitsTransfer') -and ($Dependency -ne "git")-and ($Dependency -ne "installerbase"))
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
2017-10-18 04:45:41 +08:00
|
|
|
try
|
|
|
|
{
|
2017-02-03 08:00:30 +08:00
|
|
|
Start-BitsTransfer -Source $url -Destination $downloadPath -ErrorAction Stop
|
2017-10-18 04:45:41 +08:00
|
|
|
return
|
2017-02-03 08:00:30 +08:00
|
|
|
}
|
2017-10-18 04:45:41 +08:00
|
|
|
catch [System.Exception]
|
|
|
|
{
|
2017-02-03 08:00:30 +08:00
|
|
|
# If BITS fails for any reason, delete any potentially partially downloaded files and continue
|
|
|
|
if (Test-Path $downloadPath)
|
|
|
|
{
|
|
|
|
Remove-Item $downloadPath
|
|
|
|
}
|
|
|
|
}
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
2017-10-18 04:45:41 +08:00
|
|
|
|
|
|
|
Write-Verbose("Downloading $Dependency...")
|
|
|
|
$WC.DownloadFile($url, $downloadPath)
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Enums (without resorting to C#) are only available on powershell 5+.
|
|
|
|
$ExtractionType_NO_EXTRACTION_REQUIRED = 0
|
|
|
|
$ExtractionType_ZIP = 1
|
|
|
|
$ExtractionType_SELF_EXTRACTING_7Z = 2
|
2017-01-24 09:02:43 +08:00
|
|
|
|
|
|
|
|
2016-09-19 11:50:08 +08:00
|
|
|
# Using this to wait for the execution to finish
|
2017-01-24 09:02:43 +08:00
|
|
|
function Invoke-Command()
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
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)
|
2017-01-24 09:02:43 +08:00
|
|
|
if ( $waitForExit )
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
|
|
|
$proc.WaitForExit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function Expand-ZIPFile($file, $destination)
|
|
|
|
{
|
2017-02-16 08:35:51 +08:00
|
|
|
if (!(Test-Path $destination))
|
|
|
|
{
|
|
|
|
New-Item -ItemType Directory -Path $destination | Out-Null
|
|
|
|
}
|
|
|
|
|
2017-10-18 04:19:42 +08:00
|
|
|
if (Test-Command -commandName 'Expand-Archive')
|
2016-09-19 11:50:08 +08:00
|
|
|
{
|
2017-10-18 04:19:42 +08:00
|
|
|
Expand-Archive -path $file -destinationpath $destination
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$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
|
|
|
|
}
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($Dependency -eq "cmake")
|
|
|
|
{
|
2017-10-18 03:41:34 +08:00
|
|
|
$requiredVersion = "3.9.4"
|
|
|
|
$downloadVersion = "3.9.4"
|
|
|
|
$url = "https://cmake.org/files/v3.9/cmake-3.9.4-win32-x86.zip"
|
|
|
|
$downloadPath = "$downloadsDir\cmake-3.9.4-win32-x86.zip"
|
|
|
|
$expectedDownloadedFileHash = "8214df1ff51f9a6a1f0e27f9bd18f402b1749c5b645fbf6e401bcb00047171cd"
|
|
|
|
$executableFromDownload = "$downloadsDir\cmake-3.9.4-win32-x86\bin\cmake.exe"
|
2016-09-19 11:50:08 +08:00
|
|
|
$extractionType = $ExtractionType_ZIP
|
2017-02-16 08:35:51 +08:00
|
|
|
$extractionFolder = $downloadsDir
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
elseif($Dependency -eq "nuget")
|
|
|
|
{
|
2017-10-18 05:19:48 +08:00
|
|
|
$requiredVersion = "4.4.0"
|
|
|
|
$downloadVersion = "4.4.0"
|
|
|
|
$url = "https://dist.nuget.org/win-x86-commandline/v4.4.0/nuget.exe"
|
2017-10-06 05:27:36 +08:00
|
|
|
$downloadPath = "$downloadsDir\nuget-$downloadVersion\nuget.exe"
|
2017-10-18 05:19:48 +08:00
|
|
|
$expectedDownloadedFileHash = "2cf9b118937eef825464e548f0c44f7f64090047746de295d75ac3dcffa3e1f6"
|
2017-01-25 04:30:14 +08:00
|
|
|
$executableFromDownload = $downloadPath
|
2016-09-19 11:50:08 +08:00
|
|
|
$extractionType = $ExtractionType_NO_EXTRACTION_REQUIRED
|
|
|
|
}
|
2017-10-06 05:27:36 +08:00
|
|
|
elseif($Dependency -eq "vswhere")
|
|
|
|
{
|
2017-10-18 05:08:30 +08:00
|
|
|
$requiredVersion = "2.2.7"
|
|
|
|
$downloadVersion = "2.2.7"
|
|
|
|
$url = "https://github.com/Microsoft/vswhere/releases/download/2.2.7/vswhere.exe"
|
2017-10-06 05:27:36 +08:00
|
|
|
$downloadPath = "$downloadsDir\vswhere-$downloadVersion\vswhere.exe"
|
2017-10-18 05:08:30 +08:00
|
|
|
$expectedDownloadedFileHash = "f50303881da706132516d9decfd5314d524a0044daf49c0cfd21dc39c1261ec3"
|
2017-10-06 05:27:36 +08:00
|
|
|
$executableFromDownload = $downloadPath
|
|
|
|
$extractionType = $ExtractionType_NO_EXTRACTION_REQUIRED
|
|
|
|
}
|
2016-09-19 11:50:08 +08:00
|
|
|
elseif($Dependency -eq "git")
|
|
|
|
{
|
2017-10-18 05:05:55 +08:00
|
|
|
$requiredVersion = "2.14.2"
|
|
|
|
$downloadVersion = "2.14.2"
|
|
|
|
$url = "https://github.com/git-for-windows/git/releases/download/v2.14.2.windows.3/MinGit-2.14.2.3-32-bit.zip" # We choose the 32-bit version
|
|
|
|
$downloadPath = "$downloadsDir\MinGit-2.14.2.3-32-bit.zip"
|
|
|
|
$expectedDownloadedFileHash = "7cc1f27e1cfe79381e1a504a5fc7bc33951ac9031cd14c3bf478769d21a26cce"
|
2017-02-16 08:35:51 +08:00
|
|
|
# There is another copy of git.exe in MinGit\bin. However, an installed version of git add the cmd dir to the PATH.
|
2017-01-24 09:02:43 +08:00
|
|
|
# Therefore, choosing the cmd dir here as well.
|
2017-10-18 05:05:55 +08:00
|
|
|
$executableFromDownload = "$downloadsDir\MinGit-2.14.2.3-32-bit\cmd\git.exe"
|
2017-02-16 08:35:51 +08:00
|
|
|
$extractionType = $ExtractionType_ZIP
|
2017-10-18 05:05:55 +08:00
|
|
|
$extractionFolder = "$downloadsDir\MinGit-2.14.2.3-32-bit"
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
2017-09-27 07:57:51 +08:00
|
|
|
elseif($Dependency -eq "installerbase")
|
|
|
|
{
|
|
|
|
$requiredVersion = "3.1.81"
|
|
|
|
$downloadVersion = "3.1.81"
|
|
|
|
$url = "https://github.com/podsvirov/installer-framework/releases/download/cr203958-9/QtInstallerFramework-win-x86.zip"
|
|
|
|
$downloadPath = "$downloadsDir\QtInstallerFramework-win-x86.zip"
|
|
|
|
$expectedDownloadedFileHash = "f2ce23cf5cf9fc7ce409bdca49328e09a070c0026d3c8a04e4dfde7b05b83fe8"
|
|
|
|
$executableFromDownload = "$downloadsDir\QtInstallerFramework-win-x86\bin\installerbase.exe"
|
|
|
|
$extractionType = $ExtractionType_ZIP
|
|
|
|
$extractionFolder = $downloadsDir
|
|
|
|
}
|
2016-09-19 11:50:08 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
throw "Unknown program requested"
|
|
|
|
}
|
|
|
|
|
2017-01-25 04:30:14 +08:00
|
|
|
$downloadSubdir = Split-path $downloadPath -Parent
|
|
|
|
if (!(Test-Path $downloadSubdir))
|
|
|
|
{
|
|
|
|
New-Item -ItemType Directory -Path $downloadSubdir | Out-Null
|
|
|
|
}
|
|
|
|
|
2016-09-20 09:46:02 +08:00
|
|
|
performDownload $Dependency $url $downloadsDir $downloadPath $downloadVersion $requiredVersion
|
2016-09-19 11:50:08 +08:00
|
|
|
|
|
|
|
#calculating the hash
|
2017-10-18 04:58:36 +08:00
|
|
|
if (Test-Command -commandName 'Get-FileHash')
|
|
|
|
{
|
|
|
|
$downloadedFileHash = (Get-FileHash -Path $downloadPath -Algorithm SHA256).Hash
|
|
|
|
}
|
|
|
|
else
|
2017-05-24 02:53:20 +08:00
|
|
|
{
|
|
|
|
$hashAlgorithm = [Security.Cryptography.HashAlgorithm]::Create("SHA256")
|
|
|
|
$fileAsByteArray = [io.File]::ReadAllBytes($downloadPath)
|
|
|
|
$hashByteArray = $hashAlgorithm.ComputeHash($fileAsByteArray)
|
2017-08-16 05:41:23 +08:00
|
|
|
$downloadedFileHash = -Join ($hashByteArray | ForEach-Object {"{0:x2}" -f $_})
|
2017-05-24 02:53:20 +08:00
|
|
|
}
|
|
|
|
|
2016-09-19 11:50:08 +08:00
|
|
|
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
|
|
|
|
{
|
2017-02-16 08:35:51 +08:00
|
|
|
# Expand-Archive $downloadPath -dest "$extractionFolder" -Force # Requires powershell 5+
|
|
|
|
Expand-ZIPFile -File $downloadPath -Destination $extractionFolder
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif($extractionType -eq $ExtractionType_SELF_EXTRACTING_7Z)
|
|
|
|
{
|
|
|
|
if (-not (Test-Path $executableFromDownload))
|
|
|
|
{
|
|
|
|
Invoke-Command $downloadPath "-y" -waitForExit:$true
|
2017-01-24 09:02:43 +08:00
|
|
|
}
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw "Invalid extraction type"
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-not (Test-Path $executableFromDownload))
|
|
|
|
{
|
|
|
|
throw [System.IO.FileNotFoundException] ("Could not detect or download " + $Dependency)
|
|
|
|
}
|
2017-01-25 04:36:46 +08:00
|
|
|
|
2017-03-12 11:08:16 +08:00
|
|
|
return $executableFromDownload
|
2016-09-19 11:50:08 +08:00
|
|
|
}
|
|
|
|
|
2017-05-07 12:37:04 +08:00
|
|
|
SelectProgram $Dependency
|
|
|
|
|
|
|
|
Write-Verbose "Fetching dependency: $Dependency. Done."
|