set windows-shell := ["pwsh.exe", "-NoLogo", "-NoProfile", "-Command"]

alias i := install
alias b := bump-python-dependencies

WHEEL_DIR := "wheels/"

[unix]
_activate_venv := "source .venv/bin/activate"

[windows]
_activate_venv := '.\.venv\Scripts\Activate.ps1'

[private]
@default:
    just --list

[doc("Build Python wheel with mimalloc")]
[script("pwsh.exe", "-NoLogo", "-NoProfile", "-Command")]
[windows]
[arg("mimalloc", long, short="m", value="true")]  
install mimalloc="false":
    $ErrorActionPreference = "Stop"

    if (Test-Path {{ WHEEL_DIR }}) {
        Remove-Item {{ WHEEL_DIR }} -Recurse -Force
    }

    {{ _activate_venv }}

    maturin build --out {{ WHEEL_DIR }} --release {{ if mimalloc == "true" { "--features mimalloc" } else { "" } }}

    if (Get-Command uv -ErrorAction SilentlyContinue) {
        Write-Host "uv found, using uv"
        uv pip install crc32c-rs --no-index --find-links {{ WHEEL_DIR }} --force-reinstall
    } else {
        Write-Host "uv not found, using pip"
        pip install crc32c-rs --no-index --find-links {{ WHEEL_DIR }} --force-reinstall
    }

[doc("Bump Python dependencies")]
[script("pwsh.exe", "-NoLogo", "-NoProfile", "-Command")]
[windows]
bump-python-dependencies:
    $ErrorActionPreference = "Stop"

    $branch = git branch --show-current

    if (-not $branch.StartsWith("bump")) {
        $n = 1

        while ($true) {
            $newBranch = "bump-$n"
            git show-ref --verify --quiet "refs/heads/$newBranch"
            if ($LASTEXITCODE -ne 0) {
                break
            }
            $n++
        }

        git switch -c $newBranch
        Write-Host "Switched to $newBranch"
    }

    uv run scripts/bump_python_deps.py
    git add pyproject.toml

    git diff --cached --quiet
    if ($LASTEXITCODE -eq 0) {
        Write-Host "skipping commit"
    } else {
        git commit -m "bump Python dependencies"
    }
