mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2024-12-27 15:30:15 +08:00
feat(poetry-env): create plugin (#11069)
This commit is contained in:
parent
350427dea2
commit
1cc32be5f6
10
plugins/poetry-env/README.md
Normal file
10
plugins/poetry-env/README.md
Normal file
@ -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)
|
||||||
|
```
|
27
plugins/poetry-env/poetry-env.plugin.zsh
Normal file
27
plugins/poetry-env/poetry-env.plugin.zsh
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user