-
Notifications
You must be signed in to change notification settings - Fork 14
/
poetry.zsh
executable file
·48 lines (41 loc) · 1.28 KB
/
poetry.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
#!/usr/bin/env zsh
ZSH_POETRY_AUTO_ACTIVATE=${ZSH_POETRY_AUTO_ACTIVATE:-1}
ZSH_POETRY_AUTO_DEACTIVATE=${ZSH_POETRY_AUTO_DEACTIVATE:-1}
autoload -U add-zsh-hook
_zp_current_project=
_zp_check_poetry_venv() {
local venv
if [[ -z $VIRTUAL_ENV ]] && [[ -n "${_zp_current_project}" ]]; then
_zp_current_project=
fi
if [[ -f pyproject.toml ]] \
&& [[ "${PWD}" != "${_zp_current_project}" ]]; then
if [[ -n $_zp_current_project ]]; then
deactivate
fi
# if pyproject doesn't use poetry fail silently
if [[ "$(poetry env list &> /dev/null; echo $?)" != "0" ]]; then
return 1
fi
venv="$(command poetry env list --full-path | grep Activated | sed "s/ .*//" \
| head -1)"
if [[ -d "$venv" ]] && [[ "$venv" != "$VIRTUAL_ENV" ]]; then
source "$venv"/bin/activate || return $?
_zp_current_project="${PWD}"
return 0
fi
elif [[ -n $VIRTUAL_ENV ]] \
&& [[ -n $ZSH_POETRY_AUTO_DEACTIVATE ]] \
&& [[ "${PWD}" != "${_zp_current_project}" ]] \
&& [[ "${PWD}" != "${_zp_current_project}"/* ]]; then
deactivate
_zp_current_project=
return $?
fi
return 1
}
add-zsh-hook chpwd _zp_check_poetry_venv
poetry-shell() {
_zp_check_poetry_venv
}
[[ -n $ZSH_POETRY_AUTO_ACTIVATE ]] && _zp_check_poetry_venv