feat(poetry-env): support changing between two venv dirs (#12346)
Co-authored-by: Carlo Sala <carlosalag@protonmail.com>master
parent
6e9d57d5ab
commit
31f2025e0f
1 changed files with 19 additions and 19 deletions
@ -1,27 +1,27 @@ |
|||||||
# Automatic poetry environment activation/deactivation |
|
||||||
_togglePoetryShell() { |
_togglePoetryShell() { |
||||||
# deactivate environment if pyproject.toml doesn't exist and not in a subdir |
# Determine if currently in a Poetry-managed directory |
||||||
if [[ ! -f "$PWD/pyproject.toml" ]] ; then |
local in_poetry_dir=0 |
||||||
if [[ "$poetry_active" == 1 ]]; then |
if [[ -f "$PWD/pyproject.toml" ]] && grep -q 'tool.poetry' "$PWD/pyproject.toml"; then |
||||||
if [[ "$PWD" != "$poetry_dir"* ]]; then |
in_poetry_dir=1 |
||||||
|
fi |
||||||
|
|
||||||
|
# Deactivate the current environment if moving out of a Poetry directory or into a different Poetry directory |
||||||
|
if [[ $poetry_active -eq 1 ]] && { [[ $in_poetry_dir -eq 0 ]] || [[ "$PWD" != "$poetry_dir"* ]]; }; then |
||||||
export poetry_active=0 |
export poetry_active=0 |
||||||
|
unset poetry_dir |
||||||
deactivate |
deactivate |
||||||
return |
|
||||||
fi |
|
||||||
fi |
|
||||||
fi |
fi |
||||||
|
|
||||||
# activate the environment if pyproject.toml exists |
# Activate the environment if in a Poetry directory and no environment is currently active |
||||||
if [[ "$poetry_active" != 1 ]]; then |
if [[ $in_poetry_dir -eq 1 ]] && [[ $poetry_active -ne 1 ]]; then |
||||||
if [[ -f "$PWD/pyproject.toml" ]]; then |
venv_dir=$(poetry env info --path 2>/dev/null) |
||||||
if grep -q 'tool.poetry' "$PWD/pyproject.toml" && venv_dir=$(poetry env info --path); then |
if [[ -n "$venv_dir" ]]; then |
||||||
export poetry_active=1 |
export poetry_active=1 |
||||||
export poetry_dir="$PWD" |
export poetry_dir="$PWD" |
||||||
source "${venv_dir}/bin/activate" |
source "${venv_dir}/bin/activate" |
||||||
fi |
fi |
||||||
fi |
fi |
||||||
fi |
|
||||||
} |
} |
||||||
autoload -U add-zsh-hook |
autoload -U add-zsh-hook |
||||||
add-zsh-hook chpwd _togglePoetryShell |
add-zsh-hook chpwd _togglePoetryShell |
||||||
_togglePoetryShell |
_togglePoetryShell # Initial call to check the current directory at shell startup |
||||||
|
|||||||
Loading…
Reference in new issue