UV = uv
RUN = $(UV) run

PROTO_DIR = proto
PB_DIR = src/rune_sdk/_pb

DIST_DIR = dist
TESTPYPI_URL = https://test.pypi.org/legacy/

.PHONY: default generate lint test format clean build publish-test publish check-clean

default: lint test

generate:
	@ ./scripts/generate.sh

lint:
	@ $(RUN) ruff check src tests examples
	@ $(RUN) ruff format --check src tests examples
	@ $(RUN) mypy

test:
	@ $(RUN) pytest

format:
	@ $(RUN) ruff format src tests examples
	@ $(RUN) ruff check --fix src tests examples

clean:
	@ rm -rf .mypy_cache .ruff_cache .pytest_cache dist

# hatch-vcs derives the version from the git tag; a dirty tree yields a
# .devN+g<sha> local version that PyPI rejects, so refuse to publish it.
check-clean:
	@ test -z "$$(git status --porcelain)" || \
		{ echo "error: working tree is dirty; commit or stash before publishing"; exit 1; }

build: check-clean clean
	@ $(UV) build
	@ $(UV) run --with twine twine check --strict $(DIST_DIR)/*

# Publish to TestPyPI. Requires TESTPYPI_TOKEN in the environment.
publish-test: build
	@ test -n "$$TESTPYPI_TOKEN" || \
		{ echo "error: set TESTPYPI_TOKEN to a TestPyPI API token"; exit 1; }
	@ UV_PUBLISH_TOKEN=$$TESTPYPI_TOKEN $(UV) publish --publish-url $(TESTPYPI_URL) $(DIST_DIR)/*

# Publish to PyPI. Requires PYPI_TOKEN in the environment.
publish: build
	@ test -n "$$PYPI_TOKEN" || \
		{ echo "error: set PYPI_TOKEN to a PyPI API token"; exit 1; }
	@ UV_PUBLISH_TOKEN=$$PYPI_TOKEN $(UV) publish $(DIST_DIR)/*