#!/usr/bin/env python3
"""Thin wrapper to run this project's invoke tasks without manual PATH setup.

`uv` (and the project's pinned Python) usually live in Homebrew / ~/.local/bin,
which a bare shell may not have on PATH — so every invoke call otherwise needs an
`export PATH=...` prefix. This script prepends those dirs (only if they exist and
aren't already present) and execs the canonical runner.

    ./inv rust-gate
    ./inv validate-one <nodeid> --server rust

is equivalent to:

    uv run --no-sync python -m invoke rust-gate
"""

from __future__ import annotations

import os
import sys

_EXTRA_PATH = ["/opt/homebrew/bin", os.path.expanduser("~/.local/bin")]


def main() -> None:
    current = os.environ.get("PATH", "")
    parts = current.split(os.pathsep)
    prefix = [d for d in _EXTRA_PATH if os.path.isdir(d) and d not in parts]
    if prefix:
        os.environ["PATH"] = os.pathsep.join(prefix + parts)
    # --no-sync: don't trigger a multi-minute CMake/WiredTiger rebuild just to
    #   run a task.
    # --with invoke: layer invoke into the run env so this works even in a fresh
    #   git worktree whose `.venv` was never synced (it has no `invoke` yet, and
    #   installing one would kick off the WiredTiger build). Pure-Python tasks
    #   (lint / fmt) then run with zero project build; tasks that genuinely need
    #   the compiled `secantus` (test / rust / validate) still require a built
    #   `.venv` and fail with a clear ImportError if run before one exists.
    # exec so signals (Ctrl-C) reach uv/invoke directly.
    os.execvp(
        "uv",
        ["uv", "run", "--no-sync", "--with", "invoke", "python", "-m", "invoke", *sys.argv[1:]],
    )


if __name__ == "__main__":
    main()
