Skip to content

Commit 5a795ca

Browse files
authored
refactor(reset_aliases): Move core function of reset_aliases to scoop (#4794)
1 parent 5025661 commit 5a795ca

26 files changed

Lines changed: 16 additions & 100 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
### Code Refactoring
1111

1212
- **relpath:** Use `$PSScriptRoot` instead of `relpath` ([#4793](https://github.com/ScoopInstaller/Scoop/issues/4793))
13+
- **reset_aliases:** Move core function of `reset_aliases` to `scoop` ([#4794](https://github.com/ScoopInstaller/Scoop/issues/4794))
1314

1415
## [v0.1.0](https://github.com/ScoopInstaller/Scoop/compare/2021-12-26...v0.1.0) - 2022-03-01
1516

bin/scoop.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ Set-StrictMode -off
66
. "$PSScriptRoot\..\lib\core.ps1"
77
. "$PSScriptRoot\..\lib\buckets.ps1"
88
. "$PSScriptRoot\..\lib\commands.ps1"
9-
10-
reset_aliases
9+
# for aliases where there's a local function, re-alias so the function takes precedence
10+
$aliases = Get-Alias | Where-Object { $_.Options -notmatch 'ReadOnly|AllScope' } | ForEach-Object { $_.Name }
11+
Get-ChildItem Function: | Where-Object -Property Name -In -Value $aliases | ForEach-Object {
12+
Set-Alias -Name $_.Name -Value Local:$($_.Name) -Scope Script
13+
}
1114

1215
$commands = commands
1316
if ('--version' -contains $cmd -or (!$cmd -and '-v' -contains $args)) {

lib/core.ps1

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,12 @@ function url_remote_filename($url) {
404404
return $basename
405405
}
406406

407-
function ensure($dir) { if(!(test-path $dir)) { mkdir $dir > $null }; resolve-path $dir }
407+
function ensure($dir) {
408+
if (!(Test-Path -Path $dir)) {
409+
New-Item -Path $dir -ItemType Directory | Out-Null
410+
}
411+
Convert-Path -Path $dir
412+
}
408413
function fullpath($path) {
409414
# should be ~ rooted
410415
$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($path)
@@ -888,55 +893,6 @@ function pluralize($count, $singular, $plural) {
888893
if($count -eq 1) { $singular } else { $plural }
889894
}
890895

891-
function reset_alias($name, $value) {
892-
if($existing = get-alias $name -ea ignore | Where-Object { $_.options -match 'readonly' }) {
893-
if($existing.definition -ne $value) {
894-
write-host "Alias $name is read-only; can't reset it." -f darkyellow
895-
}
896-
return # already set
897-
}
898-
if($value -is [scriptblock]) {
899-
if(!(test-path -path "function:script:$name")) {
900-
new-item -path function: -name "script:$name" -value $value | out-null
901-
}
902-
return
903-
}
904-
905-
set-alias $name $value -scope script -option allscope
906-
}
907-
908-
function reset_aliases() {
909-
# for aliases where there's a local function, re-alias so the function takes precedence
910-
$aliases = get-alias | Where-Object { $_.options -notmatch 'readonly|allscope' } | ForEach-Object { $_.name }
911-
get-childitem function: | ForEach-Object {
912-
$fn = $_.name
913-
if($aliases -contains $fn) {
914-
set-alias $fn local:$fn -scope script
915-
}
916-
}
917-
918-
# for dealing with user aliases
919-
$default_aliases = @{
920-
'cp' = 'copy-item'
921-
'echo' = 'write-output'
922-
'gc' = 'get-content'
923-
'gci' = 'get-childitem'
924-
'gcm' = 'get-command'
925-
'gm' = 'get-member'
926-
'iex' = 'invoke-expression'
927-
'ls' = 'get-childitem'
928-
'mkdir' = { new-item -type directory @args }
929-
'mv' = 'move-item'
930-
'rm' = 'remove-item'
931-
'sc' = 'set-content'
932-
'select' = 'select-object'
933-
'sls' = 'select-string'
934-
}
935-
936-
# set default aliases
937-
$default_aliases.keys | ForEach-Object { reset_alias $_ $default_aliases[$_] }
938-
}
939-
940896
# convert list of apps to list of ($app, $global) tuples
941897
function applist($apps, $global) {
942898
if(!$apps) { return @() }

libexec/scoop-bucket.ps1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ param($cmd, $name, $repo)
2323
. "$PSScriptRoot\..\lib\buckets.ps1"
2424
. "$PSScriptRoot\..\lib\help.ps1"
2525

26-
reset_aliases
27-
2826
$usage_add = "usage: scoop bucket add <name> [<repo>]"
2927
$usage_rm = "usage: scoop bucket rm <name>"
3028

libexec/scoop-cat.ps1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ param($app)
77
. "$PSScriptRoot\..\lib\install.ps1"
88
. "$PSScriptRoot\..\lib\help.ps1"
99

10-
reset_aliases
11-
1210
if (!$app) { error '<app> missing'; my_usage; exit 1 }
1311

1412
$app, $bucket, $null = parse_app $app

libexec/scoop-cleanup.ps1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
. "$PSScriptRoot\..\lib\help.ps1"
1818
. "$PSScriptRoot\..\lib\install.ps1"
1919

20-
reset_aliases
21-
2220
$opt, $apps, $err = getopt $args 'gk' 'global', 'cache'
2321
if ($err) { "scoop cleanup: $err"; exit 1 }
2422
$global = $opt.g -or $opt.global

libexec/scoop-config.ps1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ param($name, $value)
130130
. "$PSScriptRoot\..\lib\core.ps1"
131131
. "$PSScriptRoot\..\lib\help.ps1"
132132

133-
reset_aliases
134-
135133
if (!$name) {
136134
$scoopConfig
137135
} elseif ($name -like '--help') {

libexec/scoop-depends.ps1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
. "$PSScriptRoot\..\lib\decompress.ps1"
1010
. "$PSScriptRoot\..\lib\help.ps1"
1111

12-
reset_aliases
13-
1412
$opt, $apps, $err = getopt $args 'a:' 'arch='
1513
$app = $apps[0]
1614

libexec/scoop-download.ps1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
. "$PSScriptRoot\..\lib\help.ps1"
2121
. "$PSScriptRoot\..\lib\getopt.ps1"
2222

23-
reset_aliases
24-
2523
$opt, $apps, $err = getopt $args 'fhua:' 'force', 'no-hash-check', 'no-update-scoop', 'arch='
2624
if ($err) { error "scoop download: $err"; exit 1 }
2725

libexec/scoop-export.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
. "$PSScriptRoot\..\lib\manifest.ps1"
88
. "$PSScriptRoot\..\lib\buckets.ps1"
99

10-
reset_aliases
1110
$def_arch = default_architecture
1211

1312
$local = installed_apps $false | ForEach-Object { @{ name = $_; global = $false } }

0 commit comments

Comments
 (0)