.EXPORT_ALL_VARIABLES:
# Get changed files

FILES := $(wildcard **/*.py)

# if you wrap everything in uv run, it runs slower.
ifeq ($(origin VIRTUAL_ENV),undefined)
    VENV := uv run
else
    VENV :=
endif

uv.lock: pyproject.toml
	@echo "Installing dependencies"
	@uv sync

clean-pyc:
	@echo "Removing compiled files"


clean-test:
	@echo "Removing coverage data"
	@rm -f .coverage || true
	@rm -f .coverage.* || true

clean: clean-pyc clean-test

# tests can't be expected to pass if dependencies aren't installed.
# tests are often slow and linting is fast, so run tests on linted code.
test: clean uv.lock install_plugins
	@echo "Running unit tests"
	# $(VENV) pytest --doctest-modules jiggle_version
	# $(VENV) python -m unittest discover
	$(VENV) py.test test -vv -n 2 --cov=jiggle_version --cov-report=html --cov-fail-under 35 --cov-branch --cov-report=xml --junitxml=junit.xml -o junit_family=legacy
	$(VENV) bash scripts/basic_checks.sh
	$(VENV) bash scripts/integration_test.sh



isort:
	@echo "Formatting imports"
	$(VENV) isort .

.PHONY: isort

black: isort
	@echo "Formatting code"
	$(VENV) metametameta pep621
	$(VENV) black jiggle_version # --exclude .venv
	$(VENV) black test # --exclude .venv
	$(VENV) git2md jiggle_version --ignore __init__.py __pycache__ --output SOURCE.md

.PHONY: black

pre-commit: isort black
	@echo "Pre-commit checks"
	$(VENV) pre-commit run --all-files

.PHONY: pre-commit

bandit:
	@echo "Security checks...off"
#	$(VENV)  bandit jiggle_version -r

.PHONY: bandit

.PHONY: pylint
pylint: isort black
	@echo "Linting with pylint"
	$(VENV) ruff check --fix jiggle_version
	# pylint never actually ran here while targets were stamp files; this is today's score, ratchet it up.
	$(VENV) pylint jiggle_version --fail-under 9.6

# for when using -j (jobs, run in parallel)
.NOTPARALLEL: isort black

check: mypy test pylint bandit

#.PHONY: publish_test
#publish_test:
#	rm -rf dist && poetry version minor && poetry build && twine upload -r testpypi dist/*

.PHONY: publish
publish: test
	rm -rf dist && hatch build

.PHONY: mypy
mypy:
	$(VENV) echo $$PYTHONPATH
	$(VENV) mypy jiggle_version --ignore-missing-imports --check-untyped-defs

# -- Python 3.15 trial run ---------------------------------------------------
# Uses a dedicated venv so the normal .venv is never clobbered.

PY315 := 3.15.0rc2
VENV315 := .venv315rc2
PY315_EXE := $(VENV315)/Scripts/python.exe

.PHONY: venv315
venv315:
	@echo "Creating Python $(PY315) trial venv at $(VENV315)"
	@test -x $(PY315_EXE) || uv venv $(VENV315) --python $(PY315)
	uv pip install -e . pytest pytest-cov pytest-timeout pytest-mock hypothesis --python $(PY315_EXE)

.PHONY: venv315-clean
venv315-clean:
	@echo "Recreating Python $(PY315) trial venv from scratch"
	uv venv $(VENV315) --python $(PY315) --clear
	@$(MAKE) venv315

.PHONY: test315
test315: venv315
	@echo "Running unit tests on Python $(PY315)"
	$(PY315_EXE) -m pytest test -q --timeout=120

.PHONY: check315
check315: test315
	@echo "Python $(PY315) checks passed."


check_docs:
	$(VENV) interrogate jiggle_version --verbose
	$(VENV) pydoctest --config .pydoctest.json | grep -v "__init__" | grep -v "__main__" | grep -v "Unable to parse"

make_docs:
	pdoc jiggle_version --html -o docs --force

check_md:
	$(VENV) python -X utf8 scripts/check_local_links.py README.md docs
	$(VENV) markdownlint README.md --config .markdownlintrc
	$(VENV) mdformat README.md docs/*.md


check_spelling:
	$(VENV) pylint jiggle_version --enable C0402 --rcfile=.pylintrc_spell
	$(VENV) codespell README.md --ignore-words=private_dictionary.txt
	$(VENV) codespell jiggle_version --ignore-words=private_dictionary.txt

check_changelog:
	# pipx install keepachangelog-manager
	$(VENV) kaclm validate

check_all_docs: check_docs check_md check_spelling check_changelog

check_own_ver:
	# Can it verify itself?
	$(VENV) ./dog_food.sh

#audit:
#	# $(VENV) python -m jiggle_version audit
#	$(VENV) tool_audit single jiggle_version --version=">=2.0.0"

install_plugins:
	echo "N/A"

.PHONY: issues
issues:
	echo "N/A"

pin_actions:
	pinact run --update .github/workflows/build.yml .github/workflows/publish_to_pypi.yml .github/workflows/zizmor.yml

# ── Dogfooding targets (independent, not wired into check) ───────────────────

.PHONY: version-check
version-check:
	@uv run jiggle_version check

.PHONY: dev-status
dev-status:
	@uv run troml-dev-status validate .

.PHONY: prerelease-check
prerelease-check: version-check dev-status
	@echo "Pre-release checks passed."

.PHONY: dont-be-lazy
dont-be-lazy:
	@uv run dont_be_lazy --root . --no-color summary
	@uv run dont_be_lazy --root . --no-color scan jiggle_version --no-config-suppressions || true

.PHONY: pydoc-docs
pydoc-docs:
	@uv run pydoc_fork jiggle_version -o ./pydoc/

# `test` is also a directory in this repo; without .PHONY make can call the target up to date and skip it.
.PHONY: test
