.PHONY: help clean env pip_dev fmt lint test test_unit up down ps logs ci_build_plugins ci_lint_and_test ci_lint_and_test_clean
.DEFAULT_GOAL := help

# define root project dir
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

# add dmp_workflows to PYTHONPATH
export PYTHONPATH := $(PYTHONPATH):./plugins

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 .venv .cache .pytest_cache .mypy_cache .ruff_cache pyunit.xml .coverage coverage.xml
	find . | grep -E '__pycache__|\.pyc|\.pyo$\' | xargs rm -rf

env pip_dev: ## Install dev requirements
	python3.10 -m venv .venv
	.venv/bin/pip install pip setuptools -U
	.venv/bin/pip install -r requirements-dev.txt

lint: ## Run code linters
	.venv/bin/black dags/ plugins/ tests/ --check
	.venv/bin/ruff check dags/ plugins/ tests/
	.venv/bin/mypy dags/ plugins/ tests/

fmt: ## Run code formatters
	.venv/bin/black dags/ plugins/ tests/
	.venv/bin/ruff check dags/ plugins/ tests/ --fix

test: ## Run unit tests
	.venv/bin/pytest tests/unit

test_unit: ## Run unit tests with coverage
	.venv/bin/pytest \
		--cov plugins/dmp_workflows tests/unit/ \
		--cov-report xml \
		--junitxml=pyunit.xml

up: ## Compose docker app
	@docker-compose up -d --build --remove-orphans

down: ## Shutdown docker app
	@docker-compose down

ps: ## List all processes
	@docker-compose ps

logs: ## Show docker logs
	@docker-compose logs -f

ci_build_plugins: ## Build plugins for Jenkins pipeline
	docker-compose -f docker-compose-ci.yml up \
		--exit-code-from build-plugins \
		--abort-on-container-exit \
		--build \
		--remove-orphans \
		build-plugins

ci_lint_and_test: ## Run linters and unit tests in docker
	docker-compose -f docker-compose-ci.yml up \
		--exit-code-from lint-and-test \
		--abort-on-container-exit \
		--build \
		--remove-orphans \
		lint-and-test
