a22c3a1b20f2ca2c97ed3d2b1ae81c82f4e149a1

Author
Michael Aquilina <michaelaquilina@gmail.com>
Committer
Michael Aquilina <michaelaquilina@gmail.com>
Date

Message

Add support for autoswitching poetry

Diff

 1diff --git a/autoswitch_virtualenv.plugin.zsh b/autoswitch_virtualenv.plugin.zsh
 2index 605c2544b5d5623563161012493c5da030513dde..c2f0182b2b1614eadbb31997e30ded20cc31af1f 100644
 3--- a/autoswitch_virtualenv.plugin.zsh
 4+++ b/autoswitch_virtualenv.plugin.zsh
 5@@ -54,6 +54,10 @@ function _get_venv_type() {
 6         venv_type="pipenv"
 7     elif [[ -f "$venv_dir/requirements.txt" || -f "$venv_dir/setup.py" ]]; then
 8         venv_type="virtualenv"
 9+    # TODO: this is not technically correct as lots of tools use pyproject.toml
10+    # however it's probably a good enough approximation for this function
11+    elif [[ -f "$venv_dir/pyproject.toml" ]]; then
12+        venv_type="poetry"
13     fi
14     printf "%s" "$venv_type"
15 }
16@@ -170,6 +174,8 @@ function check_venv()
17 
18     # check if Pipfile exists rather than invoking pipenv as it is slow
19     local pipfile_path="$(_check_path "$PWD" "Pipfile")"
20+    # Same logic applies to poetry
21+    local pyproject_path="$(_check_path "$PWD" "pyproject.toml")"
22 
23     if [[ -n "$pipfile_path" ]] && type "pipenv" > /dev/null; then
24         # unfortunately running pipenv each time we are in a pipenv project directory is slow :(
25@@ -177,6 +183,12 @@ function check_venv()
26             _maybeworkon "$venv_path" "pipenv"
27             return
28         fi
29+    elif [[ -n "$pyproject_path" ]] && type "poetry" > /dev/null; then
30+        # we need to infer the target virtualenv directory based on poetry's data
31+        if venv_path="$(dirname $(dirname $(poetry run which python)))"; then
32+            _maybeworkon "$venv_path" "poetry"
33+            return
34+        fi
35     fi
36 
37     local venv_type="$(_get_venv_type "$PWD" "unknown")"