Skip to content

Instantly share code, notes, and snippets.

@emiller
Created July 3, 2018 00:51
Show Gist options
  • Save emiller/9f3e159801fb2317716b45215cdfa6c8 to your computer and use it in GitHub Desktop.
Save emiller/9f3e159801fb2317716b45215cdfa6c8 to your computer and use it in GitHub Desktop.
ruby-switch
#!/bin/bash
#
# ppa: brightbox/ruby-ng/ubuntu
#
BINS="gem irb rake ruby ri rdoc erb"
VERS="1.9 1.9.1 2.0 2.1 2.2 2.3"
function display_alternatives() {
update-alternatives --display ${1:?"bin name"}
}
function install_alternative() {
update-alternatives --install /usr/bin/${1:?"bin name"} ${1:?"bin name"} /usr/bin/${1:?"bin name"}${2:?"version"} ${3:-"1"}
}
function list_alternatives() {
update-alternatives --list $1
}
function list_available() {
for ver in ${1:-$VERS}; do
for bin in $BINS; do
which $bin$ver 2>/dev/null
done
done
}
function set_alternative() {
update-alternatives --verbose --set ${1:?"bin name"} /usr/bin/${1:?"bin name"}${2:?"version"}
}
case $1 in
clear)
for ver in $VERS; do
echo "removing $ver"
for bin in $(list_available $ver); do
update-alternatives --remove $(basename ${bin/$ver/}) $bin
done
done
;;
set)
for bin in $BINS; do
set_alternative $bin ${2:?"version missing"}
done
;;
install)
for ver in $VERS; do
echo "configuring $ver"
for bin in $(list_available $ver); do
install_alternative $(basename ${bin/$ver/}) $ver
done
done
;;
check)
echo -e "current:"
for bin in $BINS; do display_alternatives $bin | sed 's/^/\t/g'; done
;;
list)
echo -e "available:"
list_available $2 | sed 's/^/\t/g'
echo -e "\ninstalled:"
for bin in $BINS; do list_alternatives $bin | sed 's/^/\t/g'; done
;;
*)
echo "$0 (check|install|list) [version]"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment