Skip to content

Commit 6772e61

Browse files
authored
refactor(core): Rewrite and separate path-related functions to system.ps1 (#5836)
1 parent 77b66cc commit 6772e61

12 files changed

Lines changed: 197 additions & 167 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
- **git:** Use Invoke-Git() with direct path to git.exe to prevent spawning shim subprocesses ([#5122](https://github.com/ScoopInstaller/Scoop/issues/5122), [#5375](https://github.com/ScoopInstaller/Scoop/issues/5375))
5959
- **scoop-download:** Output more detailed manifest information ([#5277](https://github.com/ScoopInstaller/Scoop/issues/5277))
6060
- **core:** Cleanup some old codes, e.g., msi section and config migration ([#5715](https://github.com/ScoopInstaller/Scoop/issues/5715), [#5824](https://github.com/ScoopInstaller/Scoop/issues/5824))
61+
- **core:** Rewrite and separate path-related functions to `system.ps1` ([#5836](https://github.com/ScoopInstaller/Scoop/issues/5836))
6162

6263
### Builds
6364

bin/uninstall.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ param(
1212
)
1313

1414
. "$PSScriptRoot\..\lib\core.ps1"
15+
. "$PSScriptRoot\..\lib\system.ps1"
1516
. "$PSScriptRoot\..\lib\install.ps1"
1617
. "$PSScriptRoot\..\lib\shortcuts.ps1"
1718
. "$PSScriptRoot\..\lib\versions.ps1"
@@ -98,7 +99,6 @@ if ($purge) {
9899
if ($global) { keep_onlypersist $globaldir }
99100
}
100101

101-
remove_from_path (shimdir $false)
102-
if ($global) { remove_from_path (shimdir $true) }
102+
Remove-Path -Path (shimdir $global) -Global:$global
103103

104104
success 'Scoop has been uninstalled.'

lib/core.ps1

Lines changed: 11 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -581,12 +581,18 @@ function fullpath($path) {
581581
$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($path)
582582
}
583583
function friendly_path($path) {
584-
$h = (Get-PsProvider 'FileSystem').home; if(!$h.endswith('\')) { $h += '\' }
585-
if($h -eq '\') { return $path }
586-
return "$path" -replace ([regex]::escape($h)), "~\"
584+
$h = (Get-PSProvider 'FileSystem').Home
585+
if (!$h.EndsWith('\')) {
586+
$h += '\'
587+
}
588+
if ($h -eq '\') {
589+
return $path
590+
} else {
591+
return $path -replace ([Regex]::Escape($h)), '~\'
592+
}
587593
}
588594
function is_local($path) {
589-
($path -notmatch '^https?://') -and (test-path $path)
595+
($path -notmatch '^https?://') -and (Test-Path $path)
590596
}
591597

592598
# operations
@@ -715,57 +721,6 @@ function Invoke-ExternalCommand {
715721
return $true
716722
}
717723

718-
function Publish-Env {
719-
if (-not ("Win32.NativeMethods" -as [Type])) {
720-
Add-Type -Namespace Win32 -Name NativeMethods -MemberDefinition @"
721-
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
722-
public static extern IntPtr SendMessageTimeout(
723-
IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam,
724-
uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
725-
"@
726-
}
727-
728-
$HWND_BROADCAST = [IntPtr] 0xffff;
729-
$WM_SETTINGCHANGE = 0x1a;
730-
$result = [UIntPtr]::Zero
731-
732-
[Win32.Nativemethods]::SendMessageTimeout($HWND_BROADCAST,
733-
$WM_SETTINGCHANGE,
734-
[UIntPtr]::Zero,
735-
"Environment",
736-
2,
737-
5000,
738-
[ref] $result
739-
) | Out-Null
740-
}
741-
742-
function env($name, $global, $val = '__get') {
743-
$RegisterKey = if ($global) {
744-
Get-Item -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager'
745-
} else {
746-
Get-Item -Path 'HKCU:'
747-
}
748-
$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment', $val -ne '__get')
749-
750-
if ($val -eq '__get') {
751-
$RegistryValueOption = [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames
752-
$EnvRegisterKey.GetValue($name, $null, $RegistryValueOption)
753-
} elseif ($val -eq $null) {
754-
try { $EnvRegisterKey.DeleteValue($name) } catch { }
755-
Publish-Env
756-
} else {
757-
$RegistryValueKind = if ($val.Contains('%')) {
758-
[Microsoft.Win32.RegistryValueKind]::ExpandString
759-
} elseif ($EnvRegisterKey.GetValue($name)) {
760-
$EnvRegisterKey.GetValueKind($name)
761-
} else {
762-
[Microsoft.Win32.RegistryValueKind]::String
763-
}
764-
$EnvRegisterKey.SetValue($name, $val, $RegistryValueKind)
765-
Publish-Env
766-
}
767-
}
768-
769724
function isFileLocked([string]$path) {
770725
$file = New-Object System.IO.FileInfo $path
771726

@@ -873,7 +828,7 @@ function warn_on_overwrite($shim, $path) {
873828
function shim($path, $global, $name, $arg) {
874829
if (!(Test-Path $path)) { abort "Can't shim '$(fname $path)': couldn't find '$path'." }
875830
$abs_shimdir = ensure (shimdir $global)
876-
ensure_in_path $abs_shimdir $global
831+
Add-Path -Path $abs_shimdir -Global:$global
877832
if (!$name) { $name = strip_ext (fname $path) }
878833

879834
$shim = "$abs_shimdir\$($name.tolower())"
@@ -1010,26 +965,6 @@ function get_shim_path() {
1010965
return $shim_path
1011966
}
1012967

1013-
function search_in_path($target) {
1014-
$path = (env 'PATH' $false) + ";" + (env 'PATH' $true)
1015-
foreach($dir in $path.split(';')) {
1016-
if(test-path "$dir\$target" -pathType leaf) {
1017-
return "$dir\$target"
1018-
}
1019-
}
1020-
}
1021-
1022-
function ensure_in_path($dir, $global) {
1023-
$path = env 'PATH' $global
1024-
$dir = fullpath $dir
1025-
if($path -notmatch [regex]::escape($dir)) {
1026-
write-output "Adding $(friendly_path $dir) to $(if($global){'global'}else{'your'}) path."
1027-
1028-
env 'PATH' $global "$dir;$path" # for future sessions...
1029-
$env:PATH = "$dir;$env:PATH" # for this session
1030-
}
1031-
}
1032-
1033968
function Get-DefaultArchitecture {
1034969
$arch = get_config DEFAULT_ARCHITECTURE
1035970
$system = if (${env:ProgramFiles(Arm)}) {
@@ -1104,45 +1039,6 @@ function Confirm-InstallationStatus {
11041039
return , $Installed
11051040
}
11061041

1107-
function strip_path($orig_path, $dir) {
1108-
if($null -eq $orig_path) { $orig_path = '' }
1109-
$stripped = [string]::join(';', @( $orig_path.split(';') | Where-Object { $_ -and $_ -ne $dir } ))
1110-
return ($stripped -ne $orig_path), $stripped
1111-
}
1112-
1113-
function add_first_in_path($dir, $global) {
1114-
$dir = fullpath $dir
1115-
1116-
# future sessions
1117-
$null, $currpath = strip_path (env 'path' $global) $dir
1118-
env 'path' $global "$dir;$currpath"
1119-
1120-
# this session
1121-
$null, $env:PATH = strip_path $env:PATH $dir
1122-
$env:PATH = "$dir;$env:PATH"
1123-
}
1124-
1125-
function remove_from_path($dir, $global) {
1126-
$dir = fullpath $dir
1127-
1128-
# future sessions
1129-
$was_in_path, $newpath = strip_path (env 'path' $global) $dir
1130-
if($was_in_path) {
1131-
Write-Output "Removing $(friendly_path $dir) from your path."
1132-
env 'path' $global $newpath
1133-
}
1134-
1135-
# current session
1136-
$was_in_path, $newpath = strip_path $env:PATH $dir
1137-
if($was_in_path) { $env:PATH = $newpath }
1138-
}
1139-
1140-
function ensure_robocopy_in_path {
1141-
if(!(Test-CommandAvailable robocopy)) {
1142-
shim "C:\Windows\System32\Robocopy.exe" $false
1143-
}
1144-
}
1145-
11461042
function wraptext($text, $width) {
11471043
if(!$width) { $width = $host.ui.rawui.buffersize.width };
11481044
$width -= 1 # be conservative: doesn't seem to print the last char

lib/install.ps1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ function create_shims($manifest, $dir, $global, $arch) {
783783
} elseif (Test-Path $target -PathType leaf) {
784784
$bin = $target
785785
} else {
786-
$bin = search_in_path $target
786+
$bin = (Get-Command $target).Source
787787
}
788788
if (!$bin) { abort "Can't shim '$target': File doesn't exist." }
789789

@@ -876,16 +876,16 @@ function unlink_current($versiondir) {
876876

877877
# to undo after installers add to path so that scoop manifest can keep track of this instead
878878
function ensure_install_dir_not_in_path($dir, $global) {
879-
$path = (env 'path' $global)
879+
$path = (Get-EnvVar -Name 'PATH' -Global:$global)
880880

881881
$fixed, $removed = find_dir_or_subdir $path "$dir"
882882
if ($removed) {
883883
$removed | ForEach-Object { "Installer added '$(friendly_path $_)' to path. Removing." }
884-
env 'path' $global $fixed
884+
Set-EnvVar -Name 'PATH' -Value $fixed -Global:$global
885885
}
886886

887887
if (!$global) {
888-
$fixed, $removed = find_dir_or_subdir (env 'path' $true) "$dir"
888+
$fixed, $removed = find_dir_or_subdir (Get-EnvVar -Name 'PATH' -Global) "$dir"
889889
if ($removed) {
890890
$removed | ForEach-Object { warn "Installer added '$_' to system path. You might want to remove this manually (requires admin permission)." }
891891
}
@@ -920,7 +920,7 @@ function env_add_path($manifest, $dir, $global, $arch) {
920920
if (!(is_in_dir $dir $path_dir)) {
921921
abort "Error in manifest: env_add_path '$_' is outside the app directory."
922922
}
923-
add_first_in_path $path_dir $global
923+
Add-Path -Path $path_dir -Global:$global -Force
924924
}
925925
}
926926
}
@@ -935,7 +935,7 @@ function env_rm_path($manifest, $dir, $global, $arch) {
935935
} else {
936936
$path_dir = Join-Path $dir $_
937937
}
938-
remove_from_path $path_dir $global
938+
Remove-Path -Path $path_dir -Global:$global
939939
}
940940
}
941941
}
@@ -946,7 +946,7 @@ function env_set($manifest, $dir, $global, $arch) {
946946
$env_set | Get-Member -Member NoteProperty | ForEach-Object {
947947
$name = $_.name
948948
$val = format $env_set.$($_.name) @{ 'dir' = $dir }
949-
env $name $global $val
949+
Set-EnvVar -Name $name -Value $val -Global:$global
950950
Set-Content env:\$name $val
951951
}
952952
}
@@ -956,7 +956,7 @@ function env_rm($manifest, $global, $arch) {
956956
if ($env_set) {
957957
$env_set | Get-Member -Member NoteProperty | ForEach-Object {
958958
$name = $_.name
959-
env $name $global $null
959+
Set-EnvVar -Name $name -Value $null -Global:$global
960960
if (Test-Path env:\$name) { Remove-Item env:\$name }
961961
}
962962
}

lib/psmodules.ps1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,14 @@ function uninstall_psmodule($manifest, $dir, $global) {
4242
}
4343

4444
function ensure_in_psmodulepath($dir, $global) {
45-
$path = env 'psmodulepath' $global
45+
$path = Get-EnvVar -Name 'PSModulePath' -Global:$global
4646
if (!$global -and $null -eq $path) {
4747
$path = "$env:USERPROFILE\Documents\WindowsPowerShell\Modules"
4848
}
4949
$dir = fullpath $dir
5050
if ($path -notmatch [Regex]::Escape($dir)) {
5151
Write-Output "Adding $(friendly_path $dir) to $(if($global){'global'}else{'your'}) PowerShell module path."
5252

53-
env 'psmodulepath' $global "$dir;$path" # for future sessions...
54-
$env:psmodulepath = "$dir;$env:psmodulepath" # for this session
53+
Set-EnvVar -Name 'PSModulePath' -Value "$dir;$path" -Global:$global
5554
}
5655
}

0 commit comments

Comments
 (0)