.PHONY: help env clean
.PHONY: update_poetry_lockfile fmt
.PHONY: docker_unit_lint down_unit_lint
.PHONY: ci_unit_lint_clean ci_unit_lint

help:
	@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

clean:  ## Clean environment
	rm -rf env .cache .pytest_cache .mypy_cache .ruff_cache pyunit.xml .coverage coverage.xml
	find . | grep -E '__pycache__|\.pyc|\.pyo$\' | xargs rm -rf

update_poetry_lockfile:  ## Update poetry.lock file
	docker compose run --build --remove-orphans update-poetry-lockfile

fmt:  ## Run code formatters
	docker compose run --build --remove-orphans format

unit_lint:  ## Run static type checker, linter, and unit tests
	docker compose up \
		--build \
		--exit-code-from unit-lint \
		--abort-on-container-exit \
		--remove-orphans \
		unit-lint

down_unit_lint:  ## Teardown any remaining Docker containers related to unit_lint
	docker compose down unit-lint --remove-orphans

ci_unit_lint: ci_unit_lint_clean  ## Run lint and unit tests in CI/CD
	mkdir -p build
	chmod a+w build
	make unit_lint

ci_unit_lint_clean: down_unit_lint  ## Teardown lint and unit tests in CI/CD
	rm -rf build
