layout poetry
change
commit c0161e1f9daafbb5fa1f6de9e82a74a03bdc0cad
author Alpha Chen <alpha.chen@brilliant.org>
date
parent 388cec0e
diff --git a/.config/direnv/direnvrc b/.config/direnv/direnvrc
index 142a87a0..66bb1c9c 100644
--- a/.config/direnv/direnvrc
+++ b/.config/direnv/direnvrc
@@ -27,3 +27,28 @@ use_ruby() {
   # bin directory to take precedence
   PATH_add ./.direnv/bin
 }
+
+# https://github.com/direnv/direnv/wiki/Python/#poetry
+layout_poetry() {
+    PYPROJECT_TOML="${PYPROJECT_TOML:-pyproject.toml}"
+    if [[ ! -f "$PYPROJECT_TOML" ]]; then
+        log_status "No pyproject.toml found. Executing \`poetry init\` to create a \`$PYPROJECT_TOML\` first."
+        poetry init
+    fi
+
+    if [[ -d ".venv" ]]; then
+        VIRTUAL_ENV="$(pwd)/.venv"
+    else
+        VIRTUAL_ENV=$(poetry env info --path 2>/dev/null ; true)
+    fi
+
+    if [[ -z $VIRTUAL_ENV || ! -d $VIRTUAL_ENV ]]; then
+        log_status "No virtual environment exists. Executing \`poetry install\` to create one."
+        poetry install
+        VIRTUAL_ENV=$(poetry env info --path)
+    fi
+
+    PATH_add "$VIRTUAL_ENV/bin"
+    export POETRY_ACTIVE=1
+    export VIRTUAL_ENV
+}