diff --git a/eng/scripts/common.psm1 b/eng/scripts/common.psm1 new file mode 100644 index 0000000000..3a31f2a023 --- /dev/null +++ b/eng/scripts/common.psm1 @@ -0,0 +1,122 @@ +$ErrorActionPreference = 'Stop' +# Update the default TLS support to 1.2 +[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 + +function Invoke-Block([scriptblock]$cmd, [string]$WorkingDir = $null) { + if ($WorkingDir) { + Push-Location $WorkingDir + } + + try { + + $cmd | Out-String | Write-Verbose + & $cmd + + # Need to check both of these cases for errors as they represent different items + # - $?: did the powershell script block throw an error + # - $lastexitcode: did a windows command executed by the script block end in error + if ((-not $?) -or ($lastexitcode -ne 0)) { + if ($error -ne $null) + { + Write-Warning $error[0] + } + throw "Command failed to execute: $cmd" + } + } + finally { + if ($WorkingDir) { + Pop-Location + } + } +} + +function SaveXml([xml]$xml, [string]$path) { + Write-Verbose "Saving to $path" + $ErrorActionPreference = 'stop' + + $settings = New-Object System.XML.XmlWriterSettings + $settings.OmitXmlDeclaration = $true + $settings.Encoding = New-Object System.Text.UTF8Encoding( $true ) + $writer = [System.XML.XMLTextWriter]::Create($path, $settings) + $xml.Save($writer) + $writer.Close() +} + +function LoadXml([string]$path) { + Write-Verbose "Reading from $path" + + $ErrorActionPreference = 'stop' + $obj = new-object xml + $obj.PreserveWhitespace = $true + $obj.Load($path) + return $obj +} + +function Get-MSBuildPath { + param( + [switch]$Prerelease, + [string[]]$Requires + ) + + $vsInstallDir = $null + if ($env:VSINSTALLDIR -and (Test-Path $env:VSINSTALLDIR)) { + $vsInstallDir = $env:VSINSTALLDIR + Write-Verbose "Using VSINSTALLDIR=$vsInstallDir" + } + else { + $vswhere = "${env:ProgramFiles(x86)}/Microsoft Visual Studio/Installer/vswhere.exe" + Write-Verbose "Using vswhere.exe from $vswhere" + + if (-not (Test-Path $vswhere)) { + Write-Error "Missing prerequisite: could not find vswhere" + } + + [string[]] $vswhereArgs = @() + + if ($Prerelease) { + $vswhereArgs += '-prerelease' + } + + if ($Requires) { + foreach ($r in $Requires) { + $vswhereArgs += '-requires', $r + } + } + + $installs = & $vswhere -format json -version '[15.0, 16.0)' -latest -products * @vswhereArgs | ConvertFrom-Json + if (!$installs) { + Write-Error "Missing prerequisite: could not find any installations of Visual Studio" + } + + $vs = $installs | Select-Object -First 1 + $vsInstallDir = $vs.installationPath + Write-Host "Using $($vs.displayName)" + } + + $msbuild = Join-Path $vsInstallDir 'MSBuild/15.0/bin/msbuild.exe' + if (!(Test-Path $msbuild)) { + Write-Error "Missing prerequisite: could not find msbuild.exe" + } + return $msbuild +} + +function Get-RemoteFile([string]$RemotePath, [string]$LocalPath) { + if ($RemotePath -notlike 'http*') { + Copy-Item $RemotePath $LocalPath + return + } + + $retries = 10 + while ($retries -gt 0) { + $retries -= 1 + try { + Invoke-WebRequest -UseBasicParsing -Uri $RemotePath -OutFile $LocalPath + return + } + catch { + Write-Verbose "Request failed. $retries retries remaining" + } + } + + Write-Error "Download failed: '$RemotePath'." +} diff --git a/src/Installers/Windows/build.ps1 b/src/Installers/Windows/build.ps1 index a3629b2683..c1fbff4043 100644 --- a/src/Installers/Windows/build.ps1 +++ b/src/Installers/Windows/build.ps1 @@ -20,7 +20,7 @@ param( $ErrorActionPreference = 'Stop' $repoRoot = Resolve-Path "$PSScriptRoot/../../../" -Import-Module -Scope Local "$repoRoot/scripts/common.psm1" -Force +Import-Module -Scope Local "$repoRoot/eng/scripts/common.psm1" -Force $msbuild = Get-MSBuildPath -Prerelease -requires 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64' $harvestRoot = "$repoRoot/obj/sfx/" diff --git a/test/Cli.FunctionalTests/run-tests.ps1 b/test/Cli.FunctionalTests/run-tests.ps1 index 325ca8a470..1b66b79e14 100644 --- a/test/Cli.FunctionalTests/run-tests.ps1 +++ b/test/Cli.FunctionalTests/run-tests.ps1 @@ -51,7 +51,7 @@ $ErrorActionPreference = 'Stop' Set-StrictMode -Version 1 $repoRoot = Resolve-Path "$PSScriptRoot/../../" -Import-Module "$repoRoot/scripts/common.psm1" -Scope Local -Force +Import-Module "$repoRoot/eng/scripts/common.psm1" -Scope Local -Force # This ID corresponds to the ProdCon build number Write-Host "ProductBuildId: $env:PRODUCTBUILDID"