1cc32be5f632ff933b3e3f1c7bd9e612a274c095

Author
ajatkj <ajatkj@yahoo.co.in>
Committer
GitHub <noreply@github.com>
Date

Message

feat(poetry-env): create plugin (#11069)

Diff

 1diff --git a/plugins/poetry-env/README.md b/plugins/poetry-env/README.md
 2new file mode 100644
 3index 0000000000000000000000000000000000000000..a7d16563ed69204e4bf8b9182710c22a112df0f3
 4--- /dev/null
 5+++ b/plugins/poetry-env/README.md
 6@@ -0,0 +1,10 @@
 7+# Poetry Environment Plugin
 8+
 9+This plugin automatically changes poetry environment when you cd into or out of the project directory. 
10+Note: Script looks for pyproject.toml file to determine poetry if its a poetry environment
11+
12+To use it, add `poetry-env` to the plugins array in your zshrc file:
13+
14+```zsh
15+plugins=(... poetry-env)
16+```
17diff --git a/plugins/poetry-env/poetry-env.plugin.zsh b/plugins/poetry-env/poetry-env.plugin.zsh
18new file mode 100644
19index 0000000000000000000000000000000000000000..86e5fad4ec25c1c38e6469ffd37f2a6c0adc3b66
20--- /dev/null
21+++ b/plugins/poetry-env/poetry-env.plugin.zsh
22@@ -0,0 +1,27 @@
23+# Automatic poetry environment activation/deactivation
24+_togglePoetryShell() {
25+  # deactivate environment if pyproject.toml doesn't exist and not in a subdir
26+  if [[ ! -f "$PWD/pyproject.toml" ]] ; then
27+    if [[ "$poetry_active" == 1 ]]; then
28+      if [[ "$PWD" != "$poetry_dir"* ]]; then
29+        export poetry_active=0
30+        deactivate
31+        return
32+      fi
33+    fi
34+  fi
35+
36+  # activate the environment if pyproject.toml exists
37+  if [[ "$poetry_active" != 1 ]]; then
38+    if [[ -f "$PWD/pyproject.toml" ]]; then
39+      if grep -q 'tool.poetry' "$PWD/pyproject.toml"; then
40+        export poetry_active=1
41+        export poetry_dir="$PWD"
42+        source "$(poetry env info --path)/bin/activate"
43+      fi
44+    fi
45+  fi
46+}
47+autoload -U add-zsh-hook
48+add-zsh-hook chpwd _togglePoetryShell
49+_togglePoetryShell