This repository has been archived by the owner on Jan 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathfunctions.ps1
69 lines (59 loc) · 2.19 KB
/
functions.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
# copy pasted from https://gist.github.com/cloudhan/97db3c1e57895a09a80ec1f30c471cb3
function Set-EnvFromCmdSet {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]
[string]$CmdSetResult
)
process {
if ($CmdSetResult -Match "=") {
$i = $CmdSetResult.IndexOf("=")
$k = $CmdSetResult.Substring(0, $i)
$v = $CmdSetResult.Substring($i + 1)
Set-Item -Force -Path "Env:\$k" -Value "$v"
}
}
}
function Set-VSEnv {
param (
[parameter(Mandatory = $false)]
[ValidateSet(2022, 2019, 2017)]
[int]$Version = 2022,
[parameter(Mandatory = $false)]
[ValidateSet("all", "x86", "x64")]
[String]$Arch = "x64"
)
$vs_where = 'C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe'
$version_range = switch ($Version) {
2022 { '[17,18)' }
2019 { '[16,17)' }
2017 { '[15,16)' }
}
$info = &$vs_where -version $version_range -format json | ConvertFrom-Json
$vs_env = @{
install_path = if ($null -ne $info) { $info[0].installationPath } else { $null }
all = 'Common7\Tools\VsDevCmd.bat'
x64 = 'VC\Auxiliary\Build\vcvars64.bat'
x86 = 'VC\Auxiliary\Build\vcvars32.bat'
}
if ( $null -eq $vs_env.install_path) {
Write-Host -ForegroundColor Red "Visual Studio $Version is not installed."
return
}
$path = Join-Path $vs_env.install_path $vs_env.$Arch
C:/Windows/System32/cmd.exe /c "`"$path`" & set" | Set-EnvFromCmdSet
Set-Item -Force -Path "Env:\BAZEL_VC" -Value "$env:VCINSTALLDIR"
Write-Host -ForegroundColor Green "Visual Studio $Version $Arch Command Prompt variables set."
}
class EnvironmentStack {
static [System.Collections.Stack] $stack = [System.Collections.Stack]::new()
}
function Push-Environment {
[EnvironmentStack]::stack.push([Environment]::GetEnvironmentVariables())
}
function Pop-Environment {
[EnvironmentStack]::stack.pop().GetEnumerator() |
ForEach-Object {
Set-Item -Force -Path ("Env:\" + $_.Key) -Value $_.Value
}
}