Skip to content

Instantly share code, notes, and snippets.

@isaquealves
Last active January 26, 2016 15:15
Show Gist options
  • Save isaquealves/4504389549c7a922e043 to your computer and use it in GitHub Desktop.
Save isaquealves/4504389549c7a922e043 to your computer and use it in GitHub Desktop.
Runs common rvm commands using chpwd_functions from zsh. Add this to the end of .zshrc file. Runs on 'cd <directory>'
source ~/.zshrc
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
ORIGINAL_PATH=$PATH
function dev_env_setup() {
# see http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html
emulate -L zsh
# checks if current directory contains the dot-ruby-version and dot-ruby-gemset files
# if so, reads the ruby version and gemset from files to run 'rvm use $rb@$gemset' and
# set necessary ruby environment settings
export CWED=0
if [[ (-e ".ruby-version") && (-a ".ruby-gemset") ]];
then
local rb=$(cat '.ruby-version')
local gemset=$(cat '.ruby-gemset')
source ~/.rvm/scripts/rvm
rvm use "$rb@$gemset"
elif [[ (-e "composer.json") ]];
then
if [[ -d `pwd`/bin ]];
then
export CWED=1
PATH="$PATH:`pwd`/bin"
fi
else
if [[ -e ".project" ]];
then
export CWED=1
if ! type workon &> /dev/null ;
then
echo "You are entering a python project directory, but virtualenv is not properly installed or configured"
echo "Do you want to proceed with the installation? [y]|n"
read response
if [ $response == 'n' ];
then
return;
else
sudo pip install virtualenvwrapper virtualenv
fi
else
local project=$(cat '.project')
local project_root="$(pwd)"
if [[ ! ($project = $(workon | grep $project )) ]];
then
echo "To properly work on the project, we will need to setup your envvironment."
echo "Will now run mkvirtualenv -a $project_root -r $project_root/requirements.txt $project"
mkvirtualenv -a $project_root -r $project_root/requirements.txt $project
echo "The following packages were installed:"
pip list
fi
echo "Will enter now the $project environment"
workon $project
echo "Welcome! Now, you can safely work on $project."
fi
else
if ! [[ -z $VIRTUAL_ENV ]];
then
deactivate
export CWED=0
elif ! [[ $ORIGINAL_PATH = $PATH ]];
then
export PATH="$ORIGINAL_PATH"
export CWED=0
else
return ;
fi
fi
fi;
}
chpwd_functions=(${chpwd_functions[@]} "dev_env_setup")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment