Skip to content

Commit

Permalink
ENH: support pixi and venv in .envrc (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer authored Aug 9, 2024
1 parent 064446b commit 84e5d7f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"*.ico",
"*.rst_t",
".editorconfig",
".envrc",
".gitignore",
".gitpod.*",
".pre-commit-config.yaml",
Expand Down
8 changes: 7 additions & 1 deletion .envrc
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
layout anaconda
if [ -e .venv ]; then
source .venv/bin/activate
elif [ -e venv ]; then
source venv/bin/activate
else
layout anaconda
fi
1 change: 1 addition & 0 deletions src/compwa_policy/.template/.cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"*particle*.*ml",
".constraints/*.txt",
".editorconfig",
".envrc",
".gitignore",
".gitpod.*",
".mypy.ini",
Expand Down
43 changes: 32 additions & 11 deletions src/compwa_policy/check_dev_files/direnv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,50 @@

from __future__ import annotations

from textwrap import dedent
from textwrap import dedent, indent

from compwa_policy.errors import PrecommitError
from compwa_policy.utilities import CONFIG_PATH
from compwa_policy.utilities.pyproject import Pyproject

__SCRIPTS = {
"conda": "layout anaconda",
"pixi": """
watch_file pixi.lock
eval "$(pixi shell-hook)"
""",
"venv": "source venv/bin/activate",
"uv-venv": "source .venv/bin/activate",
}


def main() -> None:
statements: list[tuple[str | None, str]] = [
(".venv", __SCRIPTS["uv-venv"]),
("venv", __SCRIPTS["venv"]),
]
if (
CONFIG_PATH.pixi_lock.exists()
or CONFIG_PATH.pixi_toml.exists()
or (CONFIG_PATH.pyproject.exists() and Pyproject.load().has_table("tool.pixi"))
):
_update_envrc("""
watch_file pixi.lock
eval "$(pixi shell-hook)"
""")
elif CONFIG_PATH.conda.exists():
_update_envrc("layout anaconda")


def _update_envrc(expected: str) -> None:
expected = dedent(expected).strip() + "\n"
statements.append((".pixi", __SCRIPTS["pixi"]))
if CONFIG_PATH.conda.exists():
statements.append((None, __SCRIPTS["conda"]))
_update_envrc(statements)


def _update_envrc(statements: list[tuple[str | None, str]]) -> None:
expected = ""
for i, (trigger_path, script) in enumerate(statements):
if trigger_path is not None:
if_or_elif = "if" if i == 0 else "elif"
expected += f"{if_or_elif} [ -e {trigger_path} ]; then\n"
else:
expected += "else\n"
script = dedent(script).strip()
expected += indent(script, prefix=" ") + "\n"
expected += "fi\n"
existing = __get_existing_envrc()
if existing == expected:
return
Expand Down

0 comments on commit 84e5d7f

Please sign in to comment.