|
| 1 | +SHELL = /bin/bash |
| 2 | + |
| 3 | +.DEFAULT_GOAL := help |
| 4 | + |
| 5 | + |
| 6 | +CURRENT_DIR := $(shell pwd) |
| 7 | + |
| 8 | +# Test reports configuration |
| 9 | +TEST_REPORT_DIR ?= $(CURRENT_DIR)/tests/report |
| 10 | +TEST_REPORT_FILE ?= nose2-junit.xml |
| 11 | + |
| 12 | +# general targets timestamps |
| 13 | +TIMESTAMPS = .timestamps |
| 14 | +REQUIREMENTS := $(TIMESTAMPS) $(PIP_FILE) $(PIP_FILE_LOCK) |
| 15 | + |
| 16 | +# Find all python files that are not inside a hidden directory (directory starting with .) |
| 17 | +PYTHON_FILES := $(shell find ./* -type d \( -path ./build -o -path ./dist \) -prune -false -o -type f -name "*.py" -print) |
| 18 | + |
| 19 | +# PIPENV files |
| 20 | +PIP_FILE = Pipfile |
| 21 | +PIP_FILE_LOCK = Pipfile.lock |
| 22 | + |
| 23 | +# default configuration |
| 24 | +ENV_FILE ?= .env.local |
| 25 | + |
| 26 | +# Commands |
| 27 | +PIPENV_RUN := pipenv run |
| 28 | +PYTHON := $(PIPENV_RUN) python3 |
| 29 | +PIP := $(PIPENV_RUN) pip3 |
| 30 | +YAPF := $(PIPENV_RUN) yapf |
| 31 | +ISORT := $(PIPENV_RUN) isort |
| 32 | +NOSE := $(PIPENV_RUN) nose2 |
| 33 | +PYLINT := $(PIPENV_RUN) pylint |
| 34 | + |
| 35 | +PACKAGE_VERSION = $(shell awk '/^Version:/ {print $$2}' gatilegrid.egg-info/PKG-INFO) |
| 36 | + |
| 37 | + |
| 38 | +all: help |
| 39 | + |
| 40 | + |
| 41 | +.PHONY: help |
| 42 | +help: |
| 43 | + @echo "Usage: make <target>" |
| 44 | + @echo |
| 45 | + @echo "Possible targets:" |
| 46 | + @echo -e " \033[1mSetup TARGETS\033[0m " |
| 47 | + @echo "- setup Create the python virtual environment with developper tools" |
| 48 | + @echo -e " \033[1mFORMATING, LINTING AND TESTING TOOLS TARGETS\033[0m " |
| 49 | + @echo "- format Format the python source code" |
| 50 | + @echo "- lint Lint the python source code" |
| 51 | + @echo "- format-lint Format and lint the python source code" |
| 52 | + @echo "- test Run the tests" |
| 53 | + @echo -e " \033[1mPACKAGING TARGETS\033[0m " |
| 54 | + @echo "- package Create package" |
| 55 | + @echo "- publish Tag and publish package to PyPI" |
| 56 | + @echo -e " \033[1mCLEANING TARGETS\033[0m " |
| 57 | + @echo "- clean Clean generated files" |
| 58 | + @echo "- clean-venv Clean python venv" |
| 59 | + @echo "- clean-all Clean everything" |
| 60 | + |
| 61 | + |
| 62 | +# Build targets. Calling setup is all that is needed for the local files to be installed as needed. |
| 63 | + |
| 64 | +.PHONY: setup |
| 65 | +setup: $(REQUIREMENTS) |
| 66 | + pipenv install --dev |
| 67 | + |
| 68 | +# linting target, calls upon yapf to make sure your code is easier to read and respects some conventions. |
| 69 | + |
| 70 | +.PHONY: format |
| 71 | +format: $(REQUIREMENTS) |
| 72 | + $(YAPF) -p -i --style .style.yapf $(PYTHON_FILES) |
| 73 | + $(ISORT) $(PYTHON_FILES) |
| 74 | + |
| 75 | + |
| 76 | +.PHONY: ci-check-format |
| 77 | +ci-check-format: format |
| 78 | + @if [[ -n `git status --porcelain` ]]; then \ |
| 79 | + >&2 echo "ERROR: the following files are not formatted correctly:"; \ |
| 80 | + >&2 git status --porcelain; \ |
| 81 | + exit 1; \ |
| 82 | + fi |
| 83 | + |
| 84 | + |
| 85 | +.PHONY: lint |
| 86 | +lint: $(REQUIREMENTS) |
| 87 | + $(PYLINT) $(PYTHON_FILES) |
| 88 | + |
| 89 | + |
| 90 | +.PHONY: format-lint |
| 91 | +format-lint: format lint |
| 92 | + |
| 93 | + |
| 94 | +# Test target |
| 95 | + |
| 96 | +.PHONY: test |
| 97 | +test: $(DEV_REQUIREMENTS_TIMESTAMP) |
| 98 | + mkdir -p $(TEST_REPORT_DIR) |
| 99 | + $(NOSE) -v -c tests/unittest.cfg --junit-xml-path $(TEST_REPORT_DIR)/$(TEST_REPORT_FILE) -s tests/ |
| 100 | + |
| 101 | + |
| 102 | +# Packaging target |
| 103 | + |
| 104 | +.PHONY: package |
| 105 | +package: $(DEV_REQUIREMENTS_TIMESTAMP) |
| 106 | + $(PYTHON) -m build |
| 107 | + |
| 108 | + |
| 109 | +.PHONY: publish |
| 110 | +publish: publish-check package |
| 111 | + @echo "Upload package version=$(PACKAGE_VERSION)" |
| 112 | + $(PYTHON) -m twine upload dist/* |
| 113 | + |
| 114 | + |
| 115 | +# Clean targets |
| 116 | + |
| 117 | +.PHONY: clean-venv |
| 118 | +clean-venv: |
| 119 | + pipenv --rm |
| 120 | + |
| 121 | +.PHONY: clean |
| 122 | +clean: clean-venv |
| 123 | + @# clean python cache files |
| 124 | + find . -name __pycache__ -type d -print0 | xargs -I {} -0 rm -rf "{}" |
| 125 | + rm -rf $(TEST_REPORT_DIR) |
| 126 | + rm -rf $(TIMESTAMPS) |
| 127 | + rm -rf dist |
| 128 | + rm -rf build |
| 129 | + rm -rf *.egg-info |
| 130 | + rm -f .coverage |
| 131 | + |
| 132 | +.PHONY: clean-all |
| 133 | +clean-all: clean |
| 134 | + |
| 135 | +# Actual builds targets with dependencies |
| 136 | + |
| 137 | +$(TIMESTAMPS): |
| 138 | + mkdir -p $(TIMESTAMPS) |
| 139 | + |
| 140 | + |
| 141 | +$(VENV_TIMESTAMP): |
| 142 | + test -d $(VENV) || $(SYSTEM_PYTHON) -m venv $(VENV) && $(PIP) install --upgrade pip setuptools |
| 143 | + @touch $(VENV_TIMESTAMP) |
| 144 | + |
| 145 | + |
| 146 | +$(DEV_REQUIREMENTS_TIMESTAMP): $(VENV_TIMESTAMP) $(DEV_REQUIREMENTS) |
| 147 | + $(PIP) install -r $(DEV_REQUIREMENTS) |
| 148 | + @touch $(DEV_REQUIREMENTS_TIMESTAMP) |
| 149 | + |
| 150 | + |
| 151 | +publish-check: |
| 152 | + @echo "Check if publish is allowed" |
| 153 | + @if [ -n "`git status --porcelain`" ]; then echo "ERROR: Repo is dirty !" >&2; exit 1; fi |
0 commit comments