mirror of
https://github.com/ScoopInstaller/Scoop.git
synced 2025-12-09 17:45:39 +00:00
refactor(reset_aliases): Move core function of reset_aliases to scoop (#4794)
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
56
lib/core.ps1
56
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 @() }
|
||||
|
||||
@@ -23,8 +23,6 @@ param($cmd, $name, $repo)
|
||||
. "$PSScriptRoot\..\lib\buckets.ps1"
|
||||
. "$PSScriptRoot\..\lib\help.ps1"
|
||||
|
||||
reset_aliases
|
||||
|
||||
$usage_add = "usage: scoop bucket add <name> [<repo>]"
|
||||
$usage_rm = "usage: scoop bucket rm <name>"
|
||||
|
||||
|
||||
@@ -7,8 +7,6 @@ param($app)
|
||||
. "$PSScriptRoot\..\lib\install.ps1"
|
||||
. "$PSScriptRoot\..\lib\help.ps1"
|
||||
|
||||
reset_aliases
|
||||
|
||||
if (!$app) { error '<app> missing'; my_usage; exit 1 }
|
||||
|
||||
$app, $bucket, $null = parse_app $app
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -130,8 +130,6 @@ param($name, $value)
|
||||
. "$PSScriptRoot\..\lib\core.ps1"
|
||||
. "$PSScriptRoot\..\lib\help.ps1"
|
||||
|
||||
reset_aliases
|
||||
|
||||
if (!$name) {
|
||||
$scoopConfig
|
||||
} elseif ($name -like '--help') {
|
||||
|
||||
@@ -9,8 +9,6 @@
|
||||
. "$PSScriptRoot\..\lib\decompress.ps1"
|
||||
. "$PSScriptRoot\..\lib\help.ps1"
|
||||
|
||||
reset_aliases
|
||||
|
||||
$opt, $apps, $err = getopt $args 'a:' 'arch='
|
||||
$app = $apps[0]
|
||||
|
||||
|
||||
@@ -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 }
|
||||
|
||||
|
||||
@@ -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 } }
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
. "$PSScriptRoot\..\lib\manifest.ps1"
|
||||
. "$PSScriptRoot\..\lib\versions.ps1"
|
||||
|
||||
reset_aliases
|
||||
$apps = $args
|
||||
|
||||
if(!$apps) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 }
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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 }
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
. "$PSScriptRoot\..\lib\manifest.ps1"
|
||||
. "$PSScriptRoot\..\lib\versions.ps1"
|
||||
|
||||
reset_aliases
|
||||
$apps = $args
|
||||
|
||||
if(!$apps) {
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
. "$PSScriptRoot\..\lib\versions.ps1"
|
||||
. "$PSScriptRoot\..\lib\getopt.ps1"
|
||||
|
||||
reset_aliases
|
||||
|
||||
# options
|
||||
$opt, $apps, $err = getopt $args 'gp' 'global', 'purge'
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -5,8 +5,6 @@ param($command)
|
||||
. "$PSScriptRoot\..\lib\core.ps1"
|
||||
. "$PSScriptRoot\..\lib\help.ps1"
|
||||
|
||||
reset_aliases
|
||||
|
||||
if (!$command) {
|
||||
'ERROR: <command> missing'
|
||||
my_usage
|
||||
|
||||
@@ -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' {
|
||||
|
||||
@@ -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 }
|
||||
|
||||
Reference in New Issue
Block a user