diff --git a/.trunk/configure-pyright-with-pyenv.sh b/.trunk/configure-pyright-with-pyenv.sh deleted file mode 100755 index 88936607..00000000 --- a/.trunk/configure-pyright-with-pyenv.sh +++ /dev/null @@ -1,34 +0,0 @@ -#! /usr/bin/env bash -set -e - -# Get the name of the expected venv for this repo from the pyproject.toml file. -venv_name=$(grep -m 1 venv pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3) || true - -# Check if pyrightconfig already exists. -if [[ ! -f pyrightconfig.json ]]; then - # Check if pyenv-pyright plugin is installed - if ! command -v pyenv &>/dev/null; then - echo "pyenv not installed. Please install pyenv..." - exit 1 - fi - - pyenv_root=$(pyenv root) - dir_path="${pyenv_root}"/plugins/pyenv-pyright - if [[ ! -d ${dir_path} ]]; then - # trunk-ignore(shellcheck/SC2312) - if [[ -n $(ls -A "${dir_path}") ]]; then - git clone https://github.com/alefpereira/pyenv-pyright.git "${dir_path}" - fi - fi - - # Generate the pyrightconfig.json file. - pyenv pyright "${venv_name}" - pyenv local "${venv_name}" -fi - -# Check whether required keys are present in pyrightconfig.json. -if ! jq -r --arg venv_name "${venv_name}" '. | select(.venv != $venv_name) | select(.venvPath != null)' pyrightconfig.json; then - echo "Failed to configure pyright to use pyenv environment '${venv_name}' as interpreter. Please check pyrightconfig.json..." - exit 1 -fi -exit 0 diff --git a/.trunk/configure-pyright.sh b/.trunk/configure-pyright.sh new file mode 100755 index 00000000..d8f808ee --- /dev/null +++ b/.trunk/configure-pyright.sh @@ -0,0 +1,51 @@ +#! /usr/bin/env bash +set -e + +# Get the name of the expected venv for this repo from the pyproject.toml file. +venv_name=$(grep -m 1 venv pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3) || true + +# Check if pyrightconfig already exists. +if [[ ! -f pyrightconfig.json ]]; then + + # Capture poetry environment path and check if it's empty. + poetry_env_info=$(poetry env info --path 2>/dev/null || true) + if [[ -z ${poetry_env_info} ]]; then + # Check if pyenv is installed + if ! command -v pyenv &>/dev/null; then + echo "pyenv not installed. Please install pyenv..." + exit 1 + fi + + pyenv_root=$(pyenv root) + dir_path="${pyenv_root}"/plugins/pyenv-pyright + if [[ ! -d ${dir_path} ]]; then + # trunk-ignore(shellcheck/SC2312) + if [[ -z $(ls -A "${dir_path}") ]]; then + git clone https://github.com/alefpereira/pyenv-pyright.git "${dir_path}" + fi + fi + + # Generate the pyrightconfig.json file. + pyenv pyright "${venv_name}" + pyenv local "${venv_name}" + else + poetry_env_info=$(poetry env info --path 2>/dev/null || true) + venv_path=$(dirname "${poetry_env_info}") # Get directory path + venv_name=$(basename "${poetry_env_info}") # Get base name + + # Generate the pyrightconfig.json file. + json_string=$(jq -n \ + --arg v "${venv_name}" \ + --arg vp "${venv_path}" \ + '{venv: $v, venvPath: $vp} ') + + echo "${json_string}" >pyrightconfig.json + fi +fi + +# Check whether required keys are present in pyrightconfig.json. +if ! jq -r --arg venv_name "${venv_name}" '. | select((.venv != $venv_name or .venv == "") and (.venvPath == null or .venvPath == ""))' pyrightconfig.json >/dev/null 2>&1; then + echo "Failed to configure pyright to use environment '${venv_name}' as interpreter. Please check pyrightconfig.json..." + exit 1 +fi +exit 0 diff --git a/.trunk/trunk.yaml b/.trunk/trunk.yaml index 4ea4e0b2..6d746ff9 100644 --- a/.trunk/trunk.yaml +++ b/.trunk/trunk.yaml @@ -81,13 +81,13 @@ actions: disabled: - trunk-check-pre-push enabled: - - configure-pyright-with-pyenv + - configure-pyright - trunk-check-pre-commit - trunk-announce - trunk-fmt-pre-commit - trunk-upgrade-available definitions: - - id: configure-pyright-with-pyenv - run: source .trunk/configure-pyright-with-pyenv.sh + - id: configure-pyright + run: source .trunk/configure-pyright.sh triggers: - git_hooks: [pre-commit] diff --git a/makefile-local.defs b/makefile-local.defs index 638c69ba..afa69c68 100644 --- a/makefile-local.defs +++ b/makefile-local.defs @@ -1,42 +1,42 @@ -# definitions for local development +# Definitions for local development -poetry_environment: create_venv - poetry install - -check_dev_environment: -ifneq (,$(wildcard ./.env)) - echo "Dev environment already configured." - exit 1 +install_trunk: + $(eval trunk_installed=$(shell trunk --version > /dev/null 2>&1 ; echo $$? )) +ifneq (${trunk_installed},0) + $(eval OS_NAME=$(shell uname -s | tr A-Z a-z)) + curl https://get.trunk.io -fsSL | bash endif -configure_pyright: - trunk actions run configure-pyright-with-pyenv +uninstall_trunk: + sudo rm -if `which trunk` + rm -ifr ${HOME}/.cache/trunk -dev_install: install_trunk check_dev_environment - # Sets up a local dev environment +configure_pyright: + trunk actions run configure-pyright +create_env: # Copy .env cp .env.example .env + +setup: install_trunk create_env ## Sets up a local dev environment using Poetry # Install pip pip install --upgrade pip - # Install poetry + + # Install poetry & dependencies pip install "poetry==1.8.1" + poetry install - make poetry_environment make configure_pyright -create_venv: - -pyenv deactivate - pyenv virtualenv 3.10 backend - pyenv activate backend - -install_trunk: - $(eval trunk_installed=$(shell trunk --version > /dev/null 2>&1 ; echo $$? )) -ifneq (${trunk_installed},0) - $(eval OS_NAME=$(shell uname -s | tr A-Z a-z)) - curl https://get.trunk.io -fsSL | bash -endif +setup_with_pyenv: install_trunk create_env ## Sets up a local dev environment using Pyenv + $(eval venv_name=$(shell grep 'venv =' pyproject.toml | cut -d '"' -f 2 )) + if [ -n "$(venv_name)" ] && ! pyenv versions --bare | grep -q "^$(venv_name)$$"; then \ + $(eval python_version=$(shell grep 'python =' pyproject.toml | cut -d '"' -f 2 | sed 's/^\^//')) \ + $(eval pyenv_version=$(shell pyenv versions --bare | grep$(python_version) )) \ + pyenv virtualenv $(pyenv_version) $(venv_name); \ + fi + @eval "$$(pyenv init -)" && \ + pyenv activate $(venv_name) && \ + poetry install -uninstall_trunk: - sudo rm -if `which trunk` - rm -ifr ${HOME}/.cache/trunk + make configure_pyright