Skip to content

Commit 3bbe8e0

Browse files
Microsoft/users/aujensen/vs2019 vs task (#9273) (#9318)
VS2019 support for VSBuild and MSBuild tasks
1 parent aef4de5 commit 3bbe8e0

34 files changed

+264
-62
lines changed

Tasks/Common/MSBuildHelpers/MSBuildHelpers.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ Export-ModuleMember -Function @(
1212
# Path functions.
1313
'Get-MSBuildPath'
1414
'Get-SolutionFiles'
15-
'Get-VisualStudio_15_0'
15+
'Get-VisualStudio'
1616
'Select-MSBuildPath'
1717
)

Tasks/Common/MSBuildHelpers/PathFunctions.ps1

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
230242
function 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) {

Tasks/Common/MSBuildHelpers/Tests/Get-VisualStudio_15_0.CachesNotFoundResult.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ Register-Mock Invoke-VstsTool {
1818
} -- -FileName (Resolve-Path $PSScriptRoot\..\vswhere.exe).Path -Arguments "-version [15.0,16.0) -products Microsoft.VisualStudio.Product.BuildTools -latest -format json" -RequireExitCodeZero
1919

2020
# Act.
21-
$null = Get-VisualStudio_15_0
22-
$actual = Get-VisualStudio_15_0
21+
$null = Get-VisualStudio 15
22+
$actual = Get-VisualStudio 15
2323

2424
# Assert.
2525
Assert-AreEqual -Expected $null -Actual $actual

Tasks/Common/MSBuildHelpers/Tests/Get-VisualStudio_15_0.CachesResult.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Register-Mock Invoke-VstsTool {
1313
} -- -FileName (Resolve-Path $PSScriptRoot\..\vswhere.exe).Path -Arguments "-version [15.0,16.0) -latest -format json" -RequireExitCodeZero
1414

1515
# Act.
16-
$null = Get-VisualStudio_15_0
17-
$actual = Get-VisualStudio_15_0
16+
$null = Get-VisualStudio 15
17+
$actual = Get-VisualStudio 15
1818

1919
# Assert.
2020
Assert-AreEqual -Expected "path1" -Actual $actual.installationPath

Tasks/Common/MSBuildHelpers/Tests/Get-VisualStudio_15_0.FallsBackToBuildTools.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Register-Mock Invoke-VstsTool {
2020
} -- -FileName (Resolve-Path $PSScriptRoot\..\vswhere.exe).Path -Arguments "-version [15.0,16.0) -products Microsoft.VisualStudio.Product.BuildTools -latest -format json" -RequireExitCodeZero
2121

2222
# Act.
23-
$null = Get-VisualStudio_15_0
24-
$actual = Get-VisualStudio_15_0
23+
$null = Get-VisualStudio 15
24+
$actual = Get-VisualStudio 15
2525

2626
# Assert.
2727
Assert-AreEqual -Expected "build tools path" -Actual $actual.installationPath

Tasks/Common/MSBuildHelpers/Tests/Get-VisualStudio_15_0.IgnoresStderr.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Register-Mock Invoke-VstsTool {
1414
} -- -FileName (Resolve-Path $PSScriptRoot\..\vswhere.exe).Path -Arguments "-version [15.0,16.0) -latest -format json" -RequireExitCodeZero
1515

1616
# Act.
17-
$actual = Get-VisualStudio_15_0
17+
$actual = Get-VisualStudio 15
1818

1919
# Assert.
2020
Assert-AreEqual -Expected "some path" -Actual $actual.installationPath
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[CmdletBinding()]
2+
param()
3+
4+
# Arrange.
5+
. $PSScriptRoot\..\..\..\..\Tests\lib\Initialize-Test.ps1
6+
Microsoft.PowerShell.Core\Import-Module $PSScriptRoot\..
7+
$script:vsCount = 0
8+
$script:buildToolsCount = 0
9+
Register-Mock Invoke-VstsTool {
10+
$script:vsCount++
11+
"["
12+
"]"
13+
} -- -FileName (Resolve-Path $PSScriptRoot\..\vswhere.exe).Path -Arguments "-version [16.0,17.0) -latest -format json" -RequireExitCodeZero
14+
Register-Mock Invoke-VstsTool {
15+
$script:buildToolsCount++
16+
"["
17+
"]"
18+
} -- -FileName (Resolve-Path $PSScriptRoot\..\vswhere.exe).Path -Arguments "-version [16.0,17.0) -products Microsoft.VisualStudio.Product.BuildTools -latest -format json" -RequireExitCodeZero
19+
20+
# Act.
21+
$null = Get-VisualStudio 16
22+
$actual = Get-VisualStudio 16
23+
24+
# Assert.
25+
Assert-AreEqual -Expected $null -Actual $actual
26+
Assert-AreEqual -Expected 1 -Actual $script:vsCount
27+
Assert-AreEqual -Expected 1 -Actual $script:buildToolsCount
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[CmdletBinding()]
2+
param()
3+
4+
# Arrange.
5+
. $PSScriptRoot\..\..\..\..\Tests\lib\Initialize-Test.ps1
6+
Microsoft.PowerShell.Core\Import-Module $PSScriptRoot\..
7+
Register-Mock Invoke-VstsTool {
8+
"["
9+
" {"
10+
" `"installationPath`": `"path1`""
11+
" }"
12+
"]"
13+
} -- -FileName (Resolve-Path $PSScriptRoot\..\vswhere.exe).Path -Arguments "-version [16.0,17.0) -latest -format json" -RequireExitCodeZero
14+
15+
# Act.
16+
$null = Get-VisualStudio 16
17+
$actual = Get-VisualStudio 16
18+
19+
# Assert.
20+
Assert-AreEqual -Expected "path1" -Actual $actual.installationPath
21+
Assert-WasCalled Invoke-VstsTool -Times 1
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[CmdletBinding()]
2+
param()
3+
4+
# Arrange.
5+
. $PSScriptRoot\..\..\..\..\Tests\lib\Initialize-Test.ps1
6+
Microsoft.PowerShell.Core\Import-Module $PSScriptRoot\..
7+
$script:vsCount = 0
8+
$script:buildToolsCount = 0
9+
Register-Mock Invoke-VstsTool {
10+
$script:vsCount++
11+
"[]"
12+
} -- -FileName (Resolve-Path $PSScriptRoot\..\vswhere.exe).Path -Arguments "-version [16.0,17.0) -latest -format json" -RequireExitCodeZero
13+
Register-Mock Invoke-VstsTool {
14+
$script:buildToolsCount++
15+
"["
16+
" {"
17+
" `"installationPath`": `"build tools path`""
18+
" }"
19+
"]"
20+
} -- -FileName (Resolve-Path $PSScriptRoot\..\vswhere.exe).Path -Arguments "-version [16.0,17.0) -products Microsoft.VisualStudio.Product.BuildTools -latest -format json" -RequireExitCodeZero
21+
22+
# Act.
23+
$null = Get-VisualStudio 16
24+
$actual = Get-VisualStudio 16
25+
26+
# Assert.
27+
Assert-AreEqual -Expected "build tools path" -Actual $actual.installationPath
28+
Assert-AreEqual -Expected 1 -Actual $script:vsCount
29+
Assert-AreEqual -Expected 1 -Actual $script:buildToolsCount
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[CmdletBinding()]
2+
param()
3+
4+
# Arrange.
5+
. $PSScriptRoot\..\..\..\..\Tests\lib\Initialize-Test.ps1
6+
Microsoft.PowerShell.Core\Import-Module $PSScriptRoot\..
7+
Register-Mock Invoke-VstsTool {
8+
Write-Error "Some simulated STDERR content" -ErrorAction Continue 2>&1
9+
"["
10+
" {"
11+
" `"installationPath`": `"some path`""
12+
" }"
13+
"]"
14+
} -- -FileName (Resolve-Path $PSScriptRoot\..\vswhere.exe).Path -Arguments "-version [16.0,17.0) -latest -format json" -RequireExitCodeZero
15+
16+
# Act.
17+
$actual = Get-VisualStudio 16
18+
19+
# Assert.
20+
Assert-AreEqual -Expected "some path" -Actual $actual.installationPath

0 commit comments

Comments
 (0)