1. set ENV_NAME to current folder name instead of an empty string when .venv file is not found

2. optimized activate & deactivated logic
This commit is contained in:
Ken Lai 2015-07-22 17:45:21 +08:00
parent a207a38d63
commit 8f571fe4e5

View File

@ -53,22 +53,21 @@ if [[ ! $DISABLE_VENV_CD -eq 1 ]]; then
elif [[ "$PROJECT_ROOT" != "." ]]; then elif [[ "$PROJECT_ROOT" != "." ]]; then
ENV_NAME="${PROJECT_ROOT:t}" ENV_NAME="${PROJECT_ROOT:t}"
else else
ENV_NAME="" FOLDER_NAME=`pwd`
ENV_NAME=`basename "$FOLDER_NAME"`
fi fi
if [[ "$ENV_NAME" != "" ]]; then
# Activate the environment only if it is not already active # Activate the environment only if it is not already active
if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then if [[ "$VIRTUAL_ENV" != "$WORKON_HOME/$ENV_NAME" ]]; then
if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then if [[ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]]; then
workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME" workon "$ENV_NAME" && export CD_VIRTUAL_ENV=`pwd`
elif [[ -e "$ENV_NAME/bin/activate" ]]; then elif [[ -e "$ENV_NAME/bin/activate" ]]; then
source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV="$ENV_NAME" source $ENV_NAME/bin/activate && export CD_VIRTUAL_ENV=`pwd`
fi elif [[ `pwd` != "$CD_VIRTUAL_ENV"* ]]; then
fi
elif [[ -n $CD_VIRTUAL_ENV && -n $VIRTUAL_ENV ]]; then
# We've just left the repo, deactivate the environment # We've just left the repo, deactivate the environment
# Note: this only happens if the virtualenv was activated automatically # Note: this only happens if the virtualenv was activated automatically
deactivate && unset CD_VIRTUAL_ENV deactivate && unset CD_VIRTUAL_ENV
fi fi
fi
unset PROJECT_ROOT unset PROJECT_ROOT
unset WORKON_CWD unset WORKON_CWD
fi fi