๐Ÿ“ฆ Pierstoval / CorahnRin

๐Ÿ“„ Makefile ยท 314 lines
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314SHELL=bash

DOCKER_COMPOSE  = docker-compose

EXEC_JS         = $(DOCKER_COMPOSE) exec -T node entrypoint
EXEC_DB         = $(DOCKER_COMPOSE) exec -T database
EXEC_QA         = $(DOCKER_COMPOSE) exec -T --env=APP_ENV=test php entrypoint
EXEC_PHP        = $(DOCKER_COMPOSE) exec -e MAPS_USERNAME="Backer" -e MAPS_PASSWORD="EsterenBacker" -T php entrypoint

SYMFONY_CONSOLE = $(EXEC_PHP) php bin/console
COMPOSER        = $(EXEC_PHP) composer
YARN             = $(EXEC_JS) yarn

TEST_DBNAME = main_test
PORTAL_DBNAME = main
LEGACY_DBNAME = esteren_legacy
DB_USER = root
DB_PWD = root

CURRENT_DATE = `date "+%Y-%m-%d_%H-%M-%S"`

# Helper variables
_TITLE := "\033[32m[%s]\033[0m %s\n" # Green text
_ERROR := "\033[31m[%s]\033[0m %s\n" # Red text

##
## Project
## -------
##

.DEFAULT_GOAL := help
help: ## Show this help message
	@grep -E '(^[a-zA-Z_-]+:.*?##.*$$)|(^##)' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[32m%-25s\033[0m %s\n", $$1, $$2}' | sed -e 's/\[32m##/[33m/'
.PHONY: help

install: build node_modules start vendor check-map-credentials db test-db legacy-db legacy-test-db get-maps-data assets ## Install and start the project
.PHONY: install

build: ## Build the Docker images
	@$(DOCKER_COMPOSE) pull --include-deps
	@$(DOCKER_COMPOSE) build --force-rm --compress
.PHONY: build

start: ## Start all containers and the PHP server
	@$(DOCKER_COMPOSE) up -d --remove-orphans --no-recreate
	@$(MAKE) start-php
.PHONY: start

stop: ## Stop all containers and the PHP server
	@$(DOCKER_COMPOSE) stop
	@$(MAKE) stop-php
.PHONY: stop

restart: stop start ## Restart the containers & the PHP server
.PHONY: restart

kill: ## Stop all containers
	$(DOCKER_COMPOSE) kill
	$(DOCKER_COMPOSE) down --volumes --remove-orphans
.PHONY: kill

clean: ## Stop the project and remove generated files and configuration
clean: kill
	@printf $(_ERROR) "WARNING" "This will remove ALL containers, data, cache, to make a fresh project! Use at your own risk!"

	@if [[ -z "$(RESET)" ]]; then \
		printf $(_ERROR) "WARNING" "If you are 100% sure of what you are doing, re-run with \"$(MAKE) RESET=1 ...\"" ; \
		exit 0 ; \
	fi ; \
	\
	$(DOCKER_COMPOSE) down --volumes --remove-orphans \
	&& rm -rf \
		vendor \
		node_modules \
		data/map_data* \
		build/* \
		public/build \
		public/bundles \
		var/cache \
		var/log \
		var/sessions \
	\
	&& printf $(_TITLE) "OK" "Done!"
.PHONY: clean

full-reset: kill install ## Clean the project and start a fresh install of it
.PHONY: full-reset

##
## Tools
## -----
##

cc: ## Clear and warmup PHP cache
	$(SYMFONY_CONSOLE) cache:clear --no-warmup
	$(SYMFONY_CONSOLE) cache:warmup
.PHONY: cc

db: dev-db migrations fixtures ## Reset the development database
.PHONY: db

dev-db: wait-for-db
	-$(SYMFONY_CONSOLE) doctrine:database:drop --if-exists --force
	-$(SYMFONY_CONSOLE) doctrine:database:create --if-not-exists

.PHONY: dev-db

test-db: wait-for-db ## Create a proper database for testing
	@echo "doctrine:database:drop"
	@APP_ENV=test $(SYMFONY_CONSOLE) --env=test doctrine:database:drop --if-exists --force
	@echo "doctrine:database:create"
	@APP_ENV=test $(SYMFONY_CONSOLE) --env=test doctrine:database:create
	@echo "doctrine:schema:create"
	@APP_ENV=test $(SYMFONY_CONSOLE) --env=test doctrine:migrations:migrate --no-interaction
	@echo "doctrine:fixtures:load"
	@APP_ENV=test $(SYMFONY_CONSOLE) --env=test doctrine:fixtures:load --append --no-interaction
.PHONY: test-db

legacy-db: wait-for-db ## Create a legacy database based on the provided sample
	@-APP_ENV=test $(SYMFONY_CONSOLE) doctrine:database:drop --connection=legacy --if-exists --force
	@-APP_ENV=test $(SYMFONY_CONSOLE) doctrine:database:create --connection=legacy

	@$(EXEC_DB) mysql -u$(DB_USER) -p$(DB_PWD) $(LEGACY_DBNAME) -e "source /srv/legacy_sample.sql"
.PHONY: legacy-db

legacy-test-db: wait-for-db ## Create a legacy database for testing
	@echo "doctrine:database:drop --connection=legacy"
	@-APP_ENV=test $(SYMFONY_CONSOLE) --env=test doctrine:database:drop --connection=legacy --if-exists --force
	@echo "doctrine:database:create --connection=legacy"
	@-APP_ENV=test $(SYMFONY_CONSOLE) --env=test doctrine:database:create --connection=legacy

	@$(EXEC_DB) mysql -u$(DB_USER) -p$(DB_PWD) test_$(LEGACY_DBNAME) -e "source /srv/legacy_sample.sql"
.PHONY: legacy-test-db

prod-db: var/dump.sql dev-db ## Installs production database if it has been saved in "var/dump.sql". You have to download it manually.
	@if [ -f var/dump.sql ]; then \
		$(EXEC_DB) mysql -u$(DB_USER) -p$(DB_PWD) $(PORTAL_DBNAME) -e "source /srv/dump.sql" ;\
	else \
		echo "No prod database to process. Download it and save it to var/dump.sql." ;\
	fi;
.PHONY: prod-db

var/dump.sql: ## Tries to download a database from production environment
	@if [ "${CORAHNRIN_DEPLOY_REMOTE}" = "" ]; then \
		echo "[ERROR] Please specify the CORAHNRIN_DEPLOY_REMOTE env var to connect to a remote" ;\
		exit 1 ;\
	fi; \
	if [ "${CORAHNRIN_DEPLOY_DIR}" = "" ]; then \
		echo "[ERROR] Please specify the CORAHNRIN_DEPLOY_DIR env var to determine which directory to use in prod" ;\
		exit 1 ;\
	fi; \
	ssh ${CORAHNRIN_DEPLOY_REMOTE} ${CORAHNRIN_DEPLOY_DIR}/../dump_db.bash > var/dump.sql

migrations: ## Reset the database
	$(SYMFONY_CONSOLE) doctrine:migrations:migrate --no-interaction
.PHONY: migrations

fixtures: ## Install all dev fixtures in the database
	$(SYMFONY_CONSOLE) doctrine:fixtures:load --append --no-interaction
	@[[ -d public/uploads/portal/ ]] || $(EXEC_PHP) mkdir -p public/uploads/portal/
	@[[ -d var/uploads/products/ ]] || $(EXEC_PHP) mkdir -p var/uploads/products/
.PHONY: fixtures

watch: ## Run Webpack to compile assets on change
	$(YARN) run watch
.PHONY: watch

assets: node_modules ## Run Webpack to compile assets
	@mkdir -p public/build/
	$(YARN) run dev
	@$(EXEC_PHP) php _dev_files/get_maps_assets.php
.PHONY: assets

vendor: ## Install PHP vendors
	$(COMPOSER) install
.PHONY: vendor

node_modules: yarn.lock ## Install JS vendors
	@mkdir -p public/build/
	$(DOCKER_COMPOSE) run --rm --entrypoint=/bin/entrypoint node yarn install
	$(DOCKER_COMPOSE) up -d node
.PHONY: node_modules

wait-for-db:
	@echo " Waiting for database..."
	@for i in {1..5}; do $(EXEC_DB) mysql -u$(DB_USER) -p$(DB_PWD) -e "SELECT 1;" > /dev/null 2>&1 && sleep 1 || echo " Unavailable..." ; done;
.PHONY: wait-for-db

start-php:
	$(DOCKER_COMPOSE) up --force-recreate --no-deps -d php
.PHONY: start-php

stop-php:
	$(DOCKER_COMPOSE) stop php
.PHONY: stop-php

start-node:
	$(DOCKER_COMPOSE) up --force-recreate --no-deps -d node
.PHONY: start-node

full-reset:
	@printf $(_ERROR) "WARNING" "This will remove ALL containers, data, cache, to make a fresh project! Use at your own risk!"

	@if [[ -z "$(RESET)" ]]; then \
		printf $(_ERROR) "WARNING" "If you are 100% sure of what you are doing, re-run with $(MAKE) -e RESET=1 full-reset" ; \
		exit 0 ; \
	fi ; \
	\
	$(DOCKER_COMPOSE) down --volumes --remove-orphans && \
	rm -rf \
		"var/cache/*" \
		"var/uploads/*" \
		"var/log/*" \
		"var/sessions/*" \
		public/build \
		public/bundles \
		public/uploads \
		node_modules \
		vendor \
	&& \
	\
	printf $(_TITLE) "OK" "Done!"
.PHONY: full-reset

##
## Tests
## -----
##

ci-vendor:
	$(COMPOSER) install --no-interaction --classmap-authoritative --prefer-dist
.PHONY: ci-vendor

install-php: build start ci-vendor db test-db assets get-maps-data ## Prepare environment to execute PHP tests
.PHONY: install-php

install-node: build node_modules start-node ## Prepare environment to execute NodeJS tests
.PHONY: install-node

php-tests: start-php qa phpstan cs-dry-run phpunit ## Execute qa & tests
.PHONY: php-tests

phpstan: ## Execute phpstan
phpstan: start-php check-phpunit
	@echo "Clear & warmup test environment cache because phpstan may use it..."
	$(EXEC_QA) bin/console cache:clear --no-warmup
	$(EXEC_QA) bin/console cache:warmup
	$(EXEC_QA) phpstan analyse -c phpstan.neon --xdebug
.PHONY: phpstan

check-phpunit:
	@$(EXEC_PHP) bin/phpunit --version
.PHONY: check-phpunit

cs: ## Execute php-cs-fixer
cs:
	$(EXEC_QA) php-cs-fixer fix
.PHONY: cs

cs-dry-run: ## Execute php-cs-fixer with a simple dry run
	$(EXEC_QA) php-cs-fixer fix --dry-run -vvv --diff --show-progress=dots
.PHONY: cs-dry-run

node-tests: start ## Execute checks & tests
	$(EXEC_JS) yarn run-script test --verbose -LLLL
.PHONY: node-tests

qa: ## Execute CS, linting, security checks, etc
	$(EXEC_QA) bin/console lint:twig templates src
	$(EXEC_QA) bin/console lint:yaml --parse-tags config
	$(EXEC_QA) bin/console lint:yaml --parse-tags src
.PHONY: qa

setup-phpunit: check-phpunit ## Setup PHPUnit before running it

phpunit: check-phpunit ## Execute all PHPUnit tests
	$(EXEC_QA) bin/phpunit
.PHONY: phpunit

phpunit-unit: ## Execute all PHPUnit unit tests
	$(EXEC_QA) bin/phpunit --group=unit
.PHONY: phpunit-unit

phpunit-integration: ## Execute all PHPUnit integration tests
	$(EXEC_QA) bin/phpunit --group=integration
.PHONY: phpunit-integration

phpunit-functional: ## Execute all PHPUnit functional tests
	$(EXEC_QA) bin/phpunit --group=functional
.PHONY: phpunit-functional

phpunit-ux: ## Execute all PHPUnit ux tests
	$(EXEC_QA) bin/phpunit --group=ux
.PHONY: phpunit-ux

coverage: ## Retrieves the code coverage of the phpunit suite
	$(EXEC_QA) php -dextension=pcov -dpcov.enabled=1 bin/phpunit --coverage-html=build/coverage/$(CURRENT_DATE) --coverage-clover=build/coverage.xml
.PHONY: coverage

##
## Project-specific commands
## โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
##

check-map-credentials:
	@$(EXEC_PHP) php _dev_files/_check_map_credentials.php

get-maps-data: ## Download data for Esteren Maps
	@$(EXEC_PHP) php _dev_files/get_maps_data.php
	@$(EXEC_PHP) php _dev_files/cache_maps_data.php
.PHONY: get-maps-data

##