Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(install-env): Avoid automatic expansion of %% in env #44

Merged
merged 4 commits into from
Feb 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,43 @@ function Get-Env {
[Switch] $global
)

$target = if ($global) { 'Machine' } else { 'User' }
return [Environment]::GetEnvironmentVariable($name, $target)
$RegisterKey = if ($global) {
Get-Item -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager'
} else {
Get-Item -Path 'HKCU:'
}

$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment')
$RegistryValueOption = [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames
$EnvRegisterKey.GetValue($name, $null, $RegistryValueOption)
}

function Write-Env {
param(
[String] $name,
[String] $val,
[Switch] $global
)

$RegisterKey = if ($global) {
Get-Item -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager'
} else {
Get-Item -Path 'HKCU:'
}

$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment', $true)
if ($val -eq $null) {
$EnvRegisterKey.DeleteValue($name)
} else {
$RegistryValueKind = if ($val.Contains('%')) {
[Microsoft.Win32.RegistryValueKind]::ExpandString
} elseif ($EnvRegisterKey.GetValue($name)) {
$EnvRegisterKey.GetValueKind($name)
} else {
[Microsoft.Win32.RegistryValueKind]::String
}
$EnvRegisterKey.SetValue($name, $val, $RegistryValueKind)
}
}

function Add-ShimsDirToPath {
Expand All @@ -387,7 +422,7 @@ function Add-ShimsDirToPath {
}

# For future sessions
[System.Environment]::SetEnvironmentVariable('PATH', "$SCOOP_SHIMS_DIR;$userEnvPath", 'User')
Write-Env 'PATH' "$SCOOP_SHIMS_DIR;$userEnvPath"
# For current session
$env:PATH = "$SCOOP_SHIMS_DIR;$env:PATH"
rashil2000 marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down