This repository has been archived by the owner on Jul 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
MSBuildHelper.ps1
214 lines (178 loc) · 5.44 KB
/
MSBuildHelper.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
Add-Type -TypeDefinition @"
public enum Platform
{
Current,
x86,
x64
}
"@
function Using-Object
{
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[AllowEmptyString()]
[AllowEmptyCollection()]
[AllowNull()]
[Object]
$InputObject,
[Parameter(Mandatory = $true)]
[scriptblock]
$ScriptBlock
)
try
{
. $ScriptBlock
}
finally
{
if ($null -ne $InputObject -and $InputObject -is [System.IDisposable])
{
$InputObject.Dispose()
}
}
}
function Get-MSBuildVersion()
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$False)]
[Version]$Version = $null,
[Parameter(Mandatory=$False)]
[switch]$All = $false
)
$versions = Get-ChildItem HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\ | ForEach-Object { New-Object System.Version ((Split-Path $_.Name -Leaf)) } | Sort-Object -Descending
if ($All)
{
return $versions
}
if ($null -eq $Version)
{
return $versions[0]
}
foreach ($currentVersion in $versions)
{
if ($currentVersion -eq $Version)
{
return $currentVersion
}
}
foreach ($currentVersion in $versions)
{
if ($currentVersion.Major -eq $Version)
{
return $currentVersion
}
}
Write-Error "MSBuild version $Version could not be found"
return $null
}
function Get-MSBuildPathLegacy()
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$False)]
[Version]$Version = $null,
[Parameter(Mandatory=$False)]
[Platform]$Platform = [Platform]::Current
)
$foundVersion = Get-MSBuildVersion $Version -ErrorAction SilentlyContinue
if ($null -eq $foundVersion)
{
Write-Error "MSBuild version $Version could not be found"
return
}
switch ($Platform)
{
Current { $registryView = [Microsoft.Win32.RegistryView]::Default }
x86 { $registryView = [Microsoft.Win32.RegistryView]::Registry32 }
x64 { $registryView = [Microsoft.Win32.RegistryView]::Registry64 }
}
$path = "MSBuildToolsPath"
Using-Object ($key = [Microsoft.Win32.RegistryKey]::OpenBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, $registryView)) {
Using-Object ($subKey = $key.OpenSubKey("SOFTWARE\Microsoft\MSBuild\ToolsVersions\$foundVersion")) {
$resolvedPath = $subKey.GetValue($path)
if ($resolvedPath -eq $null)
{
Write-Error "Could not resolve path for version '$foundVersion' and '$path'"
return $null
}
Write-Verbose "$path : $resolvedPath"
return $resolvedPath
}
}
}
function Get-MSBuildPath()
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$False)]
[String]$VersionString = $null,
[Parameter(Mandatory=$False)]
[Platform]$Platform = [Platform]::Current
)
if ($VersionString) {
if (($VersionString -like "*.*") -eq $False) {
$VersionString += ".0"
}
$version = [Version]$VersionString
}
if ((Get-Command vswhere -ErrorAction SilentlyContinue) -or $version -ge [Version]"15.0") {
if ($version -eq $null) {
$installationPath = vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath
}
else {
$versionConstraint = "[$($version.Major).$($version.Minor), $($version.Major + 1).$($version.Minor))"
$installationPath = vswhere -version $versionConstraint -products * -requires Microsoft.Component.MSBuild -property installationPath
}
if ($installationPath) {
$need64Bit = $False
switch ($Platform)
{
Current { $need64Bit = [System.Environment]::Is64BitProcess }
x64 { $need64Bit = $true }
x86 { $need64Bit = $false }
}
$likePattern = if ($need64Bit) { '*bin\amd64\msbuild.exe' } else { '*bin\msbuild.exe' }
$msbuild = (Get-ChildItem -Path $installationPath -Filter "MSBuild.exe" -Recurse) | Where-Object { $_.FullName -like $likePattern }
if (Test-Path $msbuild.DirectoryName) {
return $msbuild.DirectoryName
}
}
}
# If none of the upper branches returned a version we try the legacy path
return Get-MSBuildPathLegacy -Version $version -Platform $Platform
}
function Get-MSBuild()
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$False,Position=1)]
[String]$VersionString = $null,
[Parameter(Mandatory=$False)]
[Platform]$Platform = [Platform]::Current
)
$msbuildPath = Get-MSBuildPath -VersionString $VersionString -Platform $Platform
if ($null -eq $msbuildPath)
{
Write-Error "MSBuild could not be found"
return
}
$msbuild = Join-Path $msbuildPath "msbuild.exe"
Write-Host "Using msbuild from '$msbuild'"
return $msbuild
}
#Get-MSBuildVersion -All
#Get-MSBuildPath MSBuildToolsPath -Verbose
#Get-MSBuildPath MSBuildToolsRoot -Verbose
#Get-MSBuildPath MSBuildToolsPath -Platform x86 -Verbose
#Get-MSBuildPath MSBuildToolsPath -Platform x64 -Verbose
#Get-MSBuild
#Get-MSBuild 1 -ErrorAction Continue
#Get-MSBuild 2
#Get-MSBuild 3
#Get-MSBuild 3.5
#Get-MSBuild 12
#Get-MSBuild 14
#Get-MSBuild "14.0" -Platform x86 -Verbose
#Get-MSBuild -Version 15.0