APP = flask-atlas-auth

BUILD_NUMBER  ?= 1
PROJECT       ?= flask-atlas-auth
PROJECT_GROUP ?= core

BASE_AWS_PROFILE    = gdb-infra-dev
BASE_AWS_ACCOUNT_ID = 483193324480
BASE_AWS_REGION     = us-east-1
BASE_ECR_REGISTRY   = $(BASE_AWS_ACCOUNT_ID).dkr.ecr.$(BASE_AWS_REGION).amazonaws.com

AWS_PROFILE    ?= gdb-core-dev
AWS_ACCOUNT_ID ?= 582412345909
AWS_REGION     ?= us-east-1

ECR_REGISTRY        ?= $(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com
ECR_REPOSITORY_NAME ?= $(PROJECT_GROUP)/$(PROJECT)
ECR_REGISTRY_URI    ?= $(ECR_REGISTRY)/$(ECR_REPOSITORY_NAME)

REVISION  ?= $(shell git rev-parse --short=8 HEAD)
BUILDTIME ?= $(shell date -u +"%Y%m%d%H%M%S")
VERSION   ?= build-$(BUILDTIME).$(REVISION)
SEMVER    ?= $(shell cat VERSION )

PYTHON = $(VENV)/bin/python
PIP    = $(VENV)/bin/pip
PYTEST = $(VENV)/bin/pytest
PYLINT = $(VENV)/bin/pylint
TWINE  = $(VENV)/bin/twine
SAFETY = $(VENV)/bin/safety
FLAKE  = $(VENV)/bin/flake8
BANDIT = $(VENV)/bin/bandit
BLACK  = $(VENV)/bin/black

PYPI_INDEX_URL ?= https://pypi.org/simple

.DEFAULT_GOAL := help

## -
## 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/^/ /'

## -
## Local commands:

$(VENV)/bin/activate:
	python -m venv $(VENV)/

##   venv - create local virtual env
venv: $(VENV)/bin/activate
	@echo "Creating virtual environment"
	$(PIP) install -r requirements/local.txt

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

##   test - run tests locally
.PHONY:test
test: testenv
	@echo "Run tests"
	$(PYTEST) -vv --junitxml=./report.xml --cov=flask_atlas_auth --cov-report=xml .

##   checkstyle - run checkstyle locally
.PHONY:checkstyle
checkstyle: testenv
	@echo "Run checkstyle"
	$(FLAKE) ./flask_atlas_auth/ --max-line-length=120 --ignore=E203,W503 --max-complexity=17

##   dependencies-check - run safety locally
.PHONY:dependencies-check
dependencies-check: testenv
	@echo "Run dependencies check"
	$(SAFETY) check

##   dependencies-check - run bandit locally
.PHONY:owasp
owasp: testenv
	@echo "Run owasp check"
	$(BANDIT) -r ./flask_atlas_auth

##   static-typing - run safety and bandit locally
.PHONY:security
security: dependencies-check owasp

##   prettify - run prettify locally
.PHONY:prettify
prettify: testenv
	$(BLACK) ./flask_atlas_auth/ --exclude='/(\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|venv|_build|buck-out|build|dist|migrations|.pytest_cache)/' --line-length=120 --target-version=py38

##   build - run package build
.PHONY:build
build: venv
	@echo "Run deployment"
	$(PYTHON) setup.py sdist bdist_wheel

##   deploy - run deploy
.PHONY:deploy
deploy: build
	@echo "Run deployment"
	$(TWINE) upload --repository pypi dist/*

## -
## Version management

##   version/patch - increment version patch
.PHONY: version/patch
version/patch:
	@bumpversion \
		--config-file .bumpversion.cfg \
		--current-version ${SEMVER} \
		patch VERSION --allow-dirty

##   version/minor - increment version minor
.PHONY: version/minor
version/minor:
	@bumpversion \
		--config-file .bumpversion.cfg \
		--current-version ${SEMVER} \
		minor VERSION --allow-dirty

##   version/major - increment version major
.PHONY: version/major
version/major:
	@bumpversion \
		--config-file .bumpversion.cfg \
		--current-version ${SEMVER} \
		major VERSION --allow-dirty

## -
## Docker commands:
##   docker/login/base - login to AWS ECR registry using docker, aws configuration need to be set first
.PHONY:docker/login/base
docker/login/base:
	@aws ecr get-login \
		--profile ${BASE_AWS_PROFILE} \
		--no-include-email \
		--region ${BASE_AWS_REGION} | awk '{print $$6}' | \
		docker login -u AWS --password-stdin https://$(BASE_ECR_REGISTRY) || aws ecr get-login-password \
			--profile ${BASE_AWS_PROFILE} \
			--region ${BASE_AWS_REGION} | \
			docker login --username AWS --password-stdin https://$(BASE_ECR_REGISTRY)

##   docker/base/image/pull - pull base image from ECR
.PHONY: docker/base/image/pull
docker/base/image/pull: docker/login/base
	docker pull $(BASE_ECR_REGISTRY)/python:3.8-slim

##   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/base
	@echo "Building docker image"
	docker build \
		--build-arg VERSION=$(VERSION) \
		--build-arg REVISION=$(REVISION) \
		--build-arg BUILDTIME=$(BUILDTIME) \
		--build-arg PYPI_INDEX_URL=$(PYPI_INDEX_URL) \
		-t $(ECR_REGISTRY_URI):$(SEMVER) .

##   docker/<local target> - universal docker wrapper for local targets
docker/%:
	docker run --rm \
		-v $(PWD):/usr/local/src/flask-atlas-auth \
		$(ECR_REGISTRY_URI):$(SEMVER) make VENV=/venv $*
