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(env): Avoid automatic expansion of %% in env #5395

Merged
merged 11 commits into from
Feb 25, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- **autoupdate:** Fix file hash extraction ([#5295](https://github.com/ScoopInstaller/Scoop/issues/5295))
- **shortcuts:** Output correctly formatted path ([#5333](https://github.com/ScoopInstaller/Scoop/issues/5333))
- **core:** Fix `is_in_dir` under Unix ([#5391](https://github.com/ScoopInstaller/Scoop/issues/5391))
- **env:** Avoid automatic expansion of `%%` in env ([#5394](https://github.com/ScoopInstaller/Scoop/issues/5394))

### Code Refactoring

Expand Down
24 changes: 20 additions & 4 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,26 @@ function Invoke-ExternalCommand {
return $true
}

function env($name,$global,$val='__get') {
$target = 'User'; if($global) {$target = 'Machine'}
if($val -eq '__get') { [environment]::getEnvironmentVariable($name,$target) }
else { [environment]::setEnvironmentVariable($name,$val,$target) }
function env($name, $global, $val = '__get') {
$RegisterKey = if ($global) {
Get-Item -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager'
} else {
Get-Item -Path 'HKCU:'
}

if ($val -eq '__get') {
$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment')
$RegistryValueOption = [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames
$EnvRegisterKey.GetValue($name, $null, $RegistryValueOption)
} else {
$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment', $true)
$RegistryValueKind = if ($EnvRegisterKey.GetValue($name)) {
rashil2000 marked this conversation as resolved.
Show resolved Hide resolved
$EnvRegisterKey.GetValueKind($name)
} else {
[Microsoft.Win32.RegistryValueKind]::ExpandString
}
$EnvRegisterKey.SetValue($name, $val, $RegistryValueKind)
}
}

function isFileLocked([string]$path) {
Expand Down