Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit 1f60ff3

Browse files
Ensure build script works on VS2019-only machine (#307)
1 parent e35fd87 commit 1f60ff3

1 file changed

Lines changed: 44 additions & 37 deletions

File tree

build/common.ps1

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ $PrivateRoot = Join-Path $PSScriptRoot "private"
77
$DotNetExe = Join-Path $CLIRoot 'dotnet.exe'
88
$NuGetExe = Join-Path $NuGetClientRoot '.nuget\nuget.exe'
99
$7zipExe = Join-Path $NuGetClientRoot 'tools\7zip\7za.exe'
10+
$BuiltInVsWhereExe = "${Env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
1011
$Artifacts = Join-Path $NuGetClientRoot artifacts
1112
$MSBuildRoot = Join-Path ${env:ProgramFiles(x86)} 'MSBuild\'
1213
$MSBuildExeRelPath = 'bin\msbuild.exe'
13-
$VisualStudioVersion = 14.0
14+
$VisualStudioVersion = 15.0
1415

1516
$NuGetBuildPackageId = 'NuGet.Services.Build'
1617
$NuGetBuildPackageVersion = '1.0.0'
@@ -77,49 +78,55 @@ Function Clear-Artifacts {
7778
}
7879
}
7980

81+
Function Get-LatestVisualStudioRoot {
82+
83+
if (Test-Path $BuiltInVsWhereExe) {
84+
$installationPath = & $BuiltInVsWhereExe -latest -prerelease -property installationPath
85+
$installationVersion = & $BuiltInVsWhereExe -latest -prerelease -property installationVersion
86+
Verbose-Log "Found Visual Studio at '$installationPath' version '$installationVersion' with '$BuiltInVsWhereExe'"
87+
# Set the fallback version
88+
$majorVersion = "$installationVersion".Split('.')[0]
89+
$script:FallbackVSVersion = "$majorVersion.0"
90+
91+
return $installationPath
92+
}
93+
94+
Error-Log "Could not find a compatible Visual Studio Version because $BuiltInVsWhereExe does not exist" -Fatal
95+
}
96+
8097
Function Get-MSBuildExe {
8198
param(
82-
[int]$MSBuildVersion
99+
[ValidateSet("15", "16", $null)]
100+
[string]$MSBuildVersion
83101
)
84-
85-
$MSBuildPath = $null
86102

87-
if ($MSBuildVersion -lt 15) {
88-
$MSBuildExe = Join-Path $MSBuildRoot ([string]$MSBuildVersion + ".0")
89-
$MSBuildPath = Join-Path $MSBuildExe $MSBuildExeRelPath
103+
if(-not $MSBuildVersion){
104+
$MSBuildVersion = Get-VSMajorVersion
105+
}
106+
107+
$CommonToolsVar = "Env:VS${MSBuildVersion}0COMNTOOLS"
108+
if (Test-Path $CommonToolsVar) {
109+
$CommonToolsValue = gci $CommonToolsVar | select -expand value -ea Ignore
110+
$MSBuildRoot = Join-Path $CommonToolsValue '..\..\MSBuild' -Resolve
90111
} else {
91-
# Check if VS package to use to find $NuGetBuildPackageId is installed. If not, install it.
92-
$buildPackageFound = [System.AppDomain]::CurrentDomain.GetAssemblies() | `
93-
Where-Object { $_.FullName -like "$($NuGetBuildPackageId), *" }
94-
if (-not $buildPackageFound)
95-
{
96-
Trace-Log "Installing and configuring $NuGetBuildPackageId"
97-
$opts = "install", $NuGetBuildPackageId, `
98-
"-Version", $NuGetBuildPackageVersion, `
99-
"-Source", "https://api.nuget.org/v3/index.json;https://dotnet.myget.org/F/nuget-build/api/v3/index.json", `
100-
"-OutputDirectory", "$PSScriptRoot\packages"
101-
& $NuGetExe $opts | Out-Null
102-
if (-not $?) {
103-
Error-Log "Failed to install package $NuGetBuildPackageId $NuGetBuildPackageVersion!"
104-
} else {
105-
Add-Type -Path "$PSScriptRoot\packages\$NuGetBuildPackageId.$NuGetBuildPackageVersion\lib\net452\$NuGetBuildPackageId.dll" | Out-Null
106-
}
107-
}
108-
109-
$installations = @([NuGet.Services.Build.VisualStudioSetupConfigurationHelper]::GetInstancePaths() | ForEach-Object {
110-
$MSBuildRoot = Join-Path "$_\MSBuild" ([string]$MSBuildVersion + ".0")
111-
Join-Path $MSBuildRoot $MSBuildExeRelPath
112-
} | Where-Object { Test-Path $_ })
113-
114-
if ($installations.Count -ge 1) {
115-
$MSBuildPath = $installations[0]
116-
} else {
117-
Error-Log "Failed to find MSBuild $MSBuildVersion!"
112+
$VisualStudioRoot = Get-LatestVisualStudioRoot
113+
if ($VisualStudioRoot -and (Test-Path $VisualStudioRoot)) {
114+
$MSBuildRoot = Join-Path $VisualStudioRoot 'MSBuild'
118115
}
119116
}
120-
121-
Trace-Log "MSBuild found at $MSBuildPath"
122-
$MSBuildPath
117+
118+
$MSBuildExe = Join-Path $MSBuildRoot 'Current\bin\msbuild.exe'
119+
120+
if (-not (Test-Path $MSBuildExe)) {
121+
$MSBuildExe = Join-Path $MSBuildRoot "${MSBuildVersion}.0\bin\msbuild.exe"
122+
}
123+
124+
if (Test-Path $MSBuildExe) {
125+
Verbose-Log "Found MSBuild.exe at `"$MSBuildExe`""
126+
$MSBuildExe
127+
} else {
128+
Error-Log 'Could not find MSBuild.exe' -Fatal
129+
}
123130
}
124131

125132
Function Invoke-BuildStep {

0 commit comments

Comments
 (0)