Skip to content

Instantly share code, notes, and snippets.

@st63jun
Last active August 29, 2015 14:01
Show Gist options
  • Save st63jun/4fc34cf1e4a35180e05f to your computer and use it in GitHub Desktop.
Save st63jun/4fc34cf1e4a35180e05f to your computer and use it in GitHub Desktop.
switch-ruby
#!/bin/bash
#
# switch-ruby - Simple Ruby switcher
#
# The MIT License (MIT)
#
# Copyright (c) 2014 Jun SAITO <jun.saito@u-cast.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
RUBIES_HOME=$HOME/.rubies
CURRENT=$RUBIES_HOME/current
AVAILABLE_RUBIES=($(find $RUBIES_HOME -maxdepth 1 -type d | grep -v "^$RUBIES_HOME$"))
function show_rubies() {
current=$(readlink -f $CURRENT)
i=0
for ruby in ${AVAILABLE_RUBIES[@]}; do
if [ $ruby == $current ]; then
echo "--> " \($i\) $ruby
else
echo " " \($i\) $ruby
fi
i=$((i+1))
done
}
function switch_ruby()
{
if [ -z $1 ]; then
usage
exit 1
fi
len=${#AVAILABLE_RUBIES[*]}
if [ $1 -lt 0 -o $(expr $len - 1) -lt $1 ]; then
echo "Error: Invalid number."
exit 1
fi
if [ -e $CURRENT ]; then
unlink $CURRENT
fi
ln -sv ${AVAILABLE_RUBIES[$1]} $CURRENT
}
function usage()
{
cat <<EOF
Usage: $(basename $0) <command>
Available commands:
list List installed rubies.
switch <number> Switch ruby.
EOF
}
case $1 in
ls | list) show_rubies ;;
sw | switch) switch_ruby $2 ;;
*) usage ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment