ifeq ("$(origin V)", "command line")
BUILD_VERBOSE=$(V)
endif
ifndef BUILD_VERBOSE
$(info Use make V=1, make V=2 or set BUILD_VERBOSE similarly in your environment to increase build verbosity.)
BUILD_VERBOSE = 0
endif
ifeq ($(BUILD_VERBOSE),0)
Q = @
STEPECHO = @:
else ifeq ($(BUILD_VERBOSE),1)
Q = @
STEPECHO = @echo
else
Q =
STEPECHO = @echo
endif

UV_PYTHON = 3.15
UV_RUN_PYTHON = env UV_PYTHON=$(UV_PYTHON) VIRTUAL_ENV=.venv_$(UV_PYTHON) uv run --active --with-requirements requirements-dev.txt python -X dev
PYTHON ?= python3
ifeq ($(OS),Windows_NT)
ENVPYTHON ?= _env/Scripts/python.exe
else
ENVPYTHON ?= _env/bin/python3
endif

.PHONY: default all
default: coverage mypy
all: default pyright pyrefly

.PHONY: coverage
coverage:
	$(Q)$(UV_RUN_PYTHON) -mcoverage erase
	$(Q)env PYTHONPATH=src $(UV_RUN_PYTHON) -mcoverage run -m unittest discover -s test
	$(Q)$(UV_RUN_PYTHON) -mcoverage html
	$(Q)$(UV_RUN_PYTHON) -mcoverage json
	$(Q)$(UV_RUN_PYTHON) -mcoverage report --fail-under=100

.PHONY: testall
testall: test-3.11 test-3.15

.PHONY: test-%
test-%:
	$(Q)$(MAKE) UV_PYTHON=$* test

.PHONY: test
test:
	$(Q)env PYTHONPATH=src $(UV_RUN_PYTHON) -m unittest discover -s test

.PHONY: test_venv
test_venv:
	$(Q)$(PYTHON) -mvenv --clear _env
	$(Q)$(ENVPYTHON) -mpip install . -r requirements-dev.txt
	$(Q)$(ENVPYTHON) -m unittest discover -s test

.PHONY: test_micropython
test_micropython:
	$(Q)docker run -it --rm -w /work -v .:/work docker.io/micropython/unix:v1.29.0 run_tests_micropython.py

.PHONY: mypy
mypy:
	$(Q)uvx --python $(UV_PYTHON) --with-requirements requirements-typing.txt mypy --strict src test

.PHONY: pyright
pyright:
	$(Q)uvx --python $(UV_PYTHON) --with-requirements requirements-typing.txt pyright src test

.PHONY: pyrefly
pyrefly:
	$(Q)uvx --python $(UV_PYTHON) --with-requirements requirements-typing.txt pyrefly check src test

.PHONY: ty
ty:
	$(Q)uvx --python $(UV_PYTHON) --with-requirements requirements-typing.txt ty check src test

.PHONY: prek
prek:
	$(Q)uvx --python $(UV_PYTHON) prek --all-files

.PHONY: ci
ci: ty coverage prek testall html

.PHONY: update
update:
	$(Q)env PYTHONPATH=src $(UV_RUN_PYTHON) -mwwvb.updateiers --dist

.PHONY: html
html:
	$(Q)PYTHONPATH=src uvx --with . --with-requirements requirements-doc.txt properdocs build

.PHONY: serve
serve:
	$(Q)PYTHONPATH=src uvx --with . --with-requirements requirements-doc.txt properdocs serve


# SPDX-FileCopyrightText: 2024 Jeff Epler
#
# SPDX-License-Identifier: GPL-3.0-only
