Skip to content

Instantly share code, notes, and snippets.

@nwalker
Last active March 29, 2016 13:13
Show Gist options
  • Select an option

  • Save nwalker/eca5dbe21714200daaa4 to your computer and use it in GitHub Desktop.

Select an option

Save nwalker/eca5dbe21714200daaa4 to your computer and use it in GitHub Desktop.
##
## kerly - simple and stupid wrapper for kerl in fish
##
if not set -q KERLY_HOME
set -g KERLY_HOME $HOME/.kerly
end
mkdir -p $KERLY_HOME
function kerly --description "kerly: fish plugin to manage kerl installations"
# copy all but the first argument to $scargs
set -l sc $argv[1]
set -l funcname "__kf_$sc"
set -l scargs
if test (count $argv) -eq 0
# If called without arguments, print usage
echo "Usage: kerly <command> [<args>]"
echo
echo "Available commands:"
echo
for sc in (functions -a | sed -n '/__kf_/{s///g;p;}')
set -l helptext (functions "__vf_$sc" | head -n 1 | sed -E "s|.*'(.*)'.*|\1|")
printf " %-15s %s\n" $sc (set_color 555)$helptext(set_color normal)
end
return
end
if test (count $argv) -gt 1
set scargs $argv[2..-1]
end
if functions -q $funcname
eval $funcname $scargs
else
echo "The subcommand $sc is not defined"
end
end
function __kf_activate --description "activate kerl installation"
if [ (count $argv) -lt 1 ]
echo "specify an installation"
return 1
end
if not [ -d $KERLY_HOME/$argv[1] ]
echo "No such installation!"
return 2
end
if set -q KERL_ENV
kerly deactivate
end
set -g KERL_ENV $KERLY_HOME/$argv[1]
set -g _KF_REBAR_PLT_DIR $REBAR_PLT_DIR
set -gx PATH $KERL_ENV/bin $PATH
set -gx MANPATH $KERL_ENV/man $MANPATH
set -gx REBAR_PLT_DIR $KERL_ENV
return 0
end
function __kf_deactivate --description "deactivate current kerl installation"
if not set -q KERL_ENV
echo "no active installation"
return 0
end
_kf_strip PATH $KERL_ENV/bin
_kf_strip MANPATH $KERL_ENV/man
set -gx REBAR_PLT_DIR $_KF_REBAR_PLT_DIR
set -e _KF_REBAR_PLT_DIR
set -e KERL_ENV
set -e KERLY_CURRENT
end
function __kf_use -a kerly_inst --description "switch to other erlang installation"
if [ (count $argv) -lt 1 ]
echo "specify an installation"
return 1
end
__kf_deactivate > /dev/null
source $KERLY_HOME/$argv[1].env.fish
end
function __kf_install -a kerl_build kerly_name --description "make new installation from build"
set -l iname $kerl_build
if [ -n "$kerly_name" ]
set iname $kerly_name
end
set -l ipath $KERLY_HOME/$iname
if [ -d $KERLY_HOME/$iname ]
echo "installation already exists"
return 1
end
kerl install $argv[1] $KERLY_HOME/$iname | grep -v 'activate' | grep -v 'leave'
printf "\
setenv KERLY_CURRENT $iname
setenv KERL_ENV $ipath
setenv PATH $ipath/bin \$PATH
setenv MANPATH $ipath/man \$MANPATH
setenv REBAR_PLT_DIR $ipath
" > $KERLY_HOME/$iname.env.fish
end
function __kf_default --description "set default installation"
if not [ (count $argv) -eq 1 ]
echo "you need to specify exactly one installation."
return 1
end
if not [ -f $KERLY_HOME/$argv[1].env.fish ]
echo "no such installation"
return 1
end
ln -sf $KERLY_HOME/$argv[1].env.fish $KERLY_HOME/default.env.fish
end
function __kf_ls --description "list all of the available installations"
pushd $KERLY_HOME
for i in (ls *.env.fish)
echo $i
end | sed "s|\.env\.fish||"
popd
end
function __kf_rm --description "Delete an installation"
if not [ (count $argv) -eq 1 ]
echo "you need to specify exactly one installation."
return 1
end
if begin; set -q KERL_ENV; and [ $argv[1] = (basename $KERL_ENV) ]; end
__kf_deactivate > /dev/null
end
echo "Removing $KERLY_HOME/$argv[1]"
rm -rf $KERLY_HOME/$argv[1]
rm -rf $KERLY_HOME/$argv[1].env.fish
end
function _kf_strip
set var_name $argv[1]
eval "set var {\$$var_name}"
if [ (count $argv) -lt 2 ]
return 0
end
set prefix $argv[2]
set to_rem
for i in (seq (count $var))
echo "$prefix $var[$i]"
if contains $prefix $var[$i]
set to_rem $to_rem $i
end
end
for i in $to_rem
set -e var[$i]
end
eval "set -gx $var_name $var"
end
################
# Autocomplete
# Based on https://github.com/zmalltalker/fish-nuggets/blob/master/completions/git.fish
begin
function __kfcompletion_needs_command
set cmd (commandline -opc)
if test (count $cmd) -eq 1 -a $cmd[1] = 'kerly'
return 0
end
return 1
end
function __kfcompletion_using_command
set cmd (commandline -opc)
if test (count $cmd) -gt 1
if test $argv[1] = $cmd[2]
return 0
end
end
return 1
end
# add completion for subcommands
for sc in (functions -a | sed -n '/__kf_/{s///g;p;}')
set -l helptext (functions "__kf_$sc" | head -n 1 | sed -E "s|.*'(.*)'.*|\1|")
complete -x -c kerly -n '__kfcompletion_needs_command' -a $sc -d $helptext
end
complete -x -c kerly -n '__kfcompletion_using_command activate' -a "(kerly ls)"
complete -x -c kerly -n '__kfcompletion_using_command rm' -a "(kerly ls)"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment