# Available recipes: `just --list`. Everything here is a thin wrapper around
# `uv`/`uvx`.
set positional-arguments := true

default:
    @just --list

# (quality) Lint (fails on issues, doesn't fix). Use `fix` to auto-fix.
lint:
    uv run ruff check .
    uv run ruff format --check .

# (quality) Auto-fix lint issues and reformat.
fix:
    uv run ruff check --fix .
    uv run ruff format .

# (annotations) Check static typing.
typecheck:
    uv run ty check

# (test) Run tests. Extra args are forwarded to pytest.
test *args:
    uv run pytest "$@"

# (test) Run tests and show a coverage report (htmlcov/index.html + terminal).
coverage:
    uv run pytest --cov=ioimage --cov-report=html --cov-report=term

# (quality) Run all pre-commit hooks against every file.
precommit:
    uvx pre-commit run --all-files

# (installation) Install pre-commit as a git hook.
precommit-install:
    uvx pre-commit install

# (build) Check that the future sdist/wheel will contain all the files you want.
check-manifest:
    uvx check-manifest -c -v

# (profiling) Run scalene on a script, e.g. `just profile myscript.py`.
profile *args:
    uv run --with scalene scalene "$@"

# (docs) Build the Sphinx docs into docs/_build.
docs:
    uv run sphinx-build -M html docs docs/_build

# (docs) Build the docs and serve them at http://localhost:8000/ (Ctrl-C to quit).
docs-serve: docs
    uv run python -m http.server 8000 -d docs/_build/html

# (build) Build an SDist and wheel(s) locally, into dist/.
build:
    #!/usr/bin/env bash
    set -euo pipefail
    rm -rf build dist
    # Pure Python: a single universal wheel, no per-interpreter or per-platform rebuild needed.
    uv build -o dist

# (build) Check and upload the built packages with twine (target chosen below; credentials from ~/.pypirc).
publish:
    #!/usr/bin/env bash
    set -euo pipefail
    if [[ ! -d dist ]]; then
        echo "dist/ not found, have you run 'just build' ?"
        exit 1
    fi
    for f in dist/*.tar.gz dist/*.whl; do
        [[ -e "$f" ]] || continue
        uvx twine check "$f" --strict
    done
    # Target: $IOIMAGE_REPOSITORY if set, else "koda" if ~/.pypirc has a [koda] section, else "pypi".
    repository="${IOIMAGE_REPOSITORY:-}"
    if [[ -z "$repository" ]]; then
        if grep -qs '^\[koda\]' ~/.pypirc; then repository=koda; else repository=pypi; fi
    fi
    echo "Uploading to: $repository"
    uvx twine upload --repository "$repository" dist/*

# (clean) Delete cache folders and build artifacts.
clean:
    rm -rf build __pycache__ .pytest_cache .coverage htmlcov .ruff_cache .ty_cache docs/_build docs/_generate
