Skip to content

Instantly share code, notes, and snippets.

@blowmage
Last active August 29, 2015 13:55
Show Gist options
  • Save blowmage/8693982 to your computer and use it in GitHub Desktop.
Save blowmage/8693982 to your computer and use it in GitHub Desktop.
chruby and ohmygems together at last
# my preferred prompt - ☣ [pairwithme:rails4] $
PS1='\[\e[1;31m\]☣ [\W$(if [ -n "$OMG_NAME" ]; then eval "echo :$OMG_NAME"; fi)] $\[\e[0m\] '
# chruby configuration
source /usr/local/share/chruby/chruby.sh
source /usr/local/share/chruby/auto.sh
# set default ruby
chruby 2.1
# ohmygems configuration
source ~/ohmygems.sh
source ~/ohmygems_chruby.sh
# -*- sh -*-
ohmygems() {
name=${1:-}
if [ -z "${ORIG_GEM_PATH:-}" ]; then
export ORIG_GEM_PATH=${GEM_PATH:-}
export ORIG_GEM_HOME=${GEM_HOME:-}
export ORIG_PATH=${PATH}
fi
if [ -z "$name" ]; then
echo "usage: ohmygems <name or reset or env>"
echo
echo " Switches gem home to a (potentially new) named repo."
echo " Your previous gems are still visible,"
echo " but new gem installs will install into ~/.gem/repos/<name>."
echo " Use 'reset' to go back to normal."
echo " Use 'env' to view environment information."
echo
echo "Available repos in ~/.gem/repos:"
echo
ls ~/.gem/repos | pr -o2 -l1
echo
return
elif [ $name = "reset" ]; then
echo Resetting repo
if [ -z "$ORIG_GEM_HOME" ]; then
unset GEM_HOME
else
GEM_HOME=${ORIG_GEM_HOME}
fi
GEM_PATH=${ORIG_GEM_PATH}
PATH=$ORIG_PATH
unset OMG_NAME
elif [ $name = "env" ]; then
echo Oh My Gems Environment
echo GEM_PATH=${GEM_PATH:-not set}
echo GEM_HOME=${GEM_HOME:-not set}
echo PATH=$PATH
else
echo Switching to $name
export GEM_HOME=~/.gem/repos/${name}
export GEM_PATH=${ORIG_GEM_HOME}:${ORIG_GEM_PATH}
export PATH=${ORIG_PATH}:${GEM_HOME}/bin
[ -d $GEM_HOME ] || mkdir -p $GEM_HOME
OMG_NAME=${name}
fi
}
alias omg=ohmygems
# Reload ohmygems when chruby is called.
# Inspired by chruby's after hook wiki:
# https://github.com/postmodern/chruby/wiki/Implementing-an-'after-use'-hook
save_function() {
local ORIG_FUNC=$(declare -f $1)
local NEWNAME_FUNC="$2${ORIG_FUNC#$1}"
eval "$NEWNAME_FUNC"
}
save_function chruby ohmygems_chruby
function chruby () {
# Are we in a gemset?
omg_name=${OMG_NAME}
# Reset ohmygems
ohmygems reset
# Including removing the ENV variables
unset ORIG_GEM_PATH
unset ORIG_GEM_HOME
unset ORIG_PATH
# Switch ruby
ohmygems_chruby $*
# Reload gem repo after switching rubies
if [ -n "$omg_name" ]; then
ohmygems ${omg_name}
fi
}
☣ [~] $ time ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [universal.x86_64-darwin13]
real 0m0.006s
user 0m0.004s
sys 0m0.002s
☣ [~] $ time rails -v
Rails is not currently installed on this system. To get the latest version, simply type:
$ sudo gem install rails
You can then rerun your "rails" command.
real 0m0.042s
user 0m0.029s
sys 0m0.011s
☣ [~] $ chruby 1.9
☣ [~] $ time ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-darwin13.0.0]
real 0m0.006s
user 0m0.003s
sys 0m0.002s
☣ [~] $ time rails -v
Rails is not currently installed on this system. To get the latest version, simply type:
$ sudo gem install rails
You can then rerun your "rails" command.
real 0m0.039s
user 0m0.027s
sys 0m0.010s
☣ [~] $ omg rails4
Switching to rails4
☣ [~:rails4] $ time ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-darwin13.0.0]
real 0m0.007s
user 0m0.003s
sys 0m0.002s
☣ [~:rails4] $ time rails -v
Rails 4.0.2
real 0m0.086s
user 0m0.054s
sys 0m0.028s
☣ [~:rails4] $ chruby 2.1
Switching to rails4
☣ [~:rails4] $ time ruby -v
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin13.0]
real 0m0.008s
user 0m0.005s
sys 0m0.003s
☣ [~:rails4] $ time rails -v
Rails 4.0.2
real 0m0.084s
user 0m0.051s
sys 0m0.027s
@blowmage
Copy link
Author

My ohmygems changes:

  • Fixed typo in usage
  • Will now create the gem directory if needed
  • Split env info out to omg env
  • Store gem name in OMG_NAME, so I can display it in my prompt
  • Prepended GEM_HOME to PATH instead of appending it

See @zenspider's original post for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment