Skip to content

Commit

Permalink
Refresh tasks.py to work with uv
Browse files Browse the repository at this point in the history
- drop poetry from the tools
- special-case installing uv
- replace poetry instructions with uv instructions
  • Loading branch information
offbyone committed Nov 2, 2024
1 parent aa16c38 commit 5151b43
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
59 changes: 39 additions & 20 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,21 @@

PKG_NAME = "webassets"
PKG_PATH = Path(f"pelican/plugins/{PKG_NAME}")
TOOLS = ("poetry", "pre-commit")
TOOLS = ["pre-commit"]

ACTIVE_VENV = os.environ.get("VIRTUAL_ENV", None)
VENV_HOME = Path(os.environ.get("WORKON_HOME", "~/.local/share/virtualenvs"))
VENV_PATH = Path(ACTIVE_VENV) if ACTIVE_VENV else (VENV_HOME.expanduser() / PKG_NAME)
VENV = str(VENV_PATH.expanduser())
BIN_DIR = "bin" if os.name != "nt" else "Scripts"
VENV_BIN = Path(VENV) / Path(BIN_DIR)
POETRY = which("poetry") if which("poetry") else (VENV_BIN / "poetry")
CMD_PREFIX = f"{VENV_BIN}/" if ACTIVE_VENV else f"{POETRY} run "
PRECOMMIT = which("pre-commit") if which("pre-commit") else f"{CMD_PREFIX}pre-commit"
PTY = True if os.name != "nt" else False


@task
def tests(c):
"""Run the test suite."""
c.run(f"{CMD_PREFIX}pytest", pty=PTY)
c.run("uv run pytest", pty=PTY)


@task
Expand All @@ -36,7 +33,7 @@ def ruff(c, fix=False, diff=False):
fix_flag = "--fix"
if diff:
diff_flag = "--diff"
c.run(f"{CMD_PREFIX}/ruff check {diff_flag} {fix_flag} .", pty=PTY)
c.run(f"uv run ruff check {diff_flag} {fix_flag} .", pty=PTY)


@task
Expand All @@ -46,38 +43,60 @@ def lint(c, fix=False, diff=False):


@task
def uv(c):
"""Install uv in the local virtual environment."""
if not which("uv"):
print("** Installing uv in the project virual environment.")
c.run(f"{VENV_BIN}/python -m pip install uv", pty=PTY)


@task(pre=[uv])
def tools(c):
"""Install development tools in the virtual environment if not already on PATH."""
for tool in TOOLS:
if not which(tool):
print(f"** Installing {tool}.")
c.run(f"{CMD_PREFIX}pip install {tool}")
c.run(f"uv pip install {tool}")


@task
@task(pre=[tools])
def precommit(c):
"""Install pre-commit hooks to `.git/hooks/pre-commit`."""
print("** Installing pre-commit hooks.")
c.run(f"{PRECOMMIT} install")
pre_commit_cmd = (
which("pre-commit") if which("pre-commit") else f"{VENV_BIN}pre-commit"
)
c.run(f"{pre_commit_cmd} install")


@task
def setup(c):
"""Set up the development environment."""
if which("poetry") or ACTIVE_VENV:
tools(c)
c.run(f"{CMD_PREFIX}python -m pip install --upgrade pip")
c.run(f"{POETRY} install")
precommit(c)
print("\nDevelopment environment should now be set up and ready!\n")
else:
"""Set up the development environment. You must have `uv` installed."""
if not which("uv"):
error_message = """
Poetry is not installed, and there is no active virtual environment available.
uv is not installed, and there is no active virtual environment available.
You can either manually create and activate a virtual environment, or you can
install Poetry via:
install uv by running the following command:
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
curl -LsSf https://astral.sh/uv/install.sh | sh
Once you have taken one of the above two steps, run `invoke setup` again.
""" # noqa: E501
sys.exit(cleandoc(error_message))

global ACTIVE_VENV
if not ACTIVE_VENV:
print("** Creating a virtual environment.")
c.run("uv venv")
ACTIVE_VENV = ".venv"

tools(c)
c.run("uv sync")
precommit(c)
success_message = """
Development environment should now be set up and ready.
To enable running invoke, either run it with `uv run inv` or
activate the virtual environment with `source .venv/bin/activate`
"""
print(cleandoc(success_message))
8 changes: 4 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5151b43

Please sign in to comment.