summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorajatkj <ajatkj@yahoo.co.in>2023-09-20 20:21:27 +0530
committerGitHub <noreply@github.com>2023-09-20 15:51:27 +0100
commit1cc32be5f632ff933b3e3f1c7bd9e612a274c095 (patch)
tree58f9e804588d17991992c03367bf9892278a18c8
parent350427dea2293a3e23f9823b5dc2574c1b1ae2a2 (diff)
downloadzsh-1cc32be5f632ff933b3e3f1c7bd9e612a274c095.tar.gz
zsh-1cc32be5f632ff933b3e3f1c7bd9e612a274c095.tar.bz2
zsh-1cc32be5f632ff933b3e3f1c7bd9e612a274c095.zip
feat(poetry-env): create plugin (#11069)
-rw-r--r--plugins/poetry-env/README.md10
-rw-r--r--plugins/poetry-env/poetry-env.plugin.zsh27
2 files changed, 37 insertions, 0 deletions
diff --git a/plugins/poetry-env/README.md b/plugins/poetry-env/README.md
new file mode 100644
index 000000000..a7d16563e
--- /dev/null
+++ b/plugins/poetry-env/README.md
@@ -0,0 +1,10 @@
+# Poetry Environment Plugin
+
+This plugin automatically changes poetry environment when you cd into or out of the project directory.
+Note: Script looks for pyproject.toml file to determine poetry if its a poetry environment
+
+To use it, add `poetry-env` to the plugins array in your zshrc file:
+
+```zsh
+plugins=(... poetry-env)
+```
diff --git a/plugins/poetry-env/poetry-env.plugin.zsh b/plugins/poetry-env/poetry-env.plugin.zsh
new file mode 100644
index 000000000..86e5fad4e
--- /dev/null
+++ b/plugins/poetry-env/poetry-env.plugin.zsh
@@ -0,0 +1,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