Rename both files and refactor sw script

This commit is contained in:
mitchel 2020-10-28 16:43:17 -04:00
parent e45d11220c
commit 337d90a8c6
4 changed files with 166 additions and 137 deletions

75
run
View File

@ -1,75 +0,0 @@
#!/usr/bin/env fish
set -g run_func_prefix 'run'
if test -f ./run.fish
source ./run.fish
else
echo 'No run.fish found in current directory. Please copy the template from https://github.com/mitchell/run.fish'
exit 1
end
function main
for command in $argv
and execute_command $run_func_prefix $command
end
and if test -z "$argv"
execute_command $run_func_prefix
end
end
function execute_command -a prefix command
set -l args (string split --max 1 $run_arg_delimeter $command)
set -l func $prefix\_$args[1]
define_default_functions $prefix
if test -z "$command"
execute_command $prefix default
else if functions -q $func
$func $args[2]
else
echo "$prefix command '$command' does not exist"
exit 1
end
end
function define_default_functions
set -g prefix $argv[1]
function _$prefix\_commands -d 'List all available commands'
set -l names (functions --names | grep $prefix\_)
for name in $names
set -l details (functions -D -v $name)
set -l description $details[5]
set -l short_name (string replace $prefix\_ '' $name)
if test (string length $short_name) -ge 8
echo $short_name\t$description
else
echo $short_name\t\t$description
end
end
end
function $prefix\_help -a command -d 'Print help menu or command definition'
if test -n "$command"
functions $prefix\_$command
else
echo 'Here are the available commands:'\n
_$prefix\_commands
echo \n"To see a command's definition do `run help$run_arg_delimeter{command}`"
echo "To see a subcommand group's help menu do `run {command}$run_arg_delimeter""help`"
end
end
if not functions -q $prefix\_default
function $prefix\_default -a arg -d 'Alias for help command'
$prefix\_help $arg
end
end
end
main $argv

View File

@ -1,62 +0,0 @@
# This run.fish script serves as a central place to store frequently run commands for this project.
# Replace command section with your own command functions.
# Source: https://github.com/mitchell/run.fish
### Config ###
# Top-level configurations, like the argument delimeter and custom global variables.
set -g run_arg_delimeter ':'
### Commands ###
# Add, edit, and remove commands freely below.
# To add a command simply create a function with this naming scheme: {run_func_prefix}_{name}.
function run_default -a arg -d 'Display this help menu'
# This command will run in when you simply execute `run` in this repo.
# The default command is set to help by default.
# So you can omit this from your run.fish if you like that behavior.
run_help $arg
end
function run_install -a bin_path -d 'Install the run script to a bin folder'
if test -z "$bin_path"
if test -d ~/.local/bin
set bin_path ~/.local/bin
else if test -d /usr/local/bin
set bin_path /usr/local/bin
end
end
echo "Installing run script to $bin_path."
cp ./run $bin_path/
end
function run_lint -d 'Lint fish scripts'
fish --no-execute ./run ./run.fish
end
function run_format -d 'Format fish scripts'
fish_indent --write ./run ./run.fish
end
function run_hello -a command -d 'This is an example of a subgroup of commands'
function hello_world -d 'The classic'
echo 'Hello, world!'
end
function hello_name -a name -d 'Say hello to someone specific'
echo "Hello, $name!"
end
function hello_fr -d 'Say bonjour'
echo 'Bonjour, le monde!'
end
alias hello_default='hello_world'
execute_command 'hello' $command # this command comes from the run script
end
function run_fails -d 'This command fails every time'
false
end

94
sw Executable file
View File

