Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: support pixi and venv in .envrc #367

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading