From c0161e1f9daafbb5fa1f6de9e82a74a03bdc0cad Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Mon, 9 Oct 2023 16:36:41 -0700 Subject: [PATCH] layout poetry --- .config/direnv/direnvrc | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/.config/direnv/direnvrc b/.config/direnv/direnvrc index 142a87a..66bb1c9 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 +}