.PHONY: \
	poetry in-project-venv dev-env use-env install install-dev tests \
	update-version sync-version-with-git \
	docker docker-build-run docker-build docker-run \
	docker-rm docker-rm-container docker-rm-image \
	pre-commit get-licenses prep-commit \
	docs sphinx_html sphinx_apidoc bom
.DEFAULT_GOAL := run

export DOCKER=docker
export DOCKERFILE=Dockerfile
export IMAGE_NAME=cv_analysis_service-image
export CONTAINER_NAME=cv_analysis_service-container
export HOST_PORT=9999
export CONTAINER_PORT=9999
export PYTHON_VERSION=python3.10

# all commands should be executed in the root dir or the project,
# specific environments should be deactivated

poetry: in-project-venv use-env dev-env

in-project-venv:
	poetry config virtualenvs.in-project true

use-env:
	poetry env use ${PYTHON_VERSION}

dev-env:
	poetry install --with dev && poetry update

install:
	poetry add $(pkg)

install-dev:
	poetry add --dev $(pkg)

requirements:
	poetry export --without-hashes --output requirements.txt

update-version:
	poetry version prerelease

sync-version-with-git:
	git pull -p && poetry version $(git rev-list --tags --max-count=1 | git describe --tags --abbrev=0)

bom:
	cyclonedx-py poetry -o bom.json

docker: docker-rm docker-build-run

docker-build-run: docker-build docker-run

docker-build:
	$(DOCKER) build \
	--no-cache --progress=plain \
	-t $(IMAGE_NAME) -f $(DOCKERFILE) \
	--build-arg USERNAME=${USERNAME} \
	--build-arg TOKEN=${GITLAB_TOKEN} \
	.

docker-run:
	$(DOCKER) run -it --rm -p $(HOST_PORT):$(CONTAINER_PORT)/tcp --name $(CONTAINER_NAME) $(IMAGE_NAME)

docker-rm: docker-rm-container docker-rm-image

docker-rm-container:
	-$(DOCKER) rm $(CONTAINER_NAME)

docker-rm-image:
	-$(DOCKER) image rm $(IMAGE_NAME)

tests:
	poetry run pytest ./tests

prep-commit:
	docs get-license sync-version-with-git update-version pre-commit

pre-commit:
	pre-commit run --all-files

get-licenses:
	pip-licenses --format=json --order=license --with-urls > pkg-licenses.json

docs: sphinx_apidoc sphinx_html

sphinx_html:
	poetry run sphinx-build -b html docs/source/ docs/build/html -E -a

sphinx_apidoc:
	cp ./README.md ./docs/source/README.md && cp -r ./data ./docs/source/data/ && poetry run sphinx-apidoc ./src -o ./docs/source/modules --no-toc --module-first --follow-links --separate --force

bom:
	cyclonedx-py poetry -o bom.json
