Skip to content

Commit 862a0fa

Browse files
committed
Add support for adding VS components
1 parent 41ea5bc commit 862a0fa

File tree

5 files changed

+165
-22
lines changed

5 files changed

+165
-22
lines changed

extension/BuildPhpExtension/BuildPhpExtension.psd1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
'Add-Path',
7878
'Add-PhpDependencies',
7979
'Add-StepLog',
80+
'Add-Vs',
8081
'Get-ArgumentFromConfig',
8182
'Get-BuildDirectory',
8283
'Get-Extension',
@@ -90,6 +91,7 @@
9091
'Get-PhpDevelBuild',
9192
'Get-PhpSdk',
9293
'Get-TempFiles',
94+
'Get-VsVersionHelper',
9395
'Get-VsVersion',
9496
'Invoke-Build',
9597
'Invoke-CleanupTempFiles',

extension/BuildPhpExtension/config/vs.json

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,46 @@
1414
"vc14": {
1515
"major": 14,
1616
"minorMin": 0,
17-
"minorMax": 9
17+
"minorMax": 9,
18+
"components": [
19+
"Microsoft.VisualStudio.Component.CoreBuildTools",
20+
"Microsoft.VisualStudio.Component.VC.140",
21+
"Microsoft.VisualStudio.Component.VC.ATL",
22+
"Microsoft.VisualStudio.Component.Windows10SDK.19041"
23+
]
1824
},
1925
"vc15": {
2026
"major": 14,
2127
"minorMin": 10,
22-
"minorMax": 19
28+
"minorMax": 19,
29+
"components": [
30+
"Microsoft.VisualStudio.Component.CoreBuildTools",
31+
"Microsoft.VisualStudio.Component.VC.v141.x86.x64",
32+
"Microsoft.VisualStudio.Component.VC.ATL",
33+
"Microsoft.VisualStudio.Component.Windows10SDK.19041"
34+
]
2335
},
2436
"vs16": {
2537
"major": 14,
2638
"minorMin": 20,
27-
"minorMax": 29
39+
"minorMax": 29,
40+
"components": [
41+
"Microsoft.VisualStudio.Component.CoreBuildTools",
42+
"Microsoft.VisualStudio.ComponentGroup.VC.Tools.142.x86.x64",
43+
"Microsoft.VisualStudio.Component.VC.ATL",
44+
"Microsoft.VisualStudio.Component.Windows10SDK.19041"
45+
]
2846
},
2947
"vs17": {
3048
"major": 14,
3149
"minorMin": 30,
32-
"minorMax": null
50+
"minorMax": null,
51+
"components": [
52+
"Microsoft.VisualStudio.Component.CoreBuildTools",
53+
"Microsoft.VisualStudio.Component.VC.Tools.x86.x64",
54+
"Microsoft.VisualStudio.Component.VC.ATL",
55+
"Microsoft.VisualStudio.Component.Windows10SDK.19041"
56+
]
3357
}
3458
}
3559
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
function Add-Vs {
2+
<#
3+
.SYNOPSIS
4+
Add the required Visual Studio components.
5+
.PARAMETER VsConfig
6+
Visual Studio Configuration
7+
.PARAMETER VsVersion
8+
Visual Studio Version
9+
#>
10+
[OutputType()]
11+
param (
12+
[Parameter(Mandatory = $true, Position=0, HelpMessage='Visual Studio Version')]
13+
[ValidateNotNull()]
14+
[ValidateLength(1, [int]::MaxValue)]
15+
[string] $VsVersion,
16+
[Parameter(Mandatory = $true, Position=1, HelpMessage='Visual Studio Configuration')]
17+
[PSCustomObject] $VsConfig
18+
)
19+
begin {
20+
$vsWhereUrl = 'https://github.com/microsoft/vswhere/releases/latest/download/vswhere.exe'
21+
}
22+
process {
23+
Add-StepLog "Adding Visual Studio components"
24+
try
25+
{
26+
Set-GAGroup start
27+
$Config = $VsConfig.vs.$VsVersion
28+
29+
$installerDir = Join-Path "${env:ProgramFiles(x86)}\Microsoft Visual Studio" 'Installer'
30+
$vswherePath = Join-Path $installerDir 'vswhere.exe'
31+
if (-not (Test-Path $vswherePath)) {
32+
if (-not (Test-Path $installerDir)) {
33+
New-Item -Path $installerDir -ItemType Directory -Force | Out-Null
34+
}
35+
Invoke-WebRequest -Uri $vsWhereUrl -OutFile $vswherePath -UseBasicParsing
36+
}
37+
38+
$instances = & $vswherePath -products '*' -format json 2> $null | ConvertFrom-Json
39+
$vsInst = $instances | Select-Object -First 1
40+
41+
$componentArgs = $Config.components | ForEach-Object { '--add'; $_ }
42+
43+
if ($vsInst) {
44+
[string]$channel = $vsInst.installationVersion.Split('.')[0]
45+
if ($vsInst.catalog.productId -match '(Enterprise|Professional|Community)$' ) {
46+
$exe = "vs_$($Matches[1].ToLower()).exe"
47+
} else {
48+
$exe = 'vs_buildtools.exe'
49+
}
50+
51+
$installerUrl = "https://aka.ms/vs/$channel/release/$exe"
52+
$installerPath = Join-Path $env:TEMP $exe
53+
54+
Invoke-WebRequest -Uri $installerUrl -OutFile $installerPath -UseBasicParsing
55+
56+
& $installerPath modify `
57+
--installPath $vsInst.installationPath `
58+
--quiet --wait --norestart --nocache `
59+
@componentArgs 2>&1 | ForEach-Object { Write-Host $_ }
60+
} else {
61+
$channel = $VsVersion -replace '/D', ''
62+
$exe = 'vs_buildtools.exe'
63+
$installerUrl = "https://aka.ms/vs/$channel/release/$exe"
64+
$installerPath = Join-Path $env:TEMP $exe
65+
66+
Invoke-WebRequest -Uri $installerUrl -OutFile $installerPath -UseBasicParsing
67+
& $installerPath `
68+
--quiet --wait --norestart --nocache `
69+
@componentArgs 2>&1 | ForEach-Object { Write-Host $_ }
70+
}
71+
Set-GAGroup end
72+
Add-BuildLog tick "Visual Studio" "Visual Studio components installed successfully"
73+
} catch {
74+
Add-BuildLog cross "Visual Studio" "Failed to install Visual Studio components"
75+
throw
76+
}
77+
}
78+
end {
79+
}
80+
}

extension/BuildPhpExtension/private/Get-VsVersion.ps1

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,12 @@ function Get-VsVersion {
2020
$VsConfig = ConvertFrom-Json -InputObject $jsonContent
2121
$majorMinor = $PhpVersion.Substring(0, 3)
2222
$VsVersion = $($VsConfig.php.$majorMinor)
23-
24-
if($null -eq (Get-Command vswhere -ErrorAction SilentlyContinue)) {
25-
throw "vswhere is not available"
26-
}
27-
$MSVCDirectory = vswhere -latest -find "VC\Tools\MSVC"
2823
$selectedToolset = $null
29-
$minor = $null
30-
foreach ($toolset in (Get-ChildItem $MSVCDirectory)) {
31-
$toolsetMajorVersion, $toolsetMinorVersion = $toolset.Name.split(".")[0,1]
32-
$requiredVs = $VsConfig.vs.$VsVersion
33-
if ($requiredVs.major -eq $toolsetMajorVersion -and ($toolsetMinorVersion -ge $requiredVs.minorMin -and ($null -eq $requiredVs.minorMax -or $toolsetMinorVersion -le $requiredVs.minorMax))) {
34-
if($null -eq $minor -or $toolsetMinorVersion -gt $minor) {
35-
$selectedToolset = $toolset.Name.Trim()
36-
$minor = $toolsetMinorVersion
37-
}
38-
}
39-
}
40-
if (-not $selectedToolset) {
41-
throw "toolset not available"
24+
try {
25+
$selectedToolset = Get-VsVersionHelper -VsVersion $VsVersion -VsConfig $VsConfig
26+
} catch {
27+
Add-Vs -VsVersion $VsVersion -VsConfig $VsConfig
28+
$selectedToolset = Get-VsVersionHelper -VsVersion $VsVersion -VsConfig $VsConfig
4229
}
4330
return [PSCustomObject]@{
4431
vs = $VsVersion
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
function Get-VsVersionHelper {
2+
<#
3+
.SYNOPSIS
4+
Helper to get the Visual Studio version and toolset.
5+
.PARAMETER VsConfig
6+
Visual Studio Configuration
7+
.PARAMETER VsVersion
8+
Visual Studio Version
9+
#>
10+
[OutputType()]
11+
param (
12+
[Parameter(Mandatory = $true, Position=0, HelpMessage='Visual Studio Version')]
13+
[ValidateNotNull()]
14+
[ValidateLength(1, [int]::MaxValue)]
15+
[string] $VsVersion,
16+
[Parameter(Mandatory = $true, Position=1, HelpMessage='Visual Studio Configuration')]
17+
[PSCustomObject] $VsConfig
18+
)
19+
begin {
20+
}
21+
process {
22+
if($null -eq (Get-Command vswhere -ErrorAction SilentlyContinue)) {
23+
throw "vswhere is not available"
24+
}
25+
$MSVCDirectory = vswhere -latest -products * -find "VC\Tools\MSVC"
26+
$selectedToolset = $null
27+
$minor = $null
28+
foreach ($toolset in (Get-ChildItem $MSVCDirectory)) {
29+
$toolsetMajorVersion, $toolsetMinorVersion = $toolset.Name.split(".")[0,1]
30+
$requiredVs = $VsConfig.vs.$VsVersion
31+
$majorVersionCheck = [int]$requiredVs.major -eq [int]$toolsetMajorVersion
32+
$minorLowerBoundCheck = [int]$toolsetMinorVersion -ge [int]$requiredVs.minorMin
33+
$minorUpperBoundCheck = ($null -eq $requiredVs.minorMax) -or ([int]$toolsetMinorVersion -le [int]$requiredVs.minorMax)
34+
if ($majorVersionCheck -and $minorLowerBoundCheck -and $minorUpperBoundCheck) {
35+
if($null -eq $minor -or [int]$toolsetMinorVersion -gt [int]$minor) {
36+
$selectedToolset = $toolset.Name.Trim()
37+
$minor = $toolsetMinorVersion
38+
}
39+
}
40+
}
41+
42+
if (-not $selectedToolset) {
43+
throw "toolset not available"
44+
}
45+
46+
return $selectedToolset
47+
}
48+
end {
49+
}
50+
}

0 commit comments

Comments
 (0)