1
0
Fork 0

virtualenvwrapper plugin fixes

This commit is contained in:
Luca Bilke 2024-02-22 13:45:56 +01:00
parent 82ce30200d
commit 334f8e7558
1 changed files with 19 additions and 12 deletions

31
.config/zsh/plugins/virtualenvwrapper.plugin.zsh Normal file → Executable file
View File

@ -1,34 +1,41 @@
. "$(which virtualenvwrapper_lazy.sh)"
source "$(which virtualenvwrapper_lazy.sh)"
type workon >/dev/null 2>&1
[ -z "$WORKON_HOME" ] || { echo "$WORKON_HOME unset!"; exit 1; }
[ -z "$WORKON_HOME" ] && {
echo "$WORKON_HOME unset!"
return 1
}
workon_cwd() {
[ -z "$WORKON_CWD" ] && {
WORKON_CWD=1
PROJECT_ROOT="${PWD}"
}
PROJECT_ROOT="${PWD}"
while [ "$PROJECT_ROOT" != "/" ] && [ ! -e "$PROJECT_ROOT/.venv" ] && [ ! -d "$PROJECT_ROOT/.git" ]; do
PROJECT_ROOT="$(dirname "$PROJECT_ROOT")"
done
[ "$PROJECT_ROOT" != "/" ] && ENV_NAME="$(basename "$PROJECT_ROOT")"
if [ -r "$PROJECT_ROOT/.venv" ]; then
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
}
[ -z "$ENV_NAME" ] && [ ! "$(realpath "$VIRTUAL_ENV")" = "$(realpath "$WORKON_HOME/$ENV_NAME")" ] && {
[ -n "$ENV_NAME" ] && [ ! "$(realpath "$VIRTUAL_ENV" 2>/dev/null)" = "$(realpath "$WORKON_HOME/$ENV_NAME")" ] && {
if [ -e "$WORKON_HOME/$ENV_NAME/bin/activate" ]; then
workon "$ENV_NAME" && export CD_VIRTUAL_ENV="$ENV_NAME"
elif [ -e "$ENV_NAME/bin/activate" ]; then
. "$ENV_NAME/bin/activate" && export CD_VIRTUAL_ENV="$ENV_NAME"
source "$ENV_NAME/bin/activate" && export CD_VIRTUAL_ENV="$ENV_NAME"
else
ENV_NAME=""
fi
}
[ -z "$ENV_NAME" ] && [ -n "$CD_VIRTUAL_ENV" ] && [ -n "$VIRTUAL_ENV" ] && deactivate && unset CD_VIRTUAL_ENV
[ -z "$ENV_NAME" ] && [ -n "$CD_VIRTUAL_ENV" ] && [ -n "$VIRTUAL_ENV" ] && {
deactivate && unset CD_VIRTUAL_ENV
}
}
autoload -U add-zsh-hook