Skip to content

Instantly share code, notes, and snippets.

@webfella
Created December 5, 2013 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webfella/7811177 to your computer and use it in GitHub Desktop.
Save webfella/7811177 to your computer and use it in GitHub Desktop.
zsh script for using rvm everywhere outside of a specified directory
########################################
## Set up RVM excluding a custom path ##
########################################
# References:
# http://superuser.com/questions/521657/zsh-automatically-set-environment-variables-for-a-directory
# http://michael-prokop.at/blog/2009/05/30/directory-specific-shell-configuration-with-zsh/
# http://www.refining-linux.org/archives/42/ZSH-Gem-8-Hook-function-chpwd/
# Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
# Custom home
CUSTOM_HOME=/custom-path/codebase
# Custom path
CUSTOM_PATH=$HOME/local/bin:$CUSTOM_HOME
function check_for_custom_path() {
if [ $( echo "$PATH" | grep "$CUSTOM_PATH" ) ]
then
# return true
return 0
else
# return false
return 1
fi
}
function source_custom_path() {
# Set up path for custom codebase but save the default path
OLDPATH=$PATH
OLDGEM_HOME=$GEM_HOME
OLDGEM_PATH=$GEM_PATH
export PATH=$CUSTOM_PATH_PATH:$PATH
unset GEM_HOME
unset GEM_PATH
}
function unsource_custom_path() {
# Reset to default path
export PATH=$OLDPATH
export GEM_HOME=$OLDGEM_HOME
export GEM_PATH=$OLDGEM_PATH
OLDPATH=""
OLDGEM_HOME=""
OLDGEM_PATH=""
}
# chpwd() is a zsh hook into changing directories.
function chpwd_custom_home() {
# If this is the custom code directory and the path is not set up
if [ "${PWD##$CUSTOM_HOME}" != "$PWD" ] && [ !$( check_for_custom_path) ]
then
source_custom_path
# If the path _is_ set up for the custom path and the default path, gem home, and gem path have been stored
elif [ check_for_custom_path ] && [ "$OLDPATH" ] && [ "$OLDGEM_PATH" ] && [ "$OLDGEM_HOME" ]
then
unsource_custom_path
fi
}
# So then we add our function to the array of chpwd_functions.
chpwd_functions=(${chpwd_functions[@]} "chpwd_custom_home")
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment