From f956053d4ca719edc1ea1f176137716b44a9cd9c Mon Sep 17 00:00:00 2001 From: Pavel Dubsky Date: Tue, 28 May 2024 14:47:31 +0200 Subject: [PATCH] Add GetVsInstallationPath and GetVsProperty helper functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduce GetVsProperty helper function that allows to retrieve Visual Studio installation property by providing required component (optional). It also offers the ability to choose which version of Visual Studio to use (the oldest or the newest). Also add GetVsInstallationPath helper function which is a handy shortcut to retrieve installation path property. Pick-to: 6.7 6.5 Change-Id: I90afca74db9a6f87b622c7fafec2243e085e89e0 Reviewed-by: Jøger Hansegård (cherry picked from commit 71b10bfe4839d9a7df69c7e4dfef6e987ddc2f77) Reviewed-by: Qt Cherry-pick Bot --- coin/provisioning/common/windows/helpers.ps1 | 53 +++++++++++++++++--- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/coin/provisioning/common/windows/helpers.ps1 b/coin/provisioning/common/windows/helpers.ps1 index 9fbf27aa..ad01c499 100644 --- a/coin/provisioning/common/windows/helpers.ps1 +++ b/coin/provisioning/common/windows/helpers.ps1 @@ -297,13 +297,52 @@ function DeleteSchedulerTask { SCHTASKS /DELETE /TN "Microsoft\Windows\$Task" /F } -function GetVSPath { +function GetVsProperty { Param ( - [string]$VSWhere = "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe", - [string]$Component = "Microsoft.VisualStudio.Component.VC.Tools.x86.x64" + [string]$Component = 'Microsoft.VisualStudio.Component.VC.CoreIde', + [string]$Property, + [switch]$Latest ) - return (& $VSWhere -nologo -latest -products * -requires $Component -property installationPath) + $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 { @@ -312,13 +351,11 @@ function EnterVSDevShell { [string]$Arch = "amd64" ) - $vsWere = "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" - $vcComponent = "Microsoft.VisualStudio.Component.VC.CoreIde" # 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 = (& $vsWere -nologo -products * -requires $vcComponent -sort -format value -property installationPath | Select-Object -Last 1) + $VSPath = GetVsInstallationPath - Write-Host "Enter VisualStudio developer shell (-host_arch=$HostArch -arch=$Arch)" + 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"