-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPS-VsVars.psm1
188 lines (165 loc) · 6.28 KB
/
PS-VsVars.psm1
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
$VisualStudios = @()
Export-ModuleMember -Variable VisualStudios
$EditionNodes = @(
"VisualStudio",
"VCExpress",
"VPDExpress",
"VSWinExpress",
"VWDExpress",
"WDExpress"
)
# I assume there will be a 15.0 :), who knows for sure? :P
$CtpVersions = @(
New-Object System.Version "15.0"
)
$SearchRoot = "Software\Microsoft"
if($env:PROCESSOR_ARCHITECTURE -eq "AMD64") {
$SearchRoot = "Software\Wow6432Node\Microsoft"
}
$EditionNodes | ForEach-Object {
$edition = $_;
$root = "HKLM:\$SearchRoot\$edition"
if(Test-Path $root) {
dir "$root" | Where-Object {
($_.Name -match "\d+\.\d+") -and
(![String]::IsNullOrEmpty((Get-ItemProperty "$root\$($_.PSChildName)").InstallDir))
} | ForEach-Object {
$regPath = "$root\$($_.PSChildName)"
# Gather VS data
$installDir = (Get-ItemProperty $regPath).InstallDir
$vsVars = $null;
if(Test-Path "$installDir\..\..\VC\vcvarsall.bat") {
$vsVars = Convert-Path "$installDir\..\..\VC\vcvarsall.bat"
}
$devenv = (Get-ItemProperty "$regPath\Setup\VS").EnvironmentPath;
# Make a VSInfo object
$ver = New-Object System.Version $_.PSChildName;
$vsInfo = [PSCustomObject]@{
"Edition" = $edition;
"Version" = $ver;
"RegistryRoot" = $_;
"InstallDir" = $installDir;
"VsVarsPath" = $vsVars;
"DevEnv" = $devenv;
"Prerelease" = ($CtpVersions -contains $ver)
}
# Add it to the dictionary
$VisualStudios += $vsInfo
}
}
}
$DefaultVisualStudio = $VisualStudios | Where { $_.Edition -eq "VisualStudio" } | sort Version -desc | select -first 1
Export-ModuleMember -Variable DefaultVisualStudio
function Import-VsVars {
param(
[Parameter(Mandatory=$false)][string]$Version = $null,
[Parameter(Mandatory=$false)][string]$Edition = "VisualStudio",
[Parameter(Mandatory=$false)][string]$VsVarsPath = $null,
[Parameter(Mandatory=$false)][string]$Architecture = $env:PROCESSOR_ARCHITECTURE,
[Parameter(Mandatory=$false)][switch]$PrereleaseAllowed
)
if([String]::IsNullOrEmpty($VsVarsPath)) {
Write-Debug "Finding vcvarsall.bat automatically..."
# Find all versions of the specified edition
$vers = $VisualStudios | Where { $_.Edition -eq $Edition }
$Vs = Get-VisualStudio -Version $Version -Edition $Edition -PrereleaseAllowed:$PrereleaseAllowed;
if(!$Vs) {
throw "No $Edition Environments found"
} else {
Write-Debug "Found VS $($Vs.Version) in $($Vs.InstallDir)"
$VsVarsPath = $Vs.VsVarsPath
}
}
if($VsVarsPath -and (Test-Path $VsVarsPath)) {
# Run the cmd script
Write-Debug "Invoking: `"$VsVarsPath`" $Architecture"
Invoke-CmdScript "$VsVarsPath" $Architecture
"Imported Visual Studio $VsVersion Environment into current shell"
} else {
throw "Could not find VsVars batch file at: $VsVarsPath!"
}
}
Export-ModuleMember -Function Import-VsVars
function Get-VisualStudio {
param(
[Parameter(Mandatory=$false, Position=1)][string]$Version,
[Parameter(Mandatory=$false, Position=1)][string]$Edition = "VisualStudio",
[Parameter(Mandatory=$false)][switch]$PrereleaseAllowed)
# Find all versions of the specified edition
$vers = $VisualStudios | Where { $_.Edition -eq $Edition }
$Vs = $null;
if($Version) {
$Vs = $vers | where { $_.Version -eq [System.Version]$Version } | select -first 1
} else {
$Vs = $vers | where { $PrereleaseAllowed -or !($_.Prerelease) } | sort Version -desc | select -first 1
}
if(!$Vs) {
if($Version) {
throw "Could not find $Edition $Version!"
} else {
throw "Could not find any $Edition version!"
}
}
$Vs
}
Export-ModuleMember -Function Get-VisualStudio
function Invoke-VisualStudio {
param(
[Parameter(Mandatory=$false, Position=0)][string]$Solution,
[Parameter(Mandatory=$false, Position=1)][string]$Version,
[Parameter(Mandatory=$false, Position=2)][string]$Edition,
[Parameter(Mandatory=$false)][switch]$Elevated,
[Parameter(Mandatory=$false)][switch]$PrereleaseAllowed,
[Parameter(Mandatory=$false)][switch]$WhatIf)
# Load defaults from ".vslaunch" file
if(Test-Path ".vslaunch") {
$config = ConvertFrom-Json (cat -Raw ".vslaunch")
if($config) {
$Edition = if($Edition) { $Edition } else { $config.edition }
$Version = if($Version) { $Version } else { $config.version }
$Solution = if($Solution) { $Solution } else { $config.solution }
$Elevated = if($Elevated) { $Elevated } else { $config.elevated }
}
}
if(!$Edition) {
$Edition = "VisualStudio";
}
Write-Debug "Launching: Edition=$Edition, Version=$Version, Solution=$Solution, Elevated=$Elevated"
if([String]::IsNullOrEmpty($Solution)) {
$Solution = "*.sln"
}
elseif(!$Solution.EndsWith(".sln")) {
$Solution = "*" + $Solution + "*.sln";
}
$devenvargs = @();
if(!(Test-Path $Solution)) {
Write-Host "Could not find any solutions. Launching VS without opening a solution."
}
else {
$slns = @(dir $Solution)
if($slns.Length -gt 1) {
$names = [String]::Join(",", @($slns | foreach { $_.Name }))
throw "Ambiguous matches for $($Solution): $names";
}
$devenvargs = @($slns[0])
}
$Vs = Get-VisualStudio -Version $Version -Edition $Edition -PrereleaseAllowed:$PrereleaseAllowed
$devenv = $Vs.DevEnv
if($devenv) {
if($WhatIf) {
Write-Host "Launching '$devenv $devenvargs'"
}
else {
if($Elevated) {
elevate $devenv @devenvargs
}
else {
&$devenv @devenvargs
}
}
} else {
throw "Could not find desired Visual Studio Edition/Version: $Edition v$Version"
}
}
Set-Alias -Name vs -Value Invoke-VisualStudio
Export-ModuleMember -Function Invoke-VisualStudio -Alias vs