@ -0,0 +1,94 @@
#!/usr/bin/env fish
set -g cmd_func_prefix 'swim'
if test -f ./swim.fish
source ./swim.fish
else
echo 'No swim.fish found in current directory. Please copy the template from https://github.com/mitchell/swim.fish'
exit 1
end
function main
run_command $cmd_func_prefix $argv
end
function run_command -a prefix
argparse --ignore-unknown 'h/help' -- $argv
set -l command $argv[2]
set -l func $prefix\_$command
set -l args $argv[3..-1]
define_included_functions $prefix
if functions -q $func
if test -n "$_flag_h"
if test ($func is_group) = 'is-group' >/dev/null 2>&1
$func $args --help
else
$prefix\_help $command
end
else
$func $args
end
else if test -n "$_flag_h"
$prefix\_help
else if functions -q $prefix\_default
$prefix\_default $args
else
echo "$prefix command '$command' does not exist"
return 127
end
end
function define_included_functions -a prefix
set -l prefix $prefix
function $prefix\_do -V prefix -d 'Perform a list of commands separated by commas'
for command in (string split ',' "$argv")
test -z "$command"; and continue
set -l command (string trim "$command")
echo \n"Running '$command' ..."
run_command $prefix (string split ' ' $command)
end
end
function _$prefix\_commands -V prefix -d 'List all available commands'
set -l names (functions --names | grep $prefix\_)
for name in $names
set -l details (functions -D -v $name)
set -l description $details[5]
set -l short_name (string replace $prefix\_ '' $name)
if test (string length $short_name) -ge 8
echo $short_name\t$description
else
echo $short_name\t\t$description
end
end
end
function $prefix\_help -a command -V prefix -d 'Print help menu or command definition'
if test -n "$command"
functions $prefix\_$command
else
echo 'Here are the available commands:'\n
_$prefix\_commands
echo \n"To see a command's definition do `run help {command}`"
echo "To see a subcommand group's help menu do `run {command} help`"
end
end
if not functions -q $prefix\_default
function $prefix\_default -a arg -V prefix -d 'Alias for help command'
$prefix\_help $arg
end
end
function $prefix\_is_group -d 'Tells executor whether this is a subcommand group'
echo 'is-group'
end
end
main $argv

72
swim.fish Normal file
View File

@ -0,0 +1,72 @@
# This swim.fish script serves as a central place to store frequently run commands for this project.
# Replace Commands section with your own command functions.
# Source: https://github.com/mitchell/swim.fish
# Configuration
# These are the currently configurable settings of the sw script.
# Both contain their defaults and can be omitted from your swim.fish.
set -g cmd_func_prefix 'swim' # Set the function prefix for defining tasks
set -g task_arg_delimeter '.' # Set the argument delimeter for tasks
# Commands
function swim_default -a arg -d 'Display this help menu'
# This command will run in when you simply execute `sw` in this repo.
# The default command is set to help by default.
# So you can omit this from your swim.fish if you like that behavior.
swim_help $arg
end
function swim_install -d 'Install the sw script to a bin folder'
argparse --max-args 0 'p/bin_path=' -- $argv
set -l sudo_needed true
set -l bin_path /usr/local/bin
if test -n "$_flag_p"
set sudo_needed false
set bin_path $_flag_p
else if test -d ~/.local/bin
set sudo_needed false
set bin_path ~/.local/bin
end
echo "Installing sw script to $bin_path."
set -l cp_cmd cp ./sw $bin_path/
if $sudo_needed
sudo $cp_cmd
else
$cp_cmd
end
end
function swim_lint -d 'Lint fish scripts'
fish --no-execute ./sw ./swim.fish
end
function swim_format -d 'Format fish scripts'
fish_indent --write ./sw ./swim.fish
end
function swim_hello -d 'This is an example of a subgroup of commands'
function hello_world -d 'The classic'
echo 'Hello, world!'
end
function hello_name -a name -d 'Say hello to someone specific'
echo "Hello, $name!"
end
function hello_fr -d 'Say bonjour'
echo 'Bonjour, le monde!'
end
alias hello_default='hello_world'
run_command 'hello' $argv # this command comes from the sw script
end
function swim_fails -d 'This command fails every time'
false
end