From 09ce7185499f7587f6e6cfb941ec65391e13ee99 Mon Sep 17 00:00:00 2001 From: "Dariy.Nurgaleev" Date: Thu, 19 Mar 2020 02:38:23 +0700 Subject: [PATCH] added Start-DownloadWithRetry function --- .../scripts/ImageHelpers/InstallHelpers.ps1 | 166 +++++++++++++++++- 1 file changed, 165 insertions(+), 1 deletion(-) diff --git a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 b/images/win/scripts/ImageHelpers/InstallHelpers.ps1 index f6c553b2c..201dea865 100644 --- a/images/win/scripts/ImageHelpers/InstallHelpers.ps1 +++ b/images/win/scripts/ImageHelpers/InstallHelpers.ps1 @@ -161,7 +161,134 @@ Hashtable for service arguments } } -function Get-VS19ExtensionVersion + +function Start-DownloadWithRetry +{ + param ( + [Parameter(Mandatory)] + [string] $Url, + [string] $FullFilePath = "${env:Temp}\Default.exe", + [int] $retries = 20 + ) + #Default retry logic for the package. + while($retries -gt 0) + { + try + { + Write-Host "Downloading package from: $Url to path $FilePath ." + (New-Object System.Net.WebClient).DownloadFile($Url, $FilePath) + break + } + catch + { + Write-Host "There is an error during package downloading" + $_ + + $retries-- + + if ($retries -eq 0) + { + Write-Host "File can't be downloaded. Please try later or check that file exists by url: $Url" + $_ + exit 1 + } + + Write-Host "Waiting 30 seconds before retrying. Retries left: $retries" + Start-Sleep -Seconds 30 + } + } + return $FullFilePath +} + + +function Install-VsixExtension +{ + Param + ( + [string] $Url, + [Parameter(Mandatory = $true)] + [string] $Name, + [string] $FilePath, + [Parameter(Mandatory = $true)] + [string] $VSversion, + [int] $retries = 20, + [switch] $InstallOnly + ) + + if (!$InstallOnly) + { + $FilePath = "${env:Temp}\$Name" + + while($retries -gt 0) + { + try + { + Write-Host "Downloading $Name..." + (New-Object System.Net.WebClient).DownloadFile($Url, $FilePath) + break + } + catch + { + Write-Host "There is an error during $Name downloading" + $_ + + $retries-- + + if ($retries -eq 0) + { + Write-Host "File can't be downloaded" + $_ + exit 1 + } + + Write-Host "Waiting 30 seconds before retrying. Retries left: $retries" + Start-Sleep -Seconds 30 + } + } + } + + $ArgumentList = ('/quiet', "`"$FilePath`"") + + Write-Host "Starting Install $Name..." + try + { + #There are 2 types of packages at the moment - exe and vsix + if ($Name -match "vsix") + { + $process = Start-Process -FilePath "C:\Program Files (x86)\Microsoft Visual Studio\$VSversion\Enterprise\Common7\IDE\VSIXInstaller.exe" -ArgumentList $ArgumentList -Wait -PassThru + } + else + { + $process = Start-Process -FilePath ${env:Temp}\$Name /Q -Wait -PassThru + } + } + catch + { + Write-Host "There is an error during $Name installation" + $_ + exit 1 + } + + $exitCode = $process.ExitCode + + if ($exitCode -eq 0 -or $exitCode -eq 1001) # 1001 means the extension is already installed + { + Write-Host "$Name installed successfully" + } + else + { + Write-Host "Unsuccessful exit code returned by the installation process: $exitCode." + exit 1 + } + + #Cleanup downloaded installation files + if (!$InstallOnly) + { + Remove-Item -Force -Confirm:$false $FilePath + } +} + +function Get-VSExtensionVersion { param ( [string] [Parameter(Mandatory=$true)] $packageName @@ -187,3 +314,40 @@ function Get-VS19ExtensionVersion return $packageVersion } + + +function Get-ToolcachePackages { + $toolcachePath = Join-Path $env:ROOT_FOLDER "toolcache.json" + Get-Content -Raw $toolcachePath | ConvertFrom-Json +} + +function Get-ToolsByName { + param ( + [Parameter(Mandatory = $True)] + [string]$SoftwareName + ) + + (Get-ToolcachePackages).PSObject.Properties | Where-Object { $_.Name -match $SoftwareName } | ForEach-Object { + $packageNameParts = $_.Name.Split("-") + [PSCustomObject] @{ + ToolName = $packageNameParts[1] + Versions = $_.Value + Architecture = $packageNameParts[3,4] -join "-" + } + } +} + +function Get-WinVersion +{ + (Get-WmiObject -class Win32_OperatingSystem).Caption +} + +function Test-IsWin19 +{ + (Get-WinVersion) -match "2019" +} + +function Test-IsWin16 +{ + (Get-WinVersion) -match "2016" +}