mirror of
https://github.com/ScoopInstaller/Scoop.git
synced 2025-12-10 18:15:37 +00:00
@@ -20,8 +20,11 @@ function command_path($cmd) {
|
|||||||
# get path from shim
|
# get path from shim
|
||||||
$shim_path = "$scoopdir\shims\scoop-$cmd.ps1"
|
$shim_path = "$scoopdir\shims\scoop-$cmd.ps1"
|
||||||
$line = ((gc $shim_path) | where { $_.startswith('$path') })
|
$line = ((gc $shim_path) | where { $_.startswith('$path') })
|
||||||
iex -command "$line"
|
if($line) {
|
||||||
$cmd_path = $path
|
iex -command "$line"
|
||||||
|
$cmd_path = $path
|
||||||
|
}
|
||||||
|
else { $cmd_path = $shim_path }
|
||||||
}
|
}
|
||||||
|
|
||||||
$cmd_path
|
$cmd_path
|
||||||
|
|||||||
89
libexec/scoop-alias.ps1
Normal file
89
libexec/scoop-alias.ps1
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
# Usage: scoop alias add|list|rm [<args>]
|
||||||
|
# Summary: Manage scoop aliases
|
||||||
|
# Help: Add, remove or list Scoop aliases
|
||||||
|
#
|
||||||
|
# Aliases are custom Scoop subcommands that can be created to make common tasks
|
||||||
|
# easier.
|
||||||
|
#
|
||||||
|
# To add an Alias:
|
||||||
|
# scoop alias add <name> <command> <description>
|
||||||
|
#
|
||||||
|
# e.g.:
|
||||||
|
# scoop alias add rm 'scoop uninstall $args[0]' "Uninstalls an app"
|
||||||
|
|
||||||
|
param($opt, $name, $command, $description)
|
||||||
|
|
||||||
|
. "$psscriptroot\..\lib\core.ps1"
|
||||||
|
. "$psscriptroot\..\lib\help.ps1"
|
||||||
|
. "$psscriptroot\..\lib\config.ps1"
|
||||||
|
. "$psscriptroot\..\lib\install.ps1"
|
||||||
|
|
||||||
|
$script:config_alias = "alias"
|
||||||
|
|
||||||
|
function init_alias_config {
|
||||||
|
$aliases = get_config $script:config_alias
|
||||||
|
if(!$aliases) {
|
||||||
|
$aliases = @{}
|
||||||
|
}
|
||||||
|
|
||||||
|
$aliases
|
||||||
|
}
|
||||||
|
|
||||||
|
function add_alias($name, $command) {
|
||||||
|
if(!$command) {
|
||||||
|
abort "can't create an empty alias"
|
||||||
|
}
|
||||||
|
|
||||||
|
# get current aliases from config
|
||||||
|
$aliases = init_alias_config
|
||||||
|
if($aliases.containskey($name)) {
|
||||||
|
abort "alias $name already exists"
|
||||||
|
}
|
||||||
|
|
||||||
|
$alias_file = "scoop-$name"
|
||||||
|
|
||||||
|
# generate script
|
||||||
|
$shimdir = shimdir $false
|
||||||
|
$script =
|
||||||
|
@"
|
||||||
|
# Summary: $description
|
||||||
|
$command
|
||||||
|
"@
|
||||||
|
$script | out-file "$shimdir\$alias_file.ps1" -encoding oem
|
||||||
|
|
||||||
|
# add alias to config
|
||||||
|
$aliases += @{ $name = $alias_file }
|
||||||
|
set_config $script:config_alias $aliases
|
||||||
|
}
|
||||||
|
|
||||||
|
function rm_alias($name) {
|
||||||
|
$aliases = get_config $script:config_alias
|
||||||
|
|
||||||
|
if($aliases.containskey($name)) {
|
||||||
|
"removing alias $name..."
|
||||||
|
|
||||||
|
rm_shim $aliases.get_item($name) (shimdir $false)
|
||||||
|
|
||||||
|
$aliases.remove($name)
|
||||||
|
set_config $script:config_alias $aliases
|
||||||
|
}
|
||||||
|
else { abort "alias $name doesn't exist" }
|
||||||
|
}
|
||||||
|
|
||||||
|
function list_aliases {
|
||||||
|
$aliases = @{}
|
||||||
|
(get_config $script:config_alias).getenumerator() |% {
|
||||||
|
$summary = summary (gc (command_path $_.name) -raw)
|
||||||
|
if(!($summary)) { $summary = '' }
|
||||||
|
$aliases.add("$($_.name) ", $summary)
|
||||||
|
}
|
||||||
|
|
||||||
|
$aliases.getenumerator() | sort name | ft -hidetablehead -autosize -wrap
|
||||||
|
}
|
||||||
|
|
||||||
|
switch($opt) {
|
||||||
|
"add" { add_alias $name $command }
|
||||||
|
"rm" { rm_alias $name }
|
||||||
|
"list" { list_aliases }
|
||||||
|
default { my_usage; exit 1 }
|
||||||
|
}
|
||||||
59
test/Scoop-Alias.Tests.ps1
Normal file
59
test/Scoop-Alias.Tests.ps1
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
. "$psscriptroot\..\libexec\scoop-alias.ps1"
|
||||||
|
|
||||||
|
reset_aliases
|
||||||
|
|
||||||
|
describe "add_alias" {
|
||||||
|
mock shimdir { "test\fixtures\shim" }
|
||||||
|
mock set_config { }
|
||||||
|
mock get_config { @{} }
|
||||||
|
$shimdir = shimdir
|
||||||
|
|
||||||
|
context "alias doesn't exist" {
|
||||||
|
it "creates a new alias" {
|
||||||
|
$alias_file = "$shimdir\scoop-rm.ps1"
|
||||||
|
$alias_file | should not exist
|
||||||
|
|
||||||
|
add_alias "rm" '"hello, world!"'
|
||||||
|
iex $alias_file | should be "hello, world!"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
context "alias exists" {
|
||||||
|
it "does not change existing alias" {
|
||||||
|
$alias_file = "$shimdir\scoop-rm.ps1"
|
||||||
|
new-item $alias_file -type file
|
||||||
|
$alias_file | should exist
|
||||||
|
|
||||||
|
add_alias "rm" "test"
|
||||||
|
$alias_file | should contain ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
aftereach {
|
||||||
|
rm "test\fixtures\shim\scoop-rm.ps1" -ea ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe "rm_alias" {
|
||||||
|
mock shimdir { "test\fixtures\shim" }
|
||||||
|
$shimdir = shimdir
|
||||||
|
mock set_config { }
|
||||||
|
mock get_config { @{} }
|
||||||
|
|
||||||
|
context "alias exists" {
|
||||||
|
it "removes an existing alias" {
|
||||||
|
$alias_file = "$shimdir\scoop-rm.ps1"
|
||||||
|
add_alias "rm" '"hello, world!"'
|
||||||
|
|
||||||
|
$alias_file | should exist
|
||||||
|
mock get_config { @(@{"rm" = "scoop-rm"}) }
|
||||||
|
|
||||||
|
rm_alias "rm"
|
||||||
|
$alias_file | should not exist
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
afterall {
|
||||||
|
rm "test\fixtures\shim\scoop-rm.ps1" -ea ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user