diff options
Diffstat (limited to 'coin/provisioning/common/windows/helpers.ps1')
-rw-r--r-- | coin/provisioning/common/windows/helpers.ps1 | 108 |
1 files changed, 107 insertions, 1 deletions
diff --git a/coin/provisioning/common/windows/helpers.ps1 b/coin/provisioning/common/windows/helpers.ps1 index 8599791d..ad01c499 100644 --- a/coin/provisioning/common/windows/helpers.ps1 +++ b/coin/provisioning/common/windows/helpers.ps1 @@ -148,7 +148,7 @@ function Download if ($CachedUrl.StartsWith("http")) { Invoke-WebRequest -UseBasicParsing $CachedUrl -OutFile $Destination } else { - Copy-Item $CachedUrl $Destination + Copy-Item $CachedUrl $Destination -errorAction stop } } catch { Write-Host "Cached download failed: Downloading from official location: $OfficialUrl" @@ -196,6 +196,28 @@ function Is64BitWinHost return [environment]::Is64BitOperatingSystem } +enum CpuArch { + x64 + x86 + arm64 + unknown +} + +function Get-CpuArchitecture +{ + # Possible values are "AMD64", "IA64", "ARM64", and "x86" + $arch = [System.Environment]::GetEnvironmentVariable('PROCESSOR_ARCHITECTURE', 'Machine') + if ($arch -eq "AMD64") { + return [CpuArch]::x64 + } elseif ($arch -eq "x86") { + return [CpuArch]::x86 + } elseif ($arch -eq "ARM64") { + return [CpuArch]::arm64 + } + + return [CpuArch]::unknown +} + function IsProxyEnabled { return (Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').proxyEnable } @@ -274,3 +296,87 @@ function DeleteSchedulerTask { Write-Host "Disabling $Task from Task Scheduler" SCHTASKS /DELETE /TN "Microsoft\Windows\$Task" /F } + +function GetVsProperty { + Param ( + [string]$Component = 'Microsoft.VisualStudio.Component.VC.CoreIde', + [string]$Property, + [switch]$Latest + ) + + $vsWhereProcessInfo = New-Object System.Diagnostics.ProcessStartInfo + $vsWhereProcessInfo.FileName = "${Env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" + $vsWhereProcessInfo.RedirectStandardError = $true + $vsWhereProcessInfo.RedirectStandardOutput = $true + $vsWhereProcessInfo.UseShellExecute = $false + + # -sort: sorts the instances from newest version and last installed to oldest + $vsWhereProcessInfo.Arguments = " -nologo -sort -products * -requires $Component -property $Property" + if ($Latest) { + # -latest: return only the newest version and last installed + $vsWhereProcessInfo.Arguments += ' -latest' + } + + $vsWhereProcess = New-Object System.Diagnostics.Process + $vsWhereProcess.StartInfo = $vsWhereProcessInfo + + $vsWhereProcess.Start() | Out-Null + $vsWhereProcess.WaitForExit() + + $standardOutput = $vsWhereProcess.StandardOutput.ReadToEnd() + if ([string]::IsNullOrEmpty($standardOutput)) { + throw "vswhere could not find property '$Property'" + } + + $exitCode = $vsWhereProcess.ExitCode + if ($exitCode -ne 0) { + $standardError = $vsWhereProcess.StandardError.ReadToEnd() + throw "vswhere failed with exit code $exitCode ($standardError)" + } + + return $standardOutput.Split([Environment]::NewLine, [StringSplitOptions]::RemoveEmptyEntries) | Select-Object -Last 1 +} + +function GetVsInstallationPath { + Param ( + [switch]$Latest + ) + + return GetVsProperty -Property 'installationPath' @PSBoundParameters +} + +function EnterVSDevShell { + Param ( + [string]$HostArch = "amd64", + [string]$Arch = "amd64" + ) + + # We pick the oldest build tools we can find and use that to be compatible with it and any newer version: + # If MSVC has an ABI break this will stop working, and yet another build must be added. + $VSPath = GetVsInstallationPath + + Write-Host "Enter VisualStudio developer shell (-host_arch=$HostArch -arch=$Arch -VsInstallPath='$VSPath')" + try { + Import-Module "$VSPath\Common7\Tools\Microsoft.VisualStudio.DevShell.dll" + Enter-VsDevShell -VsInstallPath $VSPath -DevCmdArguments "-host_arch=$HostArch -arch=$Arch -no_logo" + } catch { + Write-Host "Failed to enter VisualStudio DevShell" + return $false + } + return $true +} + +function Invoke-MtCommand { + param( + [String] $vcVarsScript, + [String] $arch, + [String] $manifest, + [String] $executable + ) + $tempFile = [IO.Path]::GetTempFileName() + Add-Content -Path $tempFile -Value $manifest + $cmdLine = """$vcVarsScript"" $arch & mt.exe -manifest ""$tempFile"" -outputresource:""$executable"";1" + Write-Output Executing $cmdLine + & $Env:SystemRoot\system32\cmd.exe /c $cmdLine | Write-Output + Remove-Item $tempFile +} |