Skip to content

Commit

Permalink
Fix bootstrapping & separate by environment (#419)
Browse files Browse the repository at this point in the history
* Fix bootstrap for pyenv

* Update trunk pyright config action

* Get python version from pyproject file
  • Loading branch information
katybaulch authored Nov 13, 2024
1 parent f1b782c commit 5b1a84b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 65 deletions.
34 changes: 0 additions & 34 deletions .trunk/configure-pyright-with-pyenv.sh

This file was deleted.

51 changes: 51 additions & 0 deletions .trunk/configure-pyright.sh
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
56 changes: 28 additions & 28 deletions makefile-local.defs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5b1a84b

Please sign in to comment.