set shell := ["bash", "-uc"]
set tempdir := "/tmp"

publish_dir := "/tmp/easy_font_dist_publish"
manylinux := "manylinux_2_34"

default:
    just --list

fmt:
    cargo fmt

fmt-check:
    cargo fmt --check

test-rust:
    cargo test

test-python:
    .venv/bin/python -m pytest -q

test: test-rust test-python

develop:
    maturin develop

bump-release-version:
    #!/usr/bin/env python3
    from pathlib import Path
    import re

    files = [Path("Cargo.toml"), Path("pyproject.toml")]
    version_re = re.compile(r'(?m)^(version\s*=\s*")(\d+)\.(\d+)\.(\d+)(")')
    versions = []

    for path in files:
        text = path.read_text()
        match = version_re.search(text)
        if not match:
            raise SystemExit(f"missing x.y.z version in {path}")
        versions.append(tuple(int(part) for part in match.groups()[1:4]))

    if len(set(versions)) != 1:
        formatted = ", ".join(f"{path}={version}" for path, version in zip(files, versions))
        raise SystemExit(f"version mismatch: {formatted}")

    major, minor, patch = versions[0]
    next_version = f"{major}.{minor}.{patch + 1}"

    for path in files:
        text = path.read_text()
        updated = version_re.sub(rf"\g<1>{next_version}\g<5>", text, count=1)
        path.write_text(updated)

    print(next_version)

clean-publish:
    rm -rf {{ publish_dir }}
    mkdir -p {{ publish_dir }}

build: clean-publish
    env -u _PYTHON_HOST_PLATFORM maturin build --release --compatibility {{ manylinux }} --out {{ publish_dir }}
    maturin sdist --out {{ publish_dir }}
    ls -lh {{ publish_dir }}

check: fmt-check test

publish: bump-release-version check build
    sh -c 'MATURIN_PYPI_TOKEN="$(tr -d "\n" < pypi_token)" maturin upload --non-interactive {{ publish_dir }}/*'

publish-built:
    sh -c 'MATURIN_PYPI_TOKEN="$(tr -d "\n" < pypi_token)" maturin upload --non-interactive {{ publish_dir }}/*'
