# Development and release tasks for vcbot.
#
# `uv` is only needed to create the environment and to build; once the project
# is installed the console script and the tests run from .venv directly.

PYTHON   ?= .venv/bin/python
UV       ?= uv
PYVER    ?= 3.13

.DEFAULT_GOAL := help
.PHONY: help venv install install-cuda test lint format build legacy check clean

help: ## Show the available targets
	@grep -hE '^[a-z-]+:.*## ' $(MAKEFILE_LIST) | sed 's/:.*## /\t/'

venv: ## Create .venv if it does not exist yet
	@test -x $(PYTHON) || $(UV) venv .venv --python $(PYVER)

install: venv ## Editable install with build and lint tools
	$(UV) pip install --python $(PYTHON) -e ".[dev]"

install-cuda: venv ## Editable install including the CUDA wheel set
	$(UV) pip install --python $(PYTHON) -e ".[dev,cuda]"

test: install ## Run the test suite
	$(PYTHON) -m unittest vcbot.test_transcription

lint: install ## Check formatting and lint rules
	$(PYTHON) -m ruff check vcbot setup.py

format: install ## Apply the lint fixes that are safe
	$(PYTHON) -m ruff check --fix vcbot setup.py

build: clean ## Build the sdist and wheel into dist/
	$(UV) build

legacy: install ## Verify setup.py reports the same name and version as pyproject.toml
	@test "$$($(PYTHON) setup.py --version 2>/dev/null)" = "$$($(PYTHON) -c 'import vcbot; print(vcbot.__version__)')" \
		&& echo "setup.py version: $$($(PYTHON) setup.py --version 2>/dev/null)"
	@test "$$($(PYTHON) setup.py --name 2>/dev/null)" = "vcbot" \
		&& echo "setup.py name: $$($(PYTHON) setup.py --name 2>/dev/null)"

check: build legacy ## Install the wheel in a scratch environment and run the entry point
	$(UV) venv .build-venv --python $(PYVER)
	$(UV) pip install --python .build-venv/bin/python --no-deps dist/*.whl
	.build-venv/bin/vcbot --version
	.build-venv/bin/vcbot --help > /dev/null
	rm -rf .build-venv

clean: ## Remove build artefacts and caches
	rm -rf dist build .ruff_cache .build-venv *.egg-info
	find . -path ./.venv -prune -o -name __pycache__ -type d -prune -exec rm -rf {} +
