summaryrefslogtreecommitdiff
path: root/plugins/poetry-env/poetry-env.plugin.zsh
blob: 86e5fad4ec25c1c38e6469ffd37f2a6c0adc3b66 (plain)
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
# Automatic poetry environment activation/deactivation
_togglePoetryShell() {
  # deactivate environment if pyproject.toml doesn't exist and not in a subdir
  if [[ ! -f "$PWD/pyproject.toml" ]] ; then
    if [[ "$poetry_active" == 1 ]]; then
      if [[ "$PWD" != "$poetry_dir"* ]]; then
        export poetry_active=0
        deactivate
        return
      fi
    fi
  fi

  # activate the environment if pyproject.toml exists
  if [[ "$poetry_active" != 1 ]]; then
    if [[ -f "$PWD/pyproject.toml" ]]; then
      if grep -q 'tool.poetry' "$PWD/pyproject.toml"; then
        export poetry_active=1
        export poetry_dir="$PWD"
        source "$(poetry env info --path)/bin/activate"
      fi
    fi
  fi
}
autoload -U add-zsh-hook
add-zsh-hook chpwd _togglePoetryShell
_togglePoetryShell