Last active
July 28, 2018 16:53
-
-
Save kbambz/10002069 to your computer and use it in GitHub Desktop.
virtualenvwrapper postactivate and predeactivate scripts to export all a .env file's declarations (foreman style) into your bash environment on activate, then clean them up on deactivate. Also tells npm and rvm to install packages directly into the active virtualenv. Useful for Heroku-hosted project development.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # This hook should be run after every virtualenv is activated. | |
| # See `predeactivate-env-cleanup` to configure automatic clean-up. | |
| # | |
| # INSTALLATION | |
| # Save the latest version of this file into your virtualenvwrapper | |
| # hook directory (e.g., ~/.virtualenvs): | |
| # $ echo "$(curl -fsSl https://gist.githubusercontent.com/kbambz/10002069/raw/postactivate-env-setup)" > $VIRTUALENVWRAPPER_HOOK_DIR/postactivate-env-setup | |
| # | |
| # Then tell postactivate to invoke the script when it runs: | |
| # $ chmod +x $VIRTUALENVWRAPPER_HOOK_DIR/postactivate-env-setup | |
| # $ echo "[ -f \$VIRTUALENVWRAPPER_HOOK_DIR/postactivate-env-setup ] && source \$VIRTUALENVWRAPPER_HOOK_DIR/postactivate-env-setup" >> $VIRTUALENVWRAPPER_HOOK_DIR/postactivate | |
| # | |
| # @author Kate Bambino (kbambz) | |
| echo "" | |
| echo -e "\033[1;32mActivating virtualenv \033[0;35m$(basename $VIRTUAL_ENV)\033[0m" | |
| # Check if a project is associated with this virtual environment | |
| if [ -e "$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME" ]; then | |
| VIRTUAL_ENV_PROJECT_PATH=`cat "$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME"` | |
| echo -e " \033[1;32m@ \033[0;35m$VIRTUAL_ENV_PROJECT_PATH\033[0m" | |
| echo "" | |
| # Read project .env file declarations into bash environment | |
| if [ -e "$VIRTUAL_ENV_PROJECT_PATH/.env" ]; then | |
| echo -e "\033[1;37mReading project's .env file declarations into bash environment...\033[0m" | |
| while read line; do | |
| [ -z "$line" ] && continue | |
| [ -z "${line%%#*}" ] && continue | |
| export ${line%%=*}="${line#*=}" | |
| echo " > ${line%%=*}" | |
| done < "$VIRTUAL_ENV_PROJECT_PATH/.env" | |
| else | |
| echo -e "\033[1;33mWARNING:\033[0m Project root does not contain an .env file." | |
| fi | |
| else | |
| echo -e "\033[1;33mWARNING:\033[0m There is no project directory associated with this virtualenv. To set one, cd into the project root and run \`setvirtualenvproject \$VIRTUAL_ENV \$(pwd) && workon \$(basename \$VIRTUAL_ENV)'" | |
| fi | |
| echo "" | |
| # Make it so npm install -g <package> and rvm install <package> | |
| # install directly into the active virtual environment | |
| # Save existing values for cleanup in predeactivate | |
| if [[ -n $GEM_HOME ]]; then | |
| export OLD_GEM_HOME=$GEM_HOME | |
| fi | |
| if [[ -n $GEM_PATH ]]; then | |
| export OLD_GEM_PATH=$GEM_PATH | |
| fi | |
| if [[ -n $npm_config_prefix ]]; then | |
| export OLD_npm_config_prefix=$npm_config_prefix | |
| fi | |
| export GEM_HOME="$VIRTUAL_ENV/lib/gems" | |
| export GEM_PATH="" | |
| PATH="$GEM_HOME/bin:$PATH" | |
| export npm_config_prefix=$VIRTUAL_ENV | |
| export PATH |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # This hook should be run before every virtualenv is deactivated. | |
| # It was designed to clean up any bash environment modifications | |
| # made by postactivate hook `postactivate-env-setup`. | |
| # | |
| # INSTALLATION | |
| # Save the latest version of this file into your virtualenvwrapper | |
| # hook directory (e.g., ~/.virtualenvs): | |
| # $ echo "$(curl -fsSl https://gist.githubusercontent.com/kbambz/10002069/raw/predeactivate-env-cleanup)" > $VIRTUALENVWRAPPER_HOOK_DIR/predeactivate-env-cleanup | |
| # | |
| # Then tell predeactivate to invoke the script when it runs: | |
| # $ chmod +x $VIRTUALENVWRAPPER_HOOK_DIR/predeactivate-env-cleanup | |
| # $ echo "[ -f \$VIRTUALENVWRAPPER_HOOK_DIR/predeactivate-env-cleanup ] && source \$VIRTUALENVWRAPPER_HOOK_DIR/predeactivate-env-cleanup" >> $VIRTUALENVWRAPPER_HOOK_DIR/predeactivate | |
| # | |
| # @author Kate Bambino (kbambz) | |
| # Check if a project is associated with this virtual environment | |
| if [ -e "$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME" ]; then | |
| VIRTUAL_ENV_PROJECT_PATH=`cat "$VIRTUAL_ENV/$VIRTUALENVWRAPPER_PROJECT_FILENAME"` | |
| if [ -e "$VIRTUAL_ENV_PROJECT_PATH/.env" ]; then | |
| echo -e "\033[1;37mCleaning up .env file associated bash assignments...\033[0m" | |
| # Clear .env file declarations from your bash environment | |
| while read line; do | |
| [ -z "$line" ] && continue | |
| [ -z "${line%%#*}" ] && continue | |
| if [[ -n ${line%%=*} ]]; then | |
| unset ${line%%=*} | |
| echo " > ${line%%=*}" | |
| fi | |
| done < "$VIRTUAL_ENV_PROJECT_PATH/.env" | |
| fi | |
| fi | |
| # Make it so npm install -g <package> and rvm install <package> | |
| # install to wherever they were set up to before this virtualenv | |
| # was activated | |
| if [[ -n $OLD_GEM_HOME ]]; then | |
| export GEM_HOME=$OLD_GEM_HOME | |
| unset OLD_GEM_HOME | |
| else | |
| unset GEM_HOME | |
| fi | |
| if [[ -n $OLD_GEM_PATH ]]; then | |
| export GEM_PATH=$OLD_GEM_PATH | |
| unset OLD_GEM_PATH | |
| else | |
| unset GEM_PATH | |
| fi | |
| if [[ -n $OLD_npm_config_prefix ]]; then | |
| export npm_config_prefix=$OLD_npm_config_prefix | |
| unset OLD_npm_config_prefix | |
| else | |
| unset npm_config_prefix | |
| fi |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To fetch and install both scripts globally:
If you'd rather the scripts only run for specific projects/virtualenvs, install them into each virtualenv's
bindirectory instead. That is, from an activate virtualenv, do:Navigate to a project's root directory and run
workon <some-venv>to verify installation, noting you may need to deactivate your active virtualenv first.If you ever want to disable the scripts, either remove
postactivate-env-setupandpredeactivate-env-cleanupfrom$VIRTUALENVWRAPPER_HOOK_DIRor comment out the lines referencing them from yourpostactivateandpredeactivatescripts.