forked from fluentribbon/Fluent.Ribbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSBuildHelper.ps1
166 lines (140 loc) · 3.7 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
Add-Type -TypeDefinition @"
public enum MSBuildPath
{
MSBuildToolsPath,
MSBuildToolsRoot
}
"@
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 = $null,
[Parameter(Mandatory=$False)]
[switch]$All = $false
)
$versions = dir HKLM:\SOFTWARE\Microsoft\MSBuild\ToolsVersions\ | %{ 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-MSBuildPath()
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[MSBuildPath]$Path,
[Parameter(Mandatory=$False)]
$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 }
}
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-MSBuild()
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$False)]
$Version = $null,
[Parameter(Mandatory=$False)]
[Platform]$Platform = [Platform]::Current
)
$toolsPath = Get-MSBuildPath MSBuildToolsPath -Version $Version -Platform $Platform
if ($null -eq $toolsPath)
{
Write-Error "MSBuild could not be found"
return
}
return Join-Path $toolsPath "msbuild.exe"
}
#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 1 -ErrorAction Continue
#Get-MSBuild 2
#Get-MSBuild 3
#Get-MSBuild 3.5
#Get-MSBuild 12
#Get-MSBuild 14 -Platform x86 -Verbose