.PHONY: clean-pyc clean-build generate clean
SHELL = bash

PROTO_FILES_DIR=./perxis-proto/proto
OUTPUT_FILES_DIR=./perxis

help:
	@echo "clean - remove all build, test, coverage and Python artifacts"
	@echo "clean-build - remove build artifacts"
	@echo "clean-pyc - remove Python file artifacts"
	@echo "clean-test - remove test and coverage artifacts"
	@echo "clean-proto - remove generated gRPC code"
	@echo "lint - check python code by flake8"
	@echo "generate - generate gRPC code"
	@echo "dist - package"
	@echo "release - package and upload a release"
	@echo "install - install the package to the active Python's site-packages"

clean: clean-build clean-pyc clean-test clean-proto

clean-build:
	rm -fr build/
	rm -fr dist/
	rm -fr .eggs/
	find . -name '*.egg-info' -exec rm -fr {} +
	find . -name '*.egg' -exec rm -f {} + || true

clean-pyc:
	find . -name '*.pyc' -exec rm -f {} +
	find . -name '*.pyo' -exec rm -f {} +
	find . -name '*~' -exec rm -f {} +
	find . -name '__pycache__' -exec rm -fr {} +

clean-test:
	rm -fr .tox/
	rm -f .coverage
	rm -fr htmlcov/

clean-proto:
	find . -name '*_pb2.py' -exec rm -f {} +
	find . -name '*_pb2_grpc.py' -exec rm -f {} +

install-build-requirements:
	pip install -r build-requirements.txt

install-requirements:
	pip install -r requirements.txt

lint: install-build-requirements
	flake8 perxis/

generate: clean-proto install-build-requirements
	find $(PROTO_FILES_DIR) -name '*.proto' -exec python -m grpc_tools.protoc -I ${PROTO_FILES_DIR} --python_out=$(OUTPUT_FILES_DIR) --grpc_python_out=$(OUTPUT_FILES_DIR) {} +
	python fix_imports.py

dist: clean generate
	python setup.py sdist
	python setup.py bdist_wheel
	ls -l dist

release: dist
	python -m twine upload --repository-url $(PYPI_REPO) ./dist/*

install: clean generate install-requirements
	python setup.py install