diff --git a/CHANGELOG.md b/CHANGELOG.md index 52494d680..686511de8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ### Code Refactoring - **relpath:** Use `$PSScriptRoot` instead of `relpath` ([#4793](https://github.com/ScoopInstaller/Scoop/issues/4793)) +- **reset_aliases:** Move core function of `reset_aliases` to `scoop` ([#4794](https://github.com/ScoopInstaller/Scoop/issues/4794)) ## [v0.1.0](https://github.com/ScoopInstaller/Scoop/compare/2021-12-26...v0.1.0) - 2022-03-01 diff --git a/bin/scoop.ps1 b/bin/scoop.ps1 index 5fe0a8f31..ba206faba 100644 --- a/bin/scoop.ps1 +++ b/bin/scoop.ps1 @@ -6,8 +6,11 @@ Set-StrictMode -off . "$PSScriptRoot\..\lib\core.ps1" . "$PSScriptRoot\..\lib\buckets.ps1" . "$PSScriptRoot\..\lib\commands.ps1" - -reset_aliases +# for aliases where there's a local function, re-alias so the function takes precedence +$aliases = Get-Alias | Where-Object { $_.Options -notmatch 'ReadOnly|AllScope' } | ForEach-Object { $_.Name } +Get-ChildItem Function: | Where-Object -Property Name -In -Value $aliases | ForEach-Object { + Set-Alias -Name $_.Name -Value Local:$($_.Name) -Scope Script +} $commands = commands if ('--version' -contains $cmd -or (!$cmd -and '-v' -contains $args)) { diff --git a/lib/core.ps1 b/lib/core.ps1 index 659b5b930..afb2295ef 100644 --- a/lib/core.ps1 +++ b/lib/core.ps1 @@ -404,7 +404,12 @@ function url_remote_filename($url) { return $basename } -function ensure($dir) { if(!(test-path $dir)) { mkdir $dir > $null }; resolve-path $dir } +function ensure($dir) { + if (!(Test-Path -Path $dir)) { + New-Item -Path $dir -ItemType Directory | Out-Null + } + Convert-Path -Path $dir +} function fullpath($path) { # should be ~ rooted $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($path) @@ -888,55 +893,6 @@ function pluralize($count, $singular, $plural) { if($count -eq 1) { $singular } else { $plural } } -function reset_alias($name, $value) { - if($existing = get-alias $name -ea ignore | Where-Object { $_.options -match 'readonly' }) { - if($existing.definition -ne $value) { - write-host "Alias $name is read-only; can't reset it." -f darkyellow - } - return # already set - } - if($value -is [scriptblock]) { - if(!(test-path -path "function:script:$name")) { - new-item -path function: -name "script:$name" -value $value | out-null - } - return - } - - set-alias $name $value -scope script -option allscope -} - -function reset_aliases() { - # for aliases where there's a local function, re-alias so the function takes precedence - $aliases = get-alias | Where-Object { $_.options -notmatch 'readonly|allscope' } | ForEach-Object { $_.name } - get-childitem function: | ForEach-Object { - $fn = $_.name - if($aliases -contains $fn) { - set-alias $fn local:$fn -scope script - } - } - - # for dealing with user aliases - $default_aliases = @{ - 'cp' = 'copy-item' - 'echo' = 'write-output' - 'gc' = 'get-content' - 'gci' = 'get-childitem' - 'gcm' = 'get-command' - 'gm' = 'get-member' - 'iex' = 'invoke-expression' - 'ls' = 'get-childitem' - 'mkdir' = { new-item -type directory @args } - 'mv' = 'move-item' - 'rm' = 'remove-item' - 'sc' = 'set-content' - 'select' = 'select-object' - 'sls' = 'select-string' - } - - # set default aliases - $default_aliases.keys | ForEach-Object { reset_alias $_ $default_aliases[$_] } -} - # convert list of apps to list of ($app, $global) tuples function applist($apps, $global) { if(!$apps) { return @() } diff --git a/libexec/scoop-bucket.ps1 b/libexec/scoop-bucket.ps1 index effb55a3a..cd47c111b 100644 --- a/libexec/scoop-bucket.ps1 +++ b/libexec/scoop-bucket.ps1 @@ -23,8 +23,6 @@ param($cmd, $name, $repo) . "$PSScriptRoot\..\lib\buckets.ps1" . "$PSScriptRoot\..\lib\help.ps1" -reset_aliases - $usage_add = "usage: scoop bucket add []" $usage_rm = "usage: scoop bucket rm " diff --git a/libexec/scoop-cat.ps1 b/libexec/scoop-cat.ps1 index 43bf7e95d..18bf2dc33 100644 --- a/libexec/scoop-cat.ps1 +++ b/libexec/scoop-cat.ps1 @@ -7,8 +7,6 @@ param($app) . "$PSScriptRoot\..\lib\install.ps1" . "$PSScriptRoot\..\lib\help.ps1" -reset_aliases - if (!$app) { error ' missing'; my_usage; exit 1 } $app, $bucket, $null = parse_app $app diff --git a/libexec/scoop-cleanup.ps1 b/libexec/scoop-cleanup.ps1 index 3c8c10859..2171e1fb8 100644 --- a/libexec/scoop-cleanup.ps1 +++ b/libexec/scoop-cleanup.ps1 @@ -17,8 +17,6 @@ . "$PSScriptRoot\..\lib\help.ps1" . "$PSScriptRoot\..\lib\install.ps1" -reset_aliases - $opt, $apps, $err = getopt $args 'gk' 'global', 'cache' if ($err) { "scoop cleanup: $err"; exit 1 } $global = $opt.g -or $opt.global diff --git a/libexec/scoop-config.ps1 b/libexec/scoop-config.ps1 index 556bf5748..32e4572dd 100644 --- a/libexec/scoop-config.ps1 +++ b/libexec/scoop-config.ps1 @@ -130,8 +130,6 @@ param($name, $value) . "$PSScriptRoot\..\lib\core.ps1" . "$PSScriptRoot\..\lib\help.ps1" -reset_aliases - if (!$name) { $scoopConfig } elseif ($name -like '--help') { diff --git a/libexec/scoop-depends.ps1 b/libexec/scoop-depends.ps1 index 4883dc78d..b545811c7 100644 --- a/libexec/scoop-depends.ps1 +++ b/libexec/scoop-depends.ps1 @@ -9,8 +9,6 @@ . "$PSScriptRoot\..\lib\decompress.ps1" . "$PSScriptRoot\..\lib\help.ps1" -reset_aliases - $opt, $apps, $err = getopt $args 'a:' 'arch=' $app = $apps[0] diff --git a/libexec/scoop-download.ps1 b/libexec/scoop-download.ps1 index e13a2520e..c5f385de7 100644 --- a/libexec/scoop-download.ps1 +++ b/libexec/scoop-download.ps1 @@ -20,8 +20,6 @@ . "$PSScriptRoot\..\lib\help.ps1" . "$PSScriptRoot\..\lib\getopt.ps1" -reset_aliases - $opt, $apps, $err = getopt $args 'fhua:' 'force', 'no-hash-check', 'no-update-scoop', 'arch=' if ($err) { error "scoop download: $err"; exit 1 } diff --git a/libexec/scoop-export.ps1 b/libexec/scoop-export.ps1 index 1c03635f9..d16631a2b 100644 --- a/libexec/scoop-export.ps1 +++ b/libexec/scoop-export.ps1 @@ -7,7 +7,6 @@ . "$PSScriptRoot\..\lib\manifest.ps1" . "$PSScriptRoot\..\lib\buckets.ps1" -reset_aliases $def_arch = default_architecture $local = installed_apps $false | ForEach-Object { @{ name = $_; global = $false } } diff --git a/libexec/scoop-help.ps1 b/libexec/scoop-help.ps1 index 45b0cb9f7..c11b13e10 100644 --- a/libexec/scoop-help.ps1 +++ b/libexec/scoop-help.ps1 @@ -6,8 +6,6 @@ param($cmd) . "$PSScriptRoot\..\lib\commands.ps1" . "$PSScriptRoot\..\lib\help.ps1" -reset_aliases - function print_help($cmd) { $file = Get-Content (command_path $cmd) -raw @@ -47,4 +45,3 @@ Some useful commands are:" } exit 0 - diff --git a/libexec/scoop-hold.ps1 b/libexec/scoop-hold.ps1 index ed171f4de..22d01c172 100644 --- a/libexec/scoop-hold.ps1 +++ b/libexec/scoop-hold.ps1 @@ -5,7 +5,6 @@ . "$PSScriptRoot\..\lib\manifest.ps1" . "$PSScriptRoot\..\lib\versions.ps1" -reset_aliases $apps = $args if(!$apps) { diff --git a/libexec/scoop-home.ps1 b/libexec/scoop-home.ps1 index aae57f69f..cbcf92e4c 100644 --- a/libexec/scoop-home.ps1 +++ b/libexec/scoop-home.ps1 @@ -7,8 +7,6 @@ param($app) . "$PSScriptRoot\..\lib\manifest.ps1" . "$PSScriptRoot\..\lib\buckets.ps1" -reset_aliases - if($app) { $manifest, $bucket = find_manifest $app if($manifest) { diff --git a/libexec/scoop-info.ps1 b/libexec/scoop-info.ps1 index 630606af8..d7898deae 100644 --- a/libexec/scoop-info.ps1 +++ b/libexec/scoop-info.ps1 @@ -9,8 +9,6 @@ . "$PSScriptRoot\..\lib\versions.ps1" . "$PSScriptRoot\..\lib\getopt.ps1" -reset_aliases - $opt, $app, $err = getopt $args 'v' 'verbose' if ($err) { error "scoop info: $err"; exit 1 } $verbose = $opt.v -or $opt.verbose diff --git a/libexec/scoop-install.ps1 b/libexec/scoop-install.ps1 index d28938b6e..85ce00f6e 100644 --- a/libexec/scoop-install.ps1 +++ b/libexec/scoop-install.ps1 @@ -29,8 +29,6 @@ . "$PSScriptRoot\..\lib\getopt.ps1" . "$PSScriptRoot\..\lib\depends.ps1" -reset_aliases - $opt, $apps, $err = getopt $args 'gikusa:' 'global', 'independent', 'no-cache', 'no-update-scoop', 'skip', 'arch=' if ($err) { "scoop install: $err"; exit 1 } diff --git a/libexec/scoop-list.ps1 b/libexec/scoop-list.ps1 index 1e265b049..a9490dedf 100644 --- a/libexec/scoop-list.ps1 +++ b/libexec/scoop-list.ps1 @@ -8,7 +8,6 @@ param($query) . "$PSScriptRoot\..\lib\manifest.ps1" . "$PSScriptRoot\..\lib\buckets.ps1" -reset_aliases $def_arch = default_architecture if (-not (Get-FormatData ScoopApps)) { Update-FormatData "$PSScriptRoot\..\supporting\formats\ScoopTypes.Format.ps1xml" diff --git a/libexec/scoop-reset.ps1 b/libexec/scoop-reset.ps1 index ceb98d8bf..7c2b826e4 100644 --- a/libexec/scoop-reset.ps1 +++ b/libexec/scoop-reset.ps1 @@ -12,7 +12,6 @@ . "$PSScriptRoot\..\lib\versions.ps1" . "$PSScriptRoot\..\lib\shortcuts.ps1" -reset_aliases $opt, $apps, $err = getopt $args if($err) { "scoop reset: $err"; exit 1 } diff --git a/libexec/scoop-search.ps1 b/libexec/scoop-search.ps1 index 4ddc29c68..a4a74f0e7 100644 --- a/libexec/scoop-search.ps1 +++ b/libexec/scoop-search.ps1 @@ -10,8 +10,6 @@ param($query) . "$PSScriptRoot\..\lib\manifest.ps1" . "$PSScriptRoot\..\lib\versions.ps1" -reset_aliases - function bin_match($manifest, $query) { if(!$manifest.bin) { return $false } foreach($bin in $manifest.bin) { diff --git a/libexec/scoop-status.ps1 b/libexec/scoop-status.ps1 index 2aa60d458..4a84334cf 100644 --- a/libexec/scoop-status.ps1 +++ b/libexec/scoop-status.ps1 @@ -7,8 +7,6 @@ . "$PSScriptRoot\..\lib\versions.ps1" . "$PSScriptRoot\..\lib\depends.ps1" -reset_aliases - # check if scoop needs updating $currentdir = fullpath $(versiondir 'scoop' 'current') $needs_update = $false diff --git a/libexec/scoop-unhold.ps1 b/libexec/scoop-unhold.ps1 index 9a16d7528..72deb2bd0 100644 --- a/libexec/scoop-unhold.ps1 +++ b/libexec/scoop-unhold.ps1 @@ -5,7 +5,6 @@ . "$PSScriptRoot\..\lib\manifest.ps1" . "$PSScriptRoot\..\lib\versions.ps1" -reset_aliases $apps = $args if(!$apps) { diff --git a/libexec/scoop-uninstall.ps1 b/libexec/scoop-uninstall.ps1 index 94351b776..3accb7c91 100644 --- a/libexec/scoop-uninstall.ps1 +++ b/libexec/scoop-uninstall.ps1 @@ -15,8 +15,6 @@ . "$PSScriptRoot\..\lib\versions.ps1" . "$PSScriptRoot\..\lib\getopt.ps1" -reset_aliases - # options $opt, $apps, $err = getopt $args 'gp' 'global', 'purge' diff --git a/libexec/scoop-update.ps1 b/libexec/scoop-update.ps1 index 9e3aebdba..57251968e 100644 --- a/libexec/scoop-update.ps1 +++ b/libexec/scoop-update.ps1 @@ -25,8 +25,6 @@ . "$PSScriptRoot\..\lib\depends.ps1" . "$PSScriptRoot\..\lib\install.ps1" -reset_aliases - $opt, $apps, $err = getopt $args 'gfiksqa' 'global', 'force', 'independent', 'no-cache', 'skip', 'quiet', 'all' if ($err) { "scoop update: $err"; exit 1 } $global = $opt.g -or $opt.global diff --git a/libexec/scoop-virustotal.ps1 b/libexec/scoop-virustotal.ps1 index 87c4593e0..33b248325 100644 --- a/libexec/scoop-virustotal.ps1 +++ b/libexec/scoop-virustotal.ps1 @@ -44,8 +44,6 @@ . "$PSScriptRoot\..\lib\install.ps1" . "$PSScriptRoot\..\lib\depends.ps1" -reset_aliases - $opt, $apps, $err = getopt $args 'a:snu' @('arch=', 'scan', 'no-depends', 'no-update-scoop') if($err) { "scoop virustotal: $err"; exit 1 } if(!$apps) { my_usage; exit 1 } diff --git a/libexec/scoop-which.ps1 b/libexec/scoop-which.ps1 index 5966d35ed..9c7de3882 100644 --- a/libexec/scoop-which.ps1 +++ b/libexec/scoop-which.ps1 @@ -5,8 +5,6 @@ param($command) . "$PSScriptRoot\..\lib\core.ps1" . "$PSScriptRoot\..\lib\help.ps1" -reset_aliases - if (!$command) { 'ERROR: missing' my_usage diff --git a/test/Scoop-Alias.Tests.ps1 b/test/Scoop-Alias.Tests.ps1 index be913af67..fa71d260b 100644 --- a/test/Scoop-Alias.Tests.ps1 +++ b/test/Scoop-Alias.Tests.ps1 @@ -1,14 +1,12 @@ . "$PSScriptRoot\..\libexec\scoop-alias.ps1" | Out-Null -reset_aliases - Describe 'add_alias' -Tag 'Scoop' { Mock shimdir { "$env:TEMP\shims" } Mock set_config { } Mock get_config { @{} } $shimdir = shimdir - New-Item -ItemType Directory -Path $shimdir -ErrorAction Ignore + ensure $shimdir Context "alias doesn't exist" { It 'creates a new alias' { @@ -38,7 +36,7 @@ Describe 'rm_alias' -Tag 'Scoop' { Mock get_config { @{} } $shimdir = shimdir - New-Item -ItemType Directory -Path $shimdir -ErrorAction Ignore + ensure $shimdir Context 'alias exists' { It 'removes an existing alias' { diff --git a/test/Scoop-Core.Tests.ps1 b/test/Scoop-Core.Tests.ps1 index e4fd6a6f0..e450a8173 100644 --- a/test/Scoop-Core.Tests.ps1 +++ b/test/Scoop-Core.Tests.ps1 @@ -234,12 +234,12 @@ Describe 'get_app_name_from_shim' -Tag 'Scoop' { } It 'returns app name if file exists and is a shim to an app' -Skip:$isUnix { - mkdir -p "$working_dir/mockapp/current/" + ensure "$working_dir/mockapp/current/" Write-Output '' | Out-File "$working_dir/mockapp/current/mockapp1.ps1" shim "$working_dir/mockapp/current/mockapp1.ps1" $false 'shim-test1' $shim_path1 = (Get-Command 'shim-test1.ps1').Path get_app_name_from_shim "$shim_path1" | Should -Be 'mockapp' - mkdir -p "$working_dir/mockapp/1.0.0/" + ensure "$working_dir/mockapp/1.0.0/" Write-Output '' | Out-File "$working_dir/mockapp/1.0.0/mockapp2.ps1" shim "$working_dir/mockapp/1.0.0/mockapp2.ps1" $false 'shim-test2' $shim_path2 = (Get-Command 'shim-test2.ps1').Path @@ -267,10 +267,6 @@ Describe 'ensure_robocopy_in_path' -Tag 'Scoop' { $shimdir = shimdir $false Mock versiondir { $repo_dir } - BeforeAll { - reset_aliases - } - Context 'robocopy is not in path' { It 'shims robocopy when not on path' -Skip:$isUnix { Mock Test-CommandAvailable { $false }