APP=$(shell basename $(CURDIR))
PACKAGE=$(subst _,-,$(APP))
STAGE?=dev

VENV_NAME ?= venv
VENV_ACTIVATE=. $(VENV_NAME)/bin/activate

PYTHON = $(VENV_NAME)/bin/python3
PIP = $(VENV_NAME)/bin/pip3
PYTEST = $(VENV_NAME)/bin/pytest
PYLINT = $(VENV_NAME)/bin/pylint
TWINE = $(VENV_NAME)/bin/twine

DOCKER_VENV_NAME ?= dockerenv

# Set AWS ECR address if your tools is going to be executed in a cloud
REGISTRY ?= $(APP)
REVISION ?= $(shell git rev-parse --short=8 HEAD)
VERSION ?= $(shell git rev-parse --abbrev-ref HEAD)
BUILDTIME = $(shell date -u +"%Y%m%d%H%M%S")

TEST_IMAGE = 475275892927.dkr.ecr.us-east-1.amazonaws.com/python:3.10-aws2-testing

.DEFAULT_GOAL := help

## -
## Local commands:

##   venv - create local virtual env (base.pip dependencies)
$(VENV_NAME)/bin/activate:
	python3.10 -m venv $(VENV_NAME)

$(VENV_NAME): $(VENV_NAME)/bin/activate setup.py
	@echo "Creating virtial environment"
	@cp pip.conf $(VENV_NAME)/
	$(PIP) install -U pip
	$(PIP) install -e .

##   testenv - create local virtual env (test.pip dependencies)
.PHONY:testenv
testenv: $(VENV_NAME)
	@echo "Installing test reqirements"
	$(PIP) install -r requirements/test.pip

##   devenv - create local virtual env (dev.pip dependencies)
.PHONY:devenv
devenv: $(VENV_NAME)
	@echo "Installing dev requirements"
	$(PIP) install -r requirements/dev.pip

##   test - run tests
.PHONY:test
test: testenv
	@echo "Running tests"
	$(PYTEST) --junitxml=./junitreport.xml --cov=$(APP) --cov-report=xml --numprocesses=auto ./tests

##   lint - run pylint check
.PHONY:lint
lint: testenv
	@echo "Running linter"
	$(PYLINT) $(APP)

##   build - build package
.PHONY:build
build: clean $(VENV_NAME)
	@echo "Building python package"
	@cp pip.conf ./$(VENV_NAME)/ && \
	$(PIP) install -U pip setuptools wheel && \
	$(PYTHON) setup.py bdist_wheel

##   upload - upload package on PyPI
.PHONY:upload
upload: build
	@echo "Uploading python package to PyPI"
	@cp pip.conf ./$(VENV_NAME)/ && \
	$(PIP) install -U pip twine && \
	$(TWINE) upload -r pypi --config-file .pypirc ./dist/*

##   clean - delete unnecessary files
.PHONY: clean
clean:
	@echo "Deleting unnecessary files"
	@rm -rf build/
	@rm -rf dist/
	@rm -rf *.egg-info
	@rm -rf *.pyc
	@rm -f $(PACKAGE)-*.tar.gz
	@rm -rf .*_cache
	@rm -rf __pycache__/
	@rm -rf $(DOCKER_VENV_NAME)/
	@find . -name '*.pyc' -delete
	@find . -name '__pycache__' -delete

## -
## Docker commands:

##   docker/login - login to AWS docker, aws configuration need to be set first
.PHONY: docker/login
docker/login:
	@echo "Docker login"
	$$(aws ecr get-login --no-include-email --region us-east-1)

##   docker/test/image - pull image used for tests execution
.PHONY: docker/test/image
docker/test/image: docker/login
	@echo "Pulling image for running tests"
	docker pull ${TEST_IMAGE}

##   docker/image/build - build lambda package for each environment and put all of them into one docker container
.PHONY: docker/image/build
docker/image/build: docker/login
	@echo "Building docker image"
	docker build \
	    --no-cache \
		--build-arg REVISION=$(REVISION) \
		--build-arg BUILDTIME=$(BUILDTIME) \
		--build-arg APP=$(APP) \
		--build-arg PACKAGE=$(PACKAGE) \
		-t $(REGISTRY):$(VERSION) \
		-t $(REGISTRY):$(REVISION) .

##   docker/image/pull - pull an image from a registry
.PHONY: docker/image/pull
docker/image/pull: docker/login
	@echo "Pulling docker image"
	docker pull $(REGISTRY):$(VERSION)

##   docker/image/push - push an image to a registry
.PHONY: docker/image/push
docker/image/push: docker/login
	@echo "Pushing docker image"
	docker push $(REGISTRY):$(VERSION)
	docker push $(REGISTRY):$(REVISION)

##   docker/<local target> - universal docker wrapper for local targets
docker/%: docker/login
	docker run --rm \
		-v /app/$(DOCKER_VENV_NAME) \
		-v $(PWD)/:/$(APP)/ \
		-w /$(APP)/ \
		-e STAGE=$(STAGE) \
		${TEST_IMAGE} make VENV_NAME=$(DOCKER_VENV_NAME) $*

## -
## help - this message
.PHONY: help
help: Makefile
	@echo "Application: ${APP}\n"
	@echo "Run command:\n  make <target>\n"
	@grep -E -h '^## .*' $(MAKEFILE_LIST) | sed -n 's/^##//p'  | column -t -s '-' |  sed -e 's/^/ /'
