-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path.autoenv.zsh
48 lines (41 loc) · 1.46 KB
/
.autoenv.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Environment file for all projects.
# - (de)activates Python virtualenvs (.venv) from pipenv
if [[ $autoenv_event == 'enter' ]]; then
autoenv_source_parent
_my_autoenv_venv_chpwd() {
if [[ -z "$_ZSH_ACTIVATED_VIRTUALENV" && -n "$VIRTUAL_ENV" ]]; then
return
fi
setopt localoptions extendedglob
local -a venv
venv=(./(../)#.venv(NY1:A))
if [[ -n "$_ZSH_ACTIVATED_VIRTUALENV" && -n "$VIRTUAL_ENV" ]]; then
if ! (( $#venv )) || [[ "$_ZSH_ACTIVATED_VIRTUALENV" != "$venv[1]" ]]; then
unset _ZSH_ACTIVATED_VIRTUALENV
echo "De-activating virtualenv: ${(D)VIRTUAL_ENV}" >&2
# Simulate "deactivate", but handle $PATH better (remove VIRTUAL_ENV).
if ! autoenv_remove_path $VIRTUAL_ENV/bin; then
echo "warning: ${VIRTUAL_ENV}/bin not found in \$PATH" >&2
fi
# NOTE: does not handle PYTHONHOME/_OLD_VIRTUAL_PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
# NOTE: does not handle PS1/_OLD_VIRTUAL_PS1
unset _OLD_VIRTUAL_PS1
unset VIRTUAL_ENV
fi
fi
if [[ -z "$VIRTUAL_ENV" ]]; then
if (( $#venv )); then
echo "Activating virtualenv: ${(D)venv}" >&2
export VIRTUAL_ENV=$venv[1]
autoenv_prepend_path $VIRTUAL_ENV/bin
_ZSH_ACTIVATED_VIRTUALENV="$venv[1]"
fi
fi
}
autoload -U add-zsh-hook
add-zsh-hook chpwd _my_autoenv_venv_chpwd
_my_autoenv_venv_chpwd
else
add-zsh-hook -d chpwd _my_autoenv_venv_chpwd
fi