Skip to content

Instantly share code, notes, and snippets.

@pedrohenriquebr
Forked from kbambz/postactivate-env-setup
Last active July 28, 2018 16:22
Show Gist options
  • Select an option

  • Save pedrohenriquebr/c317052a9407f267cca05f53624141dc to your computer and use it in GitHub Desktop.

Select an option

Save pedrohenriquebr/c317052a9407f267cca05f53624141dc 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.
#!/usr/bin/env bash
setup_scripts=(
postactivate-env-setup
predeactivate-env-cleanup
)
target_scripts=(
postactivate
predeactivate
)
declare -A scripts_map
# Prepare the map
for (( i = 0; i < ${#setup_scripts[@]}; i++ )); do
key=${setup_scripts[$i]}
value=${target_scripts[$i]}
scripts_map[$key]=$value
done
for item in ${setup_scripts[@]}; do
# Remove all previous scripts versions
rm -vf $VIRTUALENVWRAPPER_HOOK_DIR/$item
# Copy the setup scripts
cat $item > $VIRTUALENVWRAPPER_HOOK_DIR/$item
# Set the executable bit
chmod -v +x $VIRTUALENVWRAPPER_HOOK_DIR/$item
done
# Clean all previous lines without '#'
for item in ${target_scripts[@]}; do
sed -i '/^[^#]/ d' $VIRTUALENVWRAPPER_HOOK_DIR/$item
done
for script_key in ${!scripts_map[@]}; do
echo "[ -f \$VIRTUALENVWRAPPER_HOOK_DIR/${scripts_map[$script_key]} ] && source \$VIRTUALENVWRAPPER_HOOK_DIR/$script_key" >> $VIRTUALENVWRAPPER_HOOK_DIR/${scripts_map[$script_key]}
done
#!/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/pedrohenriquebr/c317052a9407f267cca05f53624141dc/raw/ca68c627ce8ea4c847756996313f7c85b57e5ea1/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
#!/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/pedrohenriquebr/c317052a9407f267cca05f53624141dc/raw/3b00900a75994da6d29fc5d35532f8b0bd882a20/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
#!/usr/bin/env bash
target_scripts=(
postactivate
postdeactivate
predeactivate
preactivate
)
rm -vf $VIRTUALENVWRAPPER_HOOK_DIR/*"env-setup"
rm -vf $VIRTUALENVWRAPPER_HOOK_DIR/*"env-cleanup"
for item in ${target_scripts[@]};
do
sed -i '/^[^#]/ d' $VIRTUALENVWRAPPER_HOOK_DIR/$item
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment