From 418ab78e79123438229c1f530e25c63d07d35646 Mon Sep 17 00:00:00 2001 From: CEbbinghaus Date: Thu, 14 Mar 2024 12:00:00 +1100 Subject: [PATCH 1/2] Changed ENV variable name to allow for custom paths # Conflicts: # lib/core.ps1 # Conflicts: # lib/install.ps1 --- lib/core.ps1 | 21 ++++++++++++++------- lib/install.ps1 | 6 +++--- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/core.ps1 b/lib/core.ps1 index 24b6c8b0c7..671b61bb7e 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -739,6 +739,13 @@ public static extern IntPtr SendMessageTimeout( } function env($name, $global, $val = '__get') { + if(-not $name) { + $name = [environment]::getEnvironmentVariable("SCOOP_ENV", "User") + if(-not $name) { + $name = 'PATH' + } + } + $RegisterKey = if ($global) { Get-Item -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' } else { @@ -1010,7 +1017,7 @@ function get_shim_path() { } function search_in_path($target) { - $path = (env 'PATH' $false) + ";" + (env 'PATH' $true) + $path = (env $Null $false) + ";" + (env $Null $true) foreach($dir in $path.split(';')) { if(test-path "$dir\$target" -pathType leaf) { return "$dir\$target" @@ -1019,12 +1026,12 @@ function search_in_path($target) { } function ensure_in_path($dir, $global) { - $path = env 'PATH' $global + $path = env $Null $global $dir = fullpath $dir if($path -notmatch [regex]::escape($dir)) { write-output "Adding $(friendly_path $dir) to $(if($global){'global'}else{'your'}) path." - env 'PATH' $global "$dir;$path" # for future sessions... + env $Null $global "$dir;$path" # for future sessions... $env:PATH = "$dir;$env:PATH" # for this session } } @@ -1113,8 +1120,8 @@ function add_first_in_path($dir, $global) { $dir = fullpath $dir # future sessions - $null, $currpath = strip_path (env 'path' $global) $dir - env 'path' $global "$dir;$currpath" + $null, $currpath = strip_path (env $Null $global) $dir + env $Null $global "$dir;$currpath" # this session $null, $env:PATH = strip_path $env:PATH $dir @@ -1125,10 +1132,10 @@ function remove_from_path($dir, $global) { $dir = fullpath $dir # future sessions - $was_in_path, $newpath = strip_path (env 'path' $global) $dir + $was_in_path, $newpath = strip_path (env $Null $global) $dir if($was_in_path) { Write-Output "Removing $(friendly_path $dir) from your path." - env 'path' $global $newpath + env $Null $global $newpath } # current session diff --git a/lib/install.ps1 b/lib/install.ps1 index 3fe945229a..ef149143f6 100644 --- a/lib/install.ps1 +++ b/lib/install.ps1 @@ -876,16 +876,16 @@ function unlink_current($versiondir) { # to undo after installers add to path so that scoop manifest can keep track of this instead function ensure_install_dir_not_in_path($dir, $global) { - $path = (env 'path' $global) + $path = (env $Null $global) $fixed, $removed = find_dir_or_subdir $path "$dir" if ($removed) { $removed | ForEach-Object { "Installer added '$(friendly_path $_)' to path. Removing." } - env 'path' $global $fixed + env $Null $global $fixed } if (!$global) { - $fixed, $removed = find_dir_or_subdir (env 'path' $true) "$dir" + $fixed, $removed = find_dir_or_subdir (env $Null $true) "$dir" if ($removed) { $removed | ForEach-Object { warn "Installer added '$_' to system path. You might want to remove this manually (requires admin permission)." } } From d0f8ee75cda790dd022371b0c60e383166f75c33 Mon Sep 17 00:00:00 2001 From: CEbbinghaus Date: Thu, 14 Mar 2024 12:00:48 +1100 Subject: [PATCH 2/2] Changed from environment variable to config # Conflicts: # lib/core.ps1 --- lib/core.ps1 | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/core.ps1 b/lib/core.ps1 index 671b61bb7e..1b5fce17b6 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -740,10 +740,11 @@ public static extern IntPtr SendMessageTimeout( function env($name, $global, $val = '__get') { if(-not $name) { - $name = [environment]::getEnvironmentVariable("SCOOP_ENV", "User") - if(-not $name) { - $name = 'PATH' - } + $name = $scoop_path_env + } + + if(-not $name) { + throw "Unable to evaluate path environment variable. Please set or remove PATH_ENV in your config.json." } $RegisterKey = if ($global) { @@ -755,7 +756,7 @@ function env($name, $global, $val = '__get') { if ($val -eq '__get') { $RegistryValueOption = [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames - $EnvRegisterKey.GetValue($name, $null, $RegistryValueOption) + $EnvRegisterKey.GetValue($name, $null, $RegistryValueOption) + "" } elseif ($val -eq $null) { try { $EnvRegisterKey.DeleteValue($name) } catch { } Publish-Env @@ -1422,6 +1423,8 @@ if ($pathExpected) { } $scoopConfig = load_cfg $configFile +$scoop_path_env = get_config PATH_ENV 'PATH' + # Scoop root directory $scoopdir = $env:SCOOP, (get_config ROOT_PATH), (Resolve-Path "$PSScriptRoot\..\..\..\.."), "$([System.Environment]::GetFolderPath('UserProfile'))\scoop" | Where-Object { -not [String]::IsNullOrEmpty($_) } | Select-Object -First 1