forked from steve-njuguna-k/BackendTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
144 lines (122 loc) · 4.81 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
## Variables used in target commands
SHELL := /bin/bash
ENV ?= Local
SETTINGS_ENV ?= ${ENV}
## Variables to make targets more readable
COMMAND = docker exec -it django-app bash -c
NON_INTERACTIVE_COMMAND = docker exec -i django-app bash -c
MANAGE = python manage.py
DOCKER_ENV_FILE = --env-file ./Envs/${ENV}/variables.env
DOCKER_COMPOSE_FILE = -f ./Envs/${ENV}/docker-compose.yml
DOCKER_FILE = docker-compose ${DOCKER_COMPOSE_FILE} ${DOCKER_ENV_FILE}
SETTINGS_FLAG = --settings=Envs.${SETTINGS_ENV}.django_settings
## Modules settings
TOML_PATH = ./Project/settings/pyproject.toml
BLACK_SETTINGS = --config="${TOML_PATH}"
ISORT_SETTINGS = --settings-path="${TOML_PATH}"
INSTALL_FORMAT_MODULES = pip3 install -r ./Envs/format_requirements.txt
## Testing settings
DJANGO_TEST_SETTINGS = --ds=Envs.${ENV}.test_django_settings
PYTEST_FLAGS = -p no:cacheprovider -p no:warnings
PYTEST_SETTINGS = ${PYTEST_FLAGS} ${DJANGO_TEST_SETTINGS}
COVERAGE_SETTINGS = --cov --cov-config=.coveragerc
HTML_PATH = --cov-report=html:./Project/.htmlconv
HTML_COVERAGE_SETTINGS = ${COVERAGE_SETTINGS} ${HTML_PATH}
## Style to print targets in a nice format
STYLE = {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}
.PHONY: help
help: ## Show this help which show all the possible make targets and its description.
@echo ""
@echo "The following are the make targets you can use in this way 'make <target>':"
@echo ""
@awk ' BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / ${STYLE}' $(MAKEFILE_LIST)
@echo ""
@echo "* You can change the environment settings with the SETTINGS_ENV."
@echo "** You can grep a string with GREP parameter."
@echo "*** You can modify the number of instances created with INSTANCES parameter."
@echo "**** You can modify the path that will be tested with TEST_PATH parameter."
@echo ""
@echo "You can interact with docker-compose using the following schema:"
@echo "${DOCKER_FILE}"
ifeq (docker,$(firstword $(MAKECMDGOALS)))
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
$(eval $(ARGS):;@:)
endif
.PHONY: docker
docker: ## Runs docker compose command. Eg: "make docker up FLAGS=-d".
@${DOCKER_FILE} $(ARGS) ${FLAGS}
ifeq (manage,$(firstword $(MAKECMDGOALS)))
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
$(eval $(ARGS):;@:)
endif
.PHONY: manage
manage: ## Runs python manage command. Eg: "make manage collectstatic".
${COMMAND} "${MANAGE} $(ARGS) ${SETTINGS_FLAG}"
ifeq (app,$(firstword $(MAKECMDGOALS)))
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
$(eval $(ARGS):;@:)
endif
.PHONY: app
app: ## Creates a new django app. Eg: "make app APP_NAME".
${COMMAND} "cd Apps && python ../manage.py startapp $(ARGS)"
.PHONY: bash
bash: ## Open a bash shell in the django container.
@${DOCKER_FILE} exec app /bin/bash
.PHONY: shell
shell: ## Open the shell_plus of django. *
${COMMAND} "${MANAGE} shell_plus ${SETTINGS_FLAG}"
.PHONY: show_urls
show_urls: ## Show the urls of the app. **
ifeq (${GREP},)
@${COMMAND} "${MANAGE} show_urls ${SETTINGS_FLAG}"
else
@${COMMAND} "${MANAGE} show_urls ${SETTINGS_FLAG}| grep ${GREP}"
endif
.PHONY: flush
flush: ## Flush the database. *
@${COMMAND} "${MANAGE} flush ${SETTINGS_FLAG}"
.PHONY: migrations
migrations: ## Creates and applies the django migrations. *
@${COMMAND} "${MANAGE} makemigrations ${SETTINGS_FLAG}"
@${COMMAND} "${MANAGE} migrate ${SETTINGS_FLAG}"
INSTANCES ?= 50
.PHONY: populate
populate: ## Populates the database with dummy data. ***
@${COMMAND} "${MANAGE} populate_db -i $(INSTANCES) ${SETTINGS_FLAG}"
.PHONY: test
test: ## Run the tests. ****
@${COMMAND} "${MANAGE} create_test_db ${SETTINGS_FLAG}"
ifeq (${TEST_PATH},)
@${COMMAND} "pytest . --reuse-db ${PYTEST_SETTINGS}"
else
@${COMMAND} "pytest ${TEST_PATH} --reuse-db ${PYTEST_SETTINGS} -s"
endif
.PHONY: fast-test
fast-test: ## Run the tests in parallel. ****
@${NON_INTERACTIVE_COMMAND} "pytest . ${PYTEST_SETTINGS} -n auto"
.PHONY: test-with-coverage
test-with-coverage: ## Run the tests with coverage.
@${COMMAND} "${MANAGE} create_test_db ${SETTINGS_FLAG}"
@${COMMAND} "pytest . ${PYTEST_SETTINGS} ${COVERAGE_SETTINGS}"
.PHONY: test-with-html
test-with-html: ## Run the tests with coverage and html report.
@${COMMAND} "${MANAGE} create_test_db ${SETTINGS_FLAG}"
@${COMMAND} "pytest . ${PYTEST_SETTINGS} ${HTML_COVERAGE_SETTINGS}"
.PHONY: check-lint
check-lint: ## Check for linting errors.
ifeq (${ENV}, CI)
@${INSTALL_FORMAT_MODULES} && black . ${BLACK_SETTINGS} --check
else
@${COMMAND} "black . ${BLACK_SETTINGS} --check"
endif
.PHONY: check-imports
check-imports: ## Check for errors on imports ordering.
ifeq (${ENV}, CI)
@${INSTALL_FORMAT_MODULES} && isort . ${ISORT_SETTINGS} --check
else
@${COMMAND} "isort . ${ISORT_SETTINGS} --check"
endif
.PHONY: format
format: ## Runs the linter and import sorter.
@${COMMAND} "black . ${BLACK_SETTINGS}"
@${COMMAND} "isort . ${ISORT_SETTINGS}"