@@ -17,10 +17,20 @@ function Get-MSBuildPath {
1717 # Only attempt to find Microsoft.Build.Utilities.Core.dll from a VS 15 Willow install
1818 # when "15.0" or latest is specified. In 15.0, the method GetPathToBuildToolsFile(...)
1919 # has regressed. When it is called for a version that is not found, the latest version
20- # found is returned instead.
20+ # found is returned instead. Same for "16.0"
2121 [System.Reflection.Assembly ]$msUtilities = $null
22- if (($Version -eq " 15.0" -or ! $Version ) -and # !$Version indicates "latest"
23- ($visualStudio15 = Get-VisualStudio_15_0 ) -and
22+ if (($Version -eq " 16.0" -or ! $Version ) -and # !$Version indicates "latest"
23+ ($visualStudio16 = Get-VisualStudio 16 ) -and
24+ $visualStudio16.installationPath ) {
25+
26+ $msbuildUtilitiesPath = [System.IO.Path ]::Combine($visualStudio16.installationPath , " MSBuild\Current\Bin\Microsoft.Build.Utilities.Core.dll" )
27+ if (Test-Path - LiteralPath $msbuildUtilitiesPath - PathType Leaf) {
28+ Write-Verbose " Loading $msbuildUtilitiesPath "
29+ $msUtilities = [System.Reflection.Assembly ]::LoadFrom($msbuildUtilitiesPath )
30+ }
31+ }
32+ elseif (($Version -eq " 15.0" -or ! $Version ) -and # !$Version indicates "latest"
33+ ($visualStudio15 = Get-VisualStudio 15 ) -and
2434 $visualStudio15.installationPath ) {
2535
2636 $msbuildUtilitiesPath = [System.IO.Path ]::Combine($visualStudio15.installationPath , " MSBuild\15.0\Bin\Microsoft.Build.Utilities.Core.dll" )
@@ -65,13 +75,13 @@ function Get-MSBuildPath {
6575 [type ]$t = $msUtilities.GetType (' Microsoft.Build.Utilities.ToolLocationHelper' )
6676 if ($t -ne $null ) {
6777 # Attempt to load the method info for GetPathToBuildToolsFile. This method
68- # is available in the 15.0, 14.0, and 12.0 utilities DLL. It is not available
78+ # is available in the 16.0, 15.0, 14.0, and 12.0 utilities DLL. It is not available
6979 # in the 4.0 utilities DLL.
7080 [System.Reflection.MethodInfo ]$mi = $t.GetMethod (
7181 " GetPathToBuildToolsFile" ,
7282 [type []]@ ( [string ], [string ], $msUtilities.GetType (" Microsoft.Build.Utilities.DotNetFrameworkArchitecture" ) ))
7383 if ($mi -ne $null -and $mi.GetParameters ().Length -eq 3 ) {
74- $versions = " 15.0" , " 14.0" , " 12.0" , " 4.0"
84+ $versions = " 16.0 " , " 15.0" , " 14.0" , " 12.0" , " 4.0"
7585 if ($Version ) {
7686 $versions = @ ( $Version )
7787 }
@@ -169,21 +179,24 @@ function Get-SolutionFiles {
169179 }
170180}
171181
172- function Get-VisualStudio_15_0 {
182+ function Get-VisualStudio {
173183 [CmdletBinding ()]
174- param ()
184+ param (
185+ [Parameter (Mandatory = $true )]
186+ [ValidateSet (15 , 16 )]
187+ [int ]$MajorVersion )
175188
176189 Trace-VstsEnteringInvocation $MyInvocation
177190 try {
178- if (! $script :visualStudioCache.ContainsKey (' 15.0 ' )) {
191+ if (! $script :visualStudioCache.ContainsKey (" $MajorVersion .0 " )) {
179192 try {
180- # Query for the latest 15 .* version.
193+ # Query for the latest $MajorVersion .* version.
181194 #
182- # Note, the capability is registered as VisualStudio_15 .0, however the actual version
183- # may be something like 15 .2.
184- Write-Verbose " Getting latest Visual Studio 15 setup instance."
195+ # Note, the capability is registered as VisualStudio_16 .0, however the actual version
196+ # may be something like 16 .2.
197+ Write-Verbose " Getting latest Visual Studio $MajorVersion setup instance."
185198 $output = New-Object System.Text.StringBuilder
186- Invoke-VstsTool - FileName " $PSScriptRoot \vswhere.exe" - Arguments " -version [15 .0,16 .0) -latest -format json" - RequireExitCodeZero 2>&1 |
199+ Invoke-VstsTool - FileName " $PSScriptRoot \vswhere.exe" - Arguments " -version [$MajorVersion .0,$ ( $MajorVersion + 1 ) .0) -latest -format json" - RequireExitCodeZero 2>&1 |
187200 ForEach-Object {
188201 if ($_ -is [System.Management.Automation.ErrorRecord ]) {
189202 Write-Verbose " STDERR: $ ( $_.Exception.Message ) "
@@ -193,16 +206,16 @@ function Get-VisualStudio_15_0 {
193206 $null = $output.AppendLine ($_ )
194207 }
195208 }
196- $script :visualStudioCache [' 15.0 ' ] = (ConvertFrom-Json - InputObject $output.ToString ()) |
209+ $script :visualStudioCache [" $MajorVersion .0 " ] = (ConvertFrom-Json - InputObject $output.ToString ()) |
197210 Select-Object - First 1
198- if (! $script :visualStudioCache [' 15.0 ' ]) {
199- # Query for the latest 15 .* BuildTools.
211+ if (! $script :visualStudioCache [" $MajorVersion .0 " ]) {
212+ # Query for the latest $MajorVersion .* BuildTools.
200213 #
201- # Note, whereas VS 15 .x version number is always 15 .0.*, BuildTools does not follow the
202- # the same scheme. It appears to follow the 15 .<UPDATE_NUMBER>.* versioning scheme.
203- Write-Verbose " Getting latest BuildTools 15 setup instance."
214+ # Note, whereas VS 16 .x version number is always 16 .0.*, BuildTools does not follow the
215+ # the same scheme. It appears to follow the 16 .<UPDATE_NUMBER>.* versioning scheme.
216+ Write-Verbose " Getting latest BuildTools 16 setup instance."
204217 $output = New-Object System.Text.StringBuilder
205- Invoke-VstsTool - FileName " $PSScriptRoot \vswhere.exe" - Arguments " -version [15 .0,16 .0) -products Microsoft.VisualStudio.Product.BuildTools -latest -format json" - RequireExitCodeZero 2>&1 |
218+ Invoke-VstsTool - FileName " $PSScriptRoot \vswhere.exe" - Arguments " -version [$MajorVersion .0,$ ( $MajorVersion + 1 ) .0) -products Microsoft.VisualStudio.Product.BuildTools -latest -format json" - RequireExitCodeZero 2>&1 |
206219 ForEach-Object {
207220 if ($_ -is [System.Management.Automation.ErrorRecord ]) {
208221 Write-Verbose " STDERR: $ ( $_.Exception.Message ) "
@@ -212,21 +225,20 @@ function Get-VisualStudio_15_0 {
212225 $null = $output.AppendLine ($_ )
213226 }
214227 }
215- $script :visualStudioCache [' 15.0 ' ] = (ConvertFrom-Json - InputObject $output.ToString ()) |
228+ $script :visualStudioCache [" $MajorVersion .0 " ] = (ConvertFrom-Json - InputObject $output.ToString ()) |
216229 Select-Object - First 1
217230 }
218231 } catch {
219232 Write-Verbose ($_ | Out-String )
220- $script :visualStudioCache [' 15.0 ' ] = $null
233+ $script :visualStudioCache [" $MajorVersion .0 " ] = $null
221234 }
222235 }
223236
224- return $script :visualStudioCache [' 15.0 ' ]
237+ return $script :visualStudioCache [" $MajorVersion .0 " ]
225238 } finally {
226239 Trace-VstsLeavingInvocation $MyInvocation
227240 }
228241}
229-
230242function Select-MSBuildPath {
231243 [CmdletBinding ()]
232244 param (
@@ -261,7 +273,7 @@ function Select-MSBuildPath {
261273 }
262274
263275 $specificVersion = $PreferredVersion -and $PreferredVersion -ne ' latest'
264- $versions = ' 15.0' , ' 14.0' , ' 12.0' , ' 4.0' | Where-Object { $_ -ne $PreferredVersion }
276+ $versions = " 16.0 " , ' 15.0' , ' 14.0' , ' 12.0' , ' 4.0' | Where-Object { $_ -ne $PreferredVersion }
265277
266278 # Look for a specific version of MSBuild.
267279 if ($specificVersion ) {
0 commit comments