poetry-env.plugin.zsh 821 B

123456789101112131415161718192021222324252627
  1. # Automatic poetry environment activation/deactivation
  2. _togglePoetryShell() {
  3. # deactivate environment if pyproject.toml doesn't exist and not in a subdir
  4. if [[ ! -f "$PWD/pyproject.toml" ]] ; then
  5. if [[ "$poetry_active" == 1 ]]; then
  6. if [[ "$PWD" != "$poetry_dir"* ]]; then
  7. export poetry_active=0
  8. deactivate
  9. return
  10. fi
  11. fi
  12. fi
  13. # activate the environment if pyproject.toml exists
  14. if [[ "$poetry_active" != 1 ]]; then
  15. if [[ -f "$PWD/pyproject.toml" ]]; then
  16. if grep -q 'tool.poetry' "$PWD/pyproject.toml" && venv_dir=$(poetry env info --path); then
  17. export poetry_active=1
  18. export poetry_dir="$PWD"
  19. source "${venv_dir}/bin/activate"
  20. fi
  21. fi
  22. fi
  23. }
  24. autoload -U add-zsh-hook
  25. add-zsh-hook chpwd _togglePoetryShell
  26. _togglePoetryShell