-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(scoop-alias): Pass options correctly
- Loading branch information
1 parent
716b6db
commit 2aa899c
Showing
4 changed files
with
147 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
function get_aliases_from_config { | ||
get_config ALIAS ([PSCustomObject]@{}) | ||
} | ||
|
||
function add_alias { | ||
param( | ||
[ValidateNotNullOrEmpty()] | ||
[string]$name, | ||
[ValidateNotNullOrEmpty()] | ||
[string]$command, | ||
[string]$description | ||
) | ||
|
||
$aliases = get_aliases_from_config | ||
if ($aliases.$name) { | ||
abort "Alias '$name' already exists." | ||
} | ||
|
||
$alias_script_name = "scoop-$name" | ||
$shimdir = shimdir $false | ||
if (Test-Path "$shimdir\$alias_script_name.ps1") { | ||
abort "File '$alias_script_name.ps1' already exists in shims directory." | ||
} | ||
$script = @( | ||
"# Summary:$(if ($description) { " $description" })", | ||
"$command" | ||
) | ||
try { | ||
$script | Out-UTF8File "$shimdir\$alias_script_name.ps1" | ||
} catch { | ||
abort $_.Exception | ||
} | ||
|
||
# Add the new alias to the config. | ||
$aliases | Add-Member -MemberType NoteProperty -Name $name -Value $alias_script_name | ||
set_config ALIAS $aliases | Out-Null | ||
} | ||
|
||
function rm_alias { | ||
param( | ||
[ValidateNotNullOrEmpty()] | ||
[string]$name | ||
) | ||
|
||
$aliases = get_aliases_from_config | ||
if (!$aliases.$name) { | ||
abort "Alias '$name' doesn't exist." | ||
} | ||
|
||
info "Removing alias '$name'..." | ||
rm_shim $aliases.$name (shimdir $false) | ||
$aliases.PSObject.Properties.Remove($name) | ||
set_config ALIAS $aliases | Out-Null | ||
return | ||
} | ||
|
||
function list_aliases { | ||
param([bool]$verbose) | ||
|
||
$aliases = get_aliases_from_config | ||
$alias_info = $aliases.PSObject.Properties.Name | ForEach-Object { | ||
$content = Get-Content (command_path $_) | ||
@{ | ||
Name = $_ | ||
Summary = (summary $content).Trim() | ||
Command = ($content | Select-Object -Skip 1).Trim() | ||
} | ||
} | ||
if (!$alias_info) { | ||
info "No alias found." | ||
return | ||
} | ||
$alias_info = $alias_info | Sort-Object Name | ||
$properties = @('Name', 'Command') | ||
if ($verbose) { | ||
$properties += 'Summary' | ||
} | ||
$alias_info | Select-Object $properties | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,70 @@ | ||
# Usage: scoop alias add|list|rm [<args>] | ||
# Usage: scoop alias <subcommand> [options] [<args>] | ||
# Summary: Manage scoop aliases | ||
# Help: Add, remove or list Scoop aliases | ||
# Help: Available subcommands: add, rm, list. | ||
# | ||
# Aliases are custom Scoop subcommands that can be created to make common tasks | ||
# easier. | ||
# Aliases are custom Scoop subcommands that can be created to make common tasks easier. | ||
# | ||
# To add an Alias: | ||
# scoop alias add <name> <command> <description> | ||
# To add an alias: | ||
# | ||
# e.g.: | ||
# scoop alias add rm 'scoop uninstall $args[0]' 'Uninstalls an app' | ||
# scoop alias add upgrade 'scoop update *' 'Updates all apps, just like brew or apt' | ||
# scoop alias add <name> <command> [<description>] | ||
# | ||
# e.g., | ||
# | ||
# scoop alias add rm 'scoop uninstall $args[0]' 'Uninstall an app' | ||
# scoop alias add upgrade 'scoop update *' 'Update all apps, just like "brew" or "apt"' | ||
# | ||
# To remove an alias: | ||
# | ||
# scoop alias rm <name> | ||
# | ||
# To list all aliases: | ||
# | ||
# scoop alias list [-v|--verbose] | ||
# | ||
# Options: | ||
# -v, --verbose Show alias description and table headers (works only for 'list') | ||
# -v, --verbose Show alias description and table headers (works only for "list") | ||
|
||
param( | ||
[String]$opt, | ||
[String]$name, | ||
[String]$command, | ||
[String]$description, | ||
[Switch]$verbose = $false | ||
) | ||
param($SubCommand) | ||
|
||
. "$PSScriptRoot\..\lib\install.ps1" # shim related | ||
. "$PSScriptRoot\..\lib\getopt.ps1" | ||
. "$PSScriptRoot\..\lib\alias.ps1" | ||
. "$PSScriptRoot\..\lib\install.ps1" | ||
|
||
$script:config_alias = 'alias' | ||
|
||
function init_alias_config { | ||
$aliases = get_config $script:config_alias | ||
if ($aliases) { | ||
$aliases | ||
$SubCommands = @('add', 'rm', 'list') | ||
if ($SubCommand -notin $SubCommands) { | ||
if (!$SubCommand) { | ||
error '<subcommand> missing' | ||
} else { | ||
New-Object -TypeName PSObject | ||
error "'$SubCommand' is not one of available subcommands: $($SubCommands -join ', ')" | ||
} | ||
my_usage | ||
exit 1 | ||
} | ||
|
||
function add_alias($name, $command) { | ||
if (!$command) { | ||
abort "Can't create an empty alias." | ||
} | ||
$opt, $other, $err = getopt $args 'v' , 'verbose' | ||
if ($err) { "scoop alias: $err"; exit 1 } | ||
|
||
# get current aliases from config | ||
$aliases = init_alias_config | ||
if ($aliases.$name) { | ||
abort "Alias '$name' already exists." | ||
} | ||
|
||
$alias_file = "scoop-$name" | ||
$name, $command, $description = $other | ||
$verbose = $opt.v -or $opt.verbose | ||
|
||
# generate script | ||
$shimdir = shimdir $false | ||
if (Test-Path "$shimdir\$alias_file.ps1") { | ||
abort "File '$alias_file.ps1' already exists in shims directory." | ||
switch ($SubCommand) { | ||
'add' { | ||
if (!$name -or !$command) { | ||
error "<name> and <command> must be specified for subcommand 'add'" | ||
exit 1 | ||
} | ||
add_alias $name $command $description | ||
} | ||
$script = | ||
@( | ||
"# Summary: $description", | ||
"$command" | ||
) -join "`r`n" | ||
$script | Out-UTF8File "$shimdir\$alias_file.ps1" | ||
|
||
# add alias to config | ||
$aliases | Add-Member -MemberType NoteProperty -Name $name -Value $alias_file | ||
|
||
set_config $script:config_alias $aliases | Out-Null | ||
} | ||
|
||
function rm_alias($name) { | ||
$aliases = init_alias_config | ||
if (!$name) { | ||
abort 'Alias to be removed has not been specified!' | ||
'rm' { | ||
if (!$name) { | ||
error "<name> must be specified for subcommand 'rm'" | ||
exit 1 | ||
} | ||
rm_alias $name | ||
} | ||
|
||
if ($aliases.$name) { | ||
info "Removing alias '$name'..." | ||
|
||
rm_shim $aliases.$name (shimdir $false) | ||
|
||
$aliases.PSObject.Properties.Remove($name) | ||
set_config $script:config_alias $aliases | Out-Null | ||
} else { | ||
abort "Alias '$name' doesn't exist." | ||
} | ||
} | ||
|
||
function list_aliases { | ||
$aliases = @() | ||
|
||
(init_alias_config).PSObject.Properties.GetEnumerator() | ForEach-Object { | ||
$content = Get-Content (command_path $_.Name) | ||
$command = ($content | Select-Object -Skip 1).Trim() | ||
$summary = (summary $content).Trim() | ||
|
||
$aliases += New-Object psobject -Property @{Name = $_.name; Summary = $summary; Command = $command } | ||
} | ||
|
||
if (!$aliases.count) { | ||
info "No alias found." | ||
'list' { | ||
list_aliases $verbose | ||
} | ||
$aliases = $aliases.GetEnumerator() | Sort-Object Name | ||
if ($verbose) { | ||
return $aliases | Select-Object Name, Command, Summary | ||
} else { | ||
return $aliases | Select-Object Name, Command | ||
} | ||
} | ||
|
||
switch ($opt) { | ||
'add' { add_alias $name $command } | ||
'rm' { rm_alias $name } | ||
'list' { list_aliases } | ||
default { my_usage; exit 1 } | ||
} | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters