MAKEFLAGS += --always-make
PYTHON_VERSIONS = 27 35 36 37 38 39 310 311 312 313 debian
TEST_PY_TARGETS = $(foreach version, $(PYTHON_VERSIONS), test-py$(version))
BUILD_PY_TARGETS = $(foreach version, $(PYTHON_VERSIONS), build-py$(version))

test-local:
	coverage run -m pytest $(TEST_ARGS) && coverage report

test-all: $(TEST_PY_TARGETS)
	@echo "Running tests for all Python versions"

build-py%:
	@echo "Running py$*"
	docker compose build py$*

test-py%:
	@echo "Running tests for py$*"
	@COMPOSE_PROFILES=py$* docker compose run --rm -it py$* bash -c "coverage run -m pytest $(TEST_ARGS) && coverage report"

py%:
	@echo "Running Python shell in version py$*"
	@COMPOSE_PROFILES=py$* docker compose run --rm -it py$* python

bash-py%:
	@echo "Running bash in version py$*"
	@COMPOSE_PROFILES=py$* docker compose run --rm -it py$* bash

bash-root-py%:
	@echo "Running bash in version py$*"
	@COMPOSE_PROFILES=py$* docker compose run --rm -itu root py$* bash

clean:
	find -regex '.*\.pyc' -exec rm {} \;
	find -regex '.*~' -exec rm {} \;
	rm -rf reg-settings.py MANIFEST dist build *.egg-info rows.1 .tox
	rm -rf docs-build docs/reference docs/man
	coverage erase

fix-imports:
	autoflake --in-place --recursive --remove-unused-variables --remove-all-unused-imports .
	isort .
	black .

install:
	make clean
	make uninstall
	python setup.py install

uninstall:
	pip uninstall -y rows

# TODO: move to use black
lint:
	pylint rows/*.py

lint-tests:
	pylint tests/*.py

docs:
	make clean install
	click-man --target=docs/man/ rows
	pycco --directory=docs/reference --generate_index --skip-bad-files rows/*.py
	pycco --directory=docs/reference/plugins --generate_index --skip-bad-files rows/plugins/*.py
	mkdocs build --strict --site-dir=docs-build
	rm -rf docs/man docs/reference

docs-serve: docs
	cd docs-build && python3 -m http.server

docs-upload: docs
	-git branch --delete --force --quiet gh-pages
	-git push turicas :gh-pages
	ghp-import --no-jekyll --message="Docs automatically built from $(shell git rev-parse HEAD)" --branch=gh-pages --push --force --remote=turicas docs-build/

release:
	python setup.py bdist bdist_wheel --universal bdist_egg upload
