# The single entry point for CI and release: standard tools only (uv, ruff, mypy, pyright, pytest, gitleaks, commitizen).
.PHONY: install lint format typecheck typos doclint debt print-cov-floor test secrets audit build check release clean

# The core with mypy's default rules and no disabled error class (vdus: the former debt list is fixed). The
# repository's [tool.mypy] (strict) is bin/check's gate and the debt measure of bin/debt.py.

install:
	uv sync --all-packages --locked

# ruff format is not in the gate yet: most files predate it, and a mass reformat would collide with open branches.
lint:
	uv run ruff check src examples tests

format:  # apply ruff format (not yet enforced; see lint)
	uv run ruff format src examples tests

typecheck:
	uv run mypy --config-file= --python-version 3.15 src/steuermaschine
	uv run pyright

# The promote gate's typos and debt ratchet (bin/check runs these same two targets): a red one cannot merge. The ratchet
# reads the .coverage file of `test`, so `debt` runs after it.
typos:
	uv run --quiet typos src plugins examples tests docs README.md bin

debt:
	uv run --quiet python bin/debt.py --gate

# The handover documents name only real commands (sm self doc-lint, moebg). The heinzel plugin comes from the test
# project file, never from this machine's user config; state goes to a throwaway directory.
DOCS := docs/*.md README.md AGENTS.md CLAUDE.md PYPI.md
doclint:
	STEUERMASCHINE_PROJECT=tests/projects/heinzel STEUERMASCHINE_USER_CONFIG=/dev/null STEUERMASCHINE_HOME="$${TMPDIR:-/tmp}/sm-doclint" \
		uv run --quiet sm self doc-lint --hidden $(DOCS)

# Coverage floor of the gate (line+branch, core+heinzel plugin, as CI measures it today): coverage_ci of
# [tool.steuermaschine.gate] in pyproject.toml, where bin/check's core and plugin floors are too (vdus).
COV_FLOOR := $(shell python3 -c 'import tomllib; print(tomllib.load(open("pyproject.toml", "rb"))["tool"]["steuermaschine"]["gate"]["coverage_ci"])')

print-cov-floor:
	@echo $(COV_FLOOR)

# Typer forces colored help when GITHUB_ACTIONS, FORCE_COLOR or PY_COLORS is set; the help tests read plain text.
test:
	env -u GITHUB_ACTIONS -u FORCE_COLOR -u PY_COLORS uv run pytest --cov --cov-report=term --cov-report=xml --cov-fail-under=$(COV_FLOOR)

secrets:
	gitleaks git --redact --no-banner .
	gitleaks dir --redact --no-banner .

audit:
	uv audit --locked --preview-features audit-command

build:
	rm -rf dist && uv build --package steuermaschine --out-dir dist && uv build --package steuermaschine-heinzel --out-dir dist

check: lint typecheck typos doclint test debt secrets

release:  # bump from Conventional Commits, write CHANGELOG.md, commit, tag v*; then: git push --follow-tags
	uv run cz bump --yes

clean:
	rm -rf dist .coverage coverage.xml .pytest_cache .mypy_cache .ruff_cache
