Skip to content

Instantly share code, notes, and snippets.

@BanzaiMan
Created August 29, 2009 06:06
Show Gist options
  • Select an option

  • Save BanzaiMan/177409 to your computer and use it in GitHub Desktop.

Select an option

Save BanzaiMan/177409 to your computer and use it in GitHub Desktop.
Poor man's ruby version manager
# poor man's ruby version manager
# assume you have ZenTest installed
# usage:
# rvm help
# rvm list
# rvm reset
# rvm switch VERSION
# rvm show
PRE_RVM_PATH=$PATH
MULTIRUBY_INSTALL_DIR=$HOME/.multiruby/install
RVM_COMMANDS="help list reset show switch"
function rvm {
# set -x
if [ ! -d $MULTIRUBY_INSTALL_DIR ]; then
echo "rvm: $MULTIRUBY_INSTALL_DIR does not exist"
echo " consider installing ZenTest with 'gem install ZenTest'"
return 1
fi
what=$1
to=$2
case $what in
list)
echo "installed:"
for i in $(ls -d $MULTIRUBY_INSTALL_DIR/*); do
echo " ${i##*/}"
done
;;
reset)
rvm switch default
;;
switch)
if [ -z $to ]; then
echo "sepecify which ruby to switch to"
rvm list
return 1
fi
if [ $to = 'default' ]; then
unalias ruby > /dev/null 2>&1 # don't care if 'ruby' alias exists or not
export PATH=$PRE_RVM_PATH
return 0
fi
if [ $(ls -1d $MULTIRUBY_INSTALL_DIR/${to}* 2>/dev/null | wc -l) = 1 ]; then
BINDIR=$(ls -1d $MULTIRUBY_INSTALL_DIR/${to}* 2>/dev/null)/bin
else
BINDIR=$MULTIRUBY_INSTALL_DIR/$to/bin
fi
EXECUTABLE=''
if [ -d $BINDIR ]; then
if [ -x $BINDIR/ruby ]; then
EXECUTABLE=$BINDIR/ruby
else
if [ -x $BINDIR/jruby ]; then
EXECUTABLE=$BINDIR/jruby
else
if [ -x $BINDIR/rbx ]; then
EXECUTABLE=$BINDIR/rbx
fi
fi
fi
else
echo "rvm: cannot switch to $to"
rvm list
return 1
fi
if [ -z $EXECUTABLE ]; then
echo "rvm: cannot switch to $to"
rvm list
return 1
fi
export PATH=$BINDIR:$PRE_RVM_PATH
alias ruby=$EXECUTABLE
;;
show)
echo $(ruby -v)
;;
*)
retval=0
if [ $what != "help" ]; then
echo "rvm: do not know how to do $what"
retval=1
fi
echo " rvm list - list installed versions"
echo " rvm reset - undo rvm settings"
echo " rvm show - show current ruby executable"
echo " rvm switch VERSION - change default ruby executable"
return $retval
;;
esac
}
function _rvm {
local i c=1 command ver
while [ $c -lt $COMP_CWORD ]; do
i="${COMP_WORDS[c]}"
command="$i"
c=$((++c))
done
cur="${COMP_WORDS[COMP_CWORD]}"
if [ -z "$command" ]; then
COMPREPLY=($(compgen -W "$RVM_COMMANDS" -- "$cur")) && return
fi
case "$command" in
switch)
COMPREPLY=($(cd ${MULTIRUBY_INSTALL_DIR} >/dev/null; compgen -W "$(ls -d *)" -- "${COMP_WORDS[COMP_CWORD]}"))
;;
*) COMPREPLY=()
;;
esac
}
complete -o nospace -F _rvm rvm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment