SRC:=src/tukuroi/ scripts/
PKG:=tukuroi

# What uv installs from uv.lock before it runs a command: every extra and group
# by default. A CI job narrows it to what it needs (#8).
SYNC?=--all-extras --all-groups
RUN=uv run $(SYNC)

all: install test lint html

install:
	uv sync $(SYNC)

venv: install

# uv.lock, and the build backend with hashes for the release build (#18). Both
# keep the versions they have: pass --upgrade to `uv lock` and to
# `uv pip compile` to take newer ones. Renovate runs the command of the header
# of build-constraints.txt (#7): it takes `--option=value` only.
lock:
	uv lock
	uv pip compile --quiet --python-version=3.14 --generate-hashes \
		build-constraints.in --output-file=build-constraints.txt

# The known vulnerabilities of what uv.lock pins, from osv.dev (#25), but those
# that `[tool.uv.audit]` of pyproject.toml leaves out. CI runs it each day.
audit:
	uv audit --preview-features audit-command

test:
	$(RUN) python -m $(PKG).tests
	$(MAKE) test-scripts
	$(MAKE) typecheck

# The tests of the scripts (#157), with scripts/ as the top: they import the
# scripts by name. Not measured: `make coverage` measures the package.
test-scripts:
	$(RUN) python -m unittest discover --start-directory scripts/tests --top-level-directory scripts

# The types of all the code (#179), on the oldest and the newest Python that
# tukuroi supports: pyproject.toml has the files, and the oldest.
typecheck:
	$(RUN) pyrefly check -c pyproject.toml
	$(RUN) pyrefly check -c pyproject.toml --python-version 3.15

# The tests change the current directory, and so do the processes that they
# start: the data goes to one place, by its absolute path (#10).
coverage: export COVERAGE_FILE=$(CURDIR)/.coverage
coverage:
	rm -f .coverage .coverage.*
	$(RUN) coverage run -m $(PKG).tests
	$(RUN) coverage combine -q
	$(RUN) coverage report

lint:
	$(RUN) ruff check $(SRC)
	$(RUN) ruff format --check $(SRC)
	$(RUN) python -m tukuroi lint $(SRC)
	$(RUN) python scripts/check_copyright.py
	$(RUN) zizmor --offline .github/workflows
	$(RUN) lint-imports

# A warning fails the build, after all of them are shown: an inventory of
# intersphinx that cannot be fetched too, so that no page without its links is
# deployed (#155). Run it again when the network failed.
html: docs/*
	$(RUN) python scripts/document_rules.py
	$(RUN) sphinx-build -W --keep-going -ab html docs html

format:
	$(RUN) ruff check --select I --fix $(SRC)
	$(RUN) ruff format $(SRC)

clean:
	rm -rf .mypy_cache build dist html *.egg-info .coverage .coverage.*

distclean: clean
	rm -rf .venv
