44 lines
1.3 KiB
Bash
Executable File
44 lines
1.3 KiB
Bash
Executable File
source "$(which virtualenvwrapper_lazy.sh)"
|
|
type workon >/dev/null 2>&1
|
|
[ -z "$WORKON_HOME" ] && {
|
|
echo "$WORKON_HOME unset!"
|
|
return 1
|
|
}
|
|
|
|
workon_cwd() {
|
|
local PROJECT_ROOT="${PWD}"
|
|
|
|
while [ "$PROJECT_ROOT" != "/" ] && [ ! -e "$PROJECT_ROOT/.venv" ] && [ ! -d "$PROJECT_ROOT/.git" ]; do
|
|
local PROJECT_ROOT="$(dirname "$PROJECT_ROOT")"
|
|
done
|
|
|
|
if [ -r "$PROJECT_ROOT/.venv" ]; then
|
|
local ENV_NAME="$(cat $PROJECT_ROOT/.venv)"
|
|
# else
|
|
# ENV_NAME="$(basename "$PROJECT_ROOT")"
|
|
fi
|
|
|
|
[ -n "$CD_VIRTUAL_ENV" ] && [ "$ENV_NAME" != "$CD_VIRTUAL_ENV" ] && {
|
|
[ -n "$VIRTUAL_ENV" ] && deactivate
|
|
unset CD_VIRTUAL_ENV
|
|
}
|
|
|
|
[ -n "$ENV_NAME" ] && [ ! "$(realpath "$VIRTUAL_ENV" 2>/dev/null)" = "$(realpath "$WORKON_HOME/$ENV_NAME")" ] && {
|
|
if [ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]; then
|
|
workon -n "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
|
|
elif [ -e "$ENV_NAME/bin/activate" ]; then
|
|
source "$ENV_NAME/bin/activate" && export CD_VIRTUAL_ENV="$ENV_NAME"
|
|
else
|
|
local ENV_NAME=""
|
|
fi
|
|
}
|
|
|
|
[ -z "$ENV_NAME" ] && [ -n "$CD_VIRTUAL_ENV" ] && [ -n "$VIRTUAL_ENV" ] && {
|
|
deactivate && unset CD_VIRTUAL_ENV
|
|
}
|
|
}
|
|
|
|
autoload -U add-zsh-hook
|
|
add-zsh-hook chpwd workon_cwd
|
|
[ "$PWD" != ~ ] && workon_cwd
|