From 7e2d9ee69daa82fcece5b37aabf77e880b6044e5 Mon Sep 17 00:00:00 2001 From: Alex Kelly Date: Wed, 17 Jul 2024 21:13:31 -0400 Subject: [PATCH] add hatch layout for direnv --- dot_config/direnv/direnvrc | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 dot_config/direnv/direnvrc diff --git a/dot_config/direnv/direnvrc b/dot_config/direnv/direnvrc new file mode 100644 index 0000000..4008540 --- /dev/null +++ b/dot_config/direnv/direnvrc @@ -0,0 +1,29 @@ +layout_hatch() { + if [[ ! -f "pyproject.toml" ]]; then + if [[ ! -f "setup.py" ]]; then + local tmpdir + log_status "No pyproject.toml or setup.py found. Executing \`hatch new\` to create a new project." + PROJECT_NAME=$(basename $PWD) + tmpdir="$(mktemp -d)" + hatch new $PROJECT_NAME $tmpdir > /dev/null + cp -a --no-clobber $tmpdir/* . && rm -rf $tmpdir + else + # I haven't yet seen a case where migrating from an existing `setup.py` works, but I'm sure there are some. + log_status "No pyproject.toml found. Executing \`hatch new --init\` to migrate from setuptools." + hatch new --init || log_error "Failed to migrate from setuptools. Please fix and run \`hatch new --init\` manually." && return 1 + fi + fi + + HATCH_ENV=${HATCH_ENV_ACTIVE:-default} + # We need this to error out if the env doesn't exist in the pyproject.toml file. + VIRTUAL_ENV=$(hatch env find $HATCH_ENV) + + if [[ ! -d $VIRTUAL_ENV ]]; then + log_status "No virtual environment exists. Executing \`hatch env create\` to create one." + hatch env create $HATCH_ENV + fi + + PATH_add "$VIRTUAL_ENV/bin" + export HATCH_ENV_ACTIVE=$HATCH_ENV + export VIRTUAL_ENV +}