forked from asdf-community/asdf-poetry
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve virtual-environment creation
- use `poetry run true` dummy command to create the virtual-env if it doesn't exist (this is necessary for `MISE_POETRY_AUTO_INSTALL` to work) - show ANSI colouring when env is created/populated - general refactoring
- Loading branch information
Showing
5 changed files
with
67 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,60 @@ | ||
#!/usr/bin/env bash | ||
#! /usr/bin/env bash | ||
|
||
set -euo pipefail | ||
|
||
pyproject=${MISE_TOOL_OPTS__PYPROJECT-} | ||
if [[ -n ${MISE_PROJECT_ROOT-} && ${pyproject} != /* ]]; then | ||
pyproject="${MISE_PROJECT_ROOT}/${pyproject}" | ||
fi | ||
|
||
auto_install=${MISE_POETRY_AUTO_INSTALL:-false} | ||
venv_auto=${MISE_POETRY_VENV_AUTO:-false} | ||
|
||
# shellcheck source-path=. | ||
source "$MISE_PLUGIN_PATH/bin/utils.sh" | ||
source "${MISE_PLUGIN_PATH}/bin/utils.sh" | ||
|
||
setup_virtualenv() { | ||
# if `uv` is being used, exit early and let the user manage venv activation | ||
if [[ -f "uv.lock" ]]; then | ||
if [[ -z ${pyproject} ]]; then | ||
# tool is installed (maybe globally) but project is not activated | ||
return | ||
elif [[ ! -f ${pyproject} ]]; then | ||
echoerr "Project file not found (${pyproject}). Execute \`poetry init\` to create it." | ||
return 1 | ||
fi | ||
|
||
set +e | ||
venv="$(poetry_venv)" | ||
poetry_venv_exit_code=$? | ||
set -e | ||
local project_root=${pyproject%/*} | ||
|
||
if [[ $poetry_venv_exit_code -eq 1 ]]; then | ||
# if `uv` is being used, exit early and let the user manage venv activation | ||
if [[ -f "${project_root}/uv.lock" ]]; then | ||
return | ||
fi | ||
|
||
if [[ ! -d "$venv" ]]; then | ||
if [[ "${MISE_POETRY_AUTO_INSTALL:-}" != "true" ]] && [[ "${MISE_POETRY_AUTO_INSTALL:-}" != "1" ]]; then | ||
# don't activate environment if lock file isn't present | ||
if [[ ${venv_auto} == "true" || ${venv_auto} == "1" ]]; then | ||
if [[ ! -f "${project_root}/poetry.lock" ]]; then | ||
return | ||
fi | ||
poetry_bin install | ||
venv="$(poetry_bin env info --path)" | ||
fi | ||
|
||
POETRY_ACTIVE=1 | ||
MISE_ADD_PATH="$venv/bin" | ||
export VIRTUAL_ENV="$venv" POETRY_ACTIVE MISE_ADD_PATH | ||
local virtual_env | ||
if ! virtual_env=$(poetry_venv "${project_root}"); then | ||
# run a dummy command to ensure the venv exists (creating it if possible) | ||
poetry_bin "${project_root}" --ansi run true | ||
|
||
if ! virtual_env=$(poetry_venv "${project_root}"); then | ||
echoerr "Unable to create/activate virtual environment." | ||
return | ||
fi | ||
|
||
if [[ ${auto_install} == "true" || ${auto_install} == "1" ]]; then | ||
echoerr "Executing \`poetry install\` to populate virtual environment." | ||
poetry_bin "${project_root}" --ansi install | ||
fi | ||
fi | ||
|
||
export POETRY_ACTIVE=1 | ||
export VIRTUAL_ENV=${virtual_env} | ||
export MISE_ADD_PATH="${virtual_env}/bin" | ||
} | ||
|
||
setup_virtualenv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,30 @@ | ||
#!/usr/bin/env bash | ||
#! /usr/bin/env bash | ||
|
||
if [ "${MISE_TRACE-}" = "1" ]; then | ||
if [[ ${MISE_TRACE-} == 1 ]]; then | ||
set -x | ||
fi | ||
|
||
echoerr() { | ||
echo "$1" >&2 | ||
printf 'mise-poetry: %s\n' "$@" >&2 | ||
} | ||
|
||
poetry_bin() { | ||
"$MISE_INSTALL_PATH/bin/poetry" "$@" | ||
local project_root=$1 | ||
shift | ||
|
||
"${MISE_INSTALL_PATH}/bin/poetry" -C "${project_root}" --no-interaction "$@" | ||
} | ||
|
||
poetry_venv() { | ||
local pyproject | ||
pyproject="$(eval "echo ${MISE_TOOL_OPTS__PYPROJECT-}")" | ||
if [[ "${MISE_POETRY_VENV_AUTO:-}" != "true" ]] && [[ "${MISE_POETRY_VENV_AUTO:-}" != "1" ]]; then | ||
if [ "$pyproject" = "" ]; then | ||
return 1 | ||
fi | ||
elif [[ ! -f "poetry.lock" ]] | [[ ! -f "pyproject.toml" ]]; then | ||
return 1 | ||
else | ||
pyproject="pyproject.toml" | ||
fi | ||
if [[ $pyproject != /* ]] && [[ -n ${MISE_PROJECT_ROOT-} ]]; then | ||
pyproject="${MISE_PROJECT_ROOT-}/$pyproject" | ||
fi | ||
if [[ ! -f $pyproject ]]; then | ||
echoerr "mise-poetry: No pyproject.toml found. Execute \`poetry init\` to create \`$pyproject\` first." | ||
local project_root=$1 | ||
shift | ||
|
||
local virtual_env | ||
virtual_env=$(poetry_bin "${project_root}" env info --path 2> /dev/null) | ||
|
||
if ! [[ -n ${virtual_env} && -d "${virtual_env}" ]]; then | ||
return 1 | ||
fi | ||
poetry_bin -C "${pyproject%/*}" env info --path 2>/dev/null | ||
true | ||
|
||
printf '%s' "${virtual_env}" | ||
} |