# Run `make check` before you push. It runs what CI runs, so a green check
# here means a green build there.
#
# uv manages the environment, so none of this needs a venv activated.

.DEFAULT_GOAL := help
.PHONY: help check lint fmt test readme matrix build wheel clean

PY ?= python3.12
VENV := .wheelcheck

help:  ## show this
	@grep -hE '^[a-z-]+:.*?##' $(MAKEFILE_LIST) | sed 's/:.*##/\t/' | expand -t22

check: lint test build wheel  ## everything CI runs, before you push

lint:  ## the linter and the formatter, as CI checks them
	uvx ruff check .
	uvx ruff format --check .

fmt:  ## apply the formatter and the safe fixes
	uvx ruff check . --fix
	uvx ruff format .

test:  ## the whole suite on the default interpreter
	uv run --group dev pytest -q

readme:  ## just the README, about five seconds
	uv run --group dev pytest tests/test_readme.py -q

matrix:  ## the suite on every version requires-python claims
	@for v in 3.11 3.12 3.13; do \
		printf 'python %s: ' $$v; \
		UV_PROJECT_ENVIRONMENT=.venv-matrix-$$v uv run --python $$v --group dev pytest -q | tail -1; \
	done

build:  ## build the artifacts and check the metadata is publishable
	rm -rf dist
	uv build
	uvx twine check dist/*

# The suite imports from src/, so nothing above can catch a file the wheel
# leaves out. This installs the artifact and uses it, as CI does.
wheel: build  ## prove the built wheel works, not just the working tree
	rm -rf $(VENV)
	uv venv --python $(PY) $(VENV)
	uv pip install --python $(VENV) dist/*.whl
	@$(VENV)/Scripts/python -c "\
	from importlib.metadata import distribution, metadata;\
	files = [str(f) for f in distribution('dagflows').files];\
	assert 'dagflows/py.typed' in files, 'py.typed missing, annotations are ignored without it';\
	assert any('licenses/LICENSE' in f for f in files), 'LICENSE missing from the wheel';\
	assert len(metadata('dagflows').get_payload() or ''), 'the README is not in the metadata';\
	print('wheel metadata ok')"
	@test ! -e $(VENV)/Scripts/dagflows.exe || \
		(echo "a dagflows console script was installed, which collides with the CLI"; exit 1)
	@$(VENV)/Scripts/python -m dagflows --help > /dev/null && echo "cli ok"

clean:  ## remove build and test leftovers
	rm -rf dist $(VENV) .venv-matrix-* .pytest_cache
