# `just` recipes for rebinning — see https://github.com/casey/just.
# Run `just` (no args) to list available recipes.

set shell := ["bash", "-uc"]

# Default: list recipes.
default:
    @just --list

# --- Local development -------------------------------------------------------

# Sync the venv and build the Rust extension in place (release mode).
dev:
    uv sync
    uv run maturin develop --uv --release

# Fast dev iteration: build extension in debug mode (much faster compile).
dev-debug:
    uv run maturin develop --uv

# Run the Python test suite.
test *args:
    uv run pytest {{args}}

# --- Quality checks ----------------------------------------------------------

# Format Rust code.
fmt:
    cargo fmt --all

# Verify Rust formatting (fails if reformat would change anything).
fmt-check:
    cargo fmt --all -- --check

# Lint Rust with clippy. Same flags CI uses.
lint:
    cargo clippy --all-targets -- -D warnings

# Run every check CI runs (formatting + clippy + tests).
check: fmt-check lint test

# --- Build artifacts ---------------------------------------------------------

# Build a release wheel for the current platform into ./dist.
wheel:
    rm -rf dist
    uv run maturin build --release --out dist

# Build a source distribution into ./dist.
sdist:
    rm -rf dist
    uv run maturin sdist --out dist

# Remove build artifacts.
clean:
    rm -rf dist target/wheels
    cargo clean

# --- Release -----------------------------------------------------------------

# Bump version, sign commit + tag; push the tag to trigger PyPI release. Usage: just release 0.2.0
release version:
    #!/usr/bin/env bash
    set -euo pipefail
    echo "Releasing v{{version}}…"
    # Preflight: signing key must be configured.
    if [ -z "$(git config --get user.signingkey)" ]; then
        echo "error: no user.signingkey configured — release commits/tags must be signed." >&2
        echo "       fix: git config user.signingkey <KEYID>" >&2
        exit 1
    fi
    if [ -n "$(git status --porcelain)" ]; then
        echo "error: working tree is dirty — commit or stash first." >&2
        exit 1
    fi
    if [ "$(git rev-parse --abbrev-ref HEAD)" != "main" ]; then
        echo "error: must release from main." >&2
        exit 1
    fi
    if git rev-parse "v{{version}}" >/dev/null 2>&1; then
        echo "error: tag v{{version}} already exists locally." >&2
        exit 1
    fi
    if git ls-remote --tags origin | grep -q "refs/tags/v{{version}}$"; then
        echo "error: tag v{{version}} already exists on origin." >&2
        exit 1
    fi
    # Both files contain a single top-level `version = "..."` line.
    # `sed -i.bak` syntax is portable between GNU sed and BSD sed (macOS).
    sed -i.bak -E 's/^version = ".*"/version = "{{version}}"/' Cargo.toml pyproject.toml
    rm -f Cargo.toml.bak pyproject.toml.bak
    cargo update -p rebinning
    git add Cargo.toml Cargo.lock pyproject.toml
    # If sed produced no change (files already at this version — e.g. the
    # initial release, or resuming a partially-failed previous run), skip
    # the commit step and tag the current HEAD directly.
    bumped=0
    if git diff --cached --quiet; then
        echo "Files already at version {{version}} — no bump needed, tagging current HEAD."
    else
        # -S: sign explicitly so the release doesn't silently degrade if
        # commit.gpgsign gets unset later.
        git commit -S -m "Release v{{version}}"
        bumped=1
    fi
    # -s: signed annotated tag (independent of tag.gpgsign config).
    git tag -s "v{{version}}" -m "v{{version}}"
    echo
    if [ "$bumped" = "1" ]; then
        echo "Created signed bump commit + signed tag v{{version}} locally."
        echo "Verify with:  git log -1 --show-signature && git tag -v v{{version}}"
        echo
        echo "Push to trigger the PyPI release workflow:"
        echo "    git push origin main && git push origin v{{version}}"
    else
        echo "Created signed tag v{{version}} on existing HEAD."
        echo "Verify with:  git tag -v v{{version}}"
        echo
        echo "Push to trigger the PyPI release workflow:"
        echo "    git push origin v{{version}}"
    fi
