-
Notifications
You must be signed in to change notification settings - Fork 30
/
Makefile
285 lines (230 loc) · 10.6 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
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
.PHONY: default pull up stop down restart \
exec exec\:wodby exec\:root drush composer \
prepare\:backend prepare\:frontend prepare\:platformsh \
install platform\:install update \
db\:dump db\:drop db\:import \
platform\:files\:sync platform\:files\:sync\:public platform\:files\:sync\:private \
code\:check code\:fix \
npm logs \
tests\:prepare tests\:run tests\:cli tests\:autocomplete
# Create local environment files.
$(shell cp -n \.\/\.docker\/docker-compose\.override\.default\.yml \.\/\.docker\/docker-compose\.override\.yml)
# If .env file doesn't exist yet - copy it from the default one.
# Then if OS is Linux we change the PHP_TAG:
# - uncomment all the strings containing 'PHP_TAG'
# - comment all the strings containing 'PHP_TAG' and '-dev-macos-'
$(shell ! test -e \.env && cp \.env\.default \.env && uname -s | grep -q 'Linux' && sed -i '/PHP_TAG/s/^# //g' \.env && sed -i -E '/PHP_TAG.+-dev-macos-/s/^/# /g' \.env)
include .env
# Define function to highlight messages.
# @see https://gist.github.com/leesei/136b522eb9bb96ba45bd
# "@echo -p [...]" allow print with color on archlinux
yellow = \033[38;5;3m
bold = \033[1m
reset = \033[0m
message = @echo -p "${yellow}${bold}${1}${reset}"
# Define 3 users with different permissions within the container.
# docker-www-data is applicable only for php container.
docker-www-data = docker-compose exec --user=82:82 $(firstword ${1}) sh -c "$(filter-out $(firstword ${1}), ${1})"
docker-wodby = docker-compose exec $(firstword ${1}) sh -c "$(filter-out $(firstword ${1}), ${1})"
docker-root = docker-compose exec --user=0:0 $(firstword ${1}) sh -c "$(filter-out $(firstword ${1}), ${1})"
default: up
pull:
$(call message,$(PROJECT_NAME): Downloading / updating Docker images...)
docker-compose pull
docker pull $(DOCKER_PHPCS)
docker pull $(DOCKER_ESLINT)
up:
$(call message,$(PROJECT_NAME): Starting Docker containers...)
docker-compose up -d --remove-orphans --scale codecept=0
stop:
$(call message,$(PROJECT_NAME): Stopping Docker containers...)
docker-compose stop
down:
$(call message,$(PROJECT_NAME): Removing Docker network & containers...)
docker-compose down --remove-orphans
restart:
$(call message,$(PROJECT_NAME): Restarting Docker containers...)
@$(MAKE) -s down
@$(MAKE) -s up
exec:
# Remove the first argument from the list of make commands.
$(eval ARGS := $(filter-out $@,$(MAKECMDGOALS)))
$(eval TARGET := $(firstword $(ARGS)))
docker-compose exec --user=82:82 $(TARGET) sh
exec\:wodby:
# Remove the first argument from the list of make commands.
$(eval ARGS := $(filter-out $@,$(MAKECMDGOALS)))
$(eval TARGET := $(firstword $(ARGS)))
docker-compose exec $(TARGET) sh
exec\:root:
# Remove the first argument from the list of make commands.
$(eval ARGS := $(filter-out $@,$(MAKECMDGOALS)))
$(eval TARGET := $(firstword $(ARGS)))
docker-compose exec --user=0:0 $(TARGET) sh
drush:
# Remove the first argument from the list of make commands.
$(eval COMMAND_ARGS := $(filter-out $@,$(MAKECMDGOALS)))
$(call docker-www-data, php drush $(COMMAND_ARGS) --yes)
composer:
# Remove the first argument from the list of make commands.
$(eval COMMAND_ARGS := $(filter-out $@,$(MAKECMDGOALS)))
$(call docker-wodby, php composer $(COMMAND_ARGS))
########################
# Project preparations #
########################
prepare\:backend:
$(call message,$(PROJECT_NAME): Installing/updating Drupal (Contenta CMS) dependencies...)
-$(call docker-wodby, php composer install)
$(call message,$(PROJECT_NAME): Updating permissions for public files...)
$(call docker-root, php mkdir -p web/sites/default/files)
$(call docker-root, php chown -R www-data: web/sites/default/files)
$(call docker-root, php chmod 666 web/sites/default/settings.php)
prepare\:frontend:
$(call message,$(PROJECT_NAME): Installing dependencies for React.js application...)
docker-compose run --rm node npm install
prepare\:platformsh:
$(call message,$(PROJECT_NAME): Setting Platform.sh git remote..)
platform project:set-remote $(PLATFORM_PROJECT_ID)
###################################
# Installation from the bottom up #
###################################
install:
@$(MAKE) -s prepare\:frontend
@$(MAKE) -s up
@$(MAKE) -s prepare\:backend
$(call message,$(PROJECT_NAME): Installing Contenta CMS...)
$(call docker-www-data, php drush site-install contenta_jsonapi --site-name=$(PROJECT_NAME) --account-pass=admin --yes)
$(call message,$(PROJECT_NAME): Preparing test suite...)
@$(MAKE) -s tests\:prepare
@$(MAKE) -s tests\:autocomplete
$(call message,$(PROJECT_NAME): The application is ready!)
######################################################
# Installation from existing Platform.sh environment #
######################################################
platform\:install:
@$(MAKE) -s prepare\:platformsh
@$(MAKE) -s prepare\:frontend
@$(MAKE) -s up
@$(MAKE) -s prepare\:backend
@$(MAKE) -s platform\:files\:sync
@$(MAKE) -s platform\:db\:dump
@$(MAKE) -s db\:import
@$(MAKE) -s update
$(call message,$(PROJECT_NAME): The application is ready!)
#########################################
# Update project after external changes #
#########################################
update:
@$(MAKE) -s prepare\:frontend
$(call message,$(PROJECT_NAME): Installing/updating backend dependencies...)
-$(call docker-wodby, php composer install)
$(call message,$(PROJECT_NAME): Rebuilding Drupal cache...)
@$(MAKE) -s drush cache-rebuild
$(call message,$(PROJECT_NAME): Applying database updates...)
@$(MAKE) -s drush updatedb
$(call message,$(PROJECT_NAME): Importing configurations...)
@$(MAKE) -s drush config-import
$(call message,$(PROJECT_NAME): Applying entity schema updates...)
@$(MAKE) -s drush entup
#######################
# Database operations #
#######################
platform\:db\:dump:
$(call message,$(PROJECT_NAME): Creating DB dump from Platform.sh...)
mkdir -p $(BACKUP_DIR)
-platform db:dump -y --project=$(PLATFORM_PROJECT_ID) --environment=$(PLATFORM_ENVIRONMENT) --app=$(PLATFORM_APPLICATION_BACKEND) --relationship=$(PLATFORM_RELATIONSHIP_BACKEND) --gzip --file=$(BACKUP_DIR)/$(DB_DUMP_NAME).sql.gz
db\:drop:
$(call message,$(PROJECT_NAME): Dropping DB from the local environment...)
@$(MAKE) -s drush sql-drop
db\:import:
@$(MAKE) -s db\:drop
$(call message,$(PROJECT_NAME): Importing DB to the local environment...)
$(call docker-www-data, php zcat ${BACKUP_DIR}/${DB_DUMP_NAME}.sql.gz | drush --root=web sql-cli)
####################
# Files operations #
####################
platform\:files\:sync:
@$(MAKE) -s platform\:files\:sync\:public
@$(MAKE) -s platform\:files\:sync\:private
platform\:files\:sync\:public:
$(call message,$(PROJECT_NAME): Creating public files directory...)
$(call docker-wodby, php mkdir -p web/sites/default/files)
$(call message,$(PROJECT_NAME): Changing public files ownership to wodby...)
$(call docker-root, php chown -R wodby: web/sites/default/files)
$(call message,$(PROJECT_NAME): Downloading public files from Platform.sh...)
-platform mount:download -y --project=$(PLATFORM_PROJECT_ID) --environment=$(PLATFORM_ENVIRONMENT) --app=$(PLATFORM_APPLICATION_BACKEND) \
--mount=web/sites/default/files --target=drupal/web/sites/default/files \
--exclude=css/* --exclude=js/* --exclude=php/* --exclude=styles/*
$(call message,$(PROJECT_NAME): Changing public files ownership to www-data...)
$(call docker-root, php chown -R www-data: web/sites/default/files)
platform\:files\:sync\:private:
$(call message,$(PROJECT_NAME): Creating private files directory...)
$(call docker-wodby, php mkdir -p web/private)
$(call message,$(PROJECT_NAME): Changing private files ownership to wodby...)
$(call docker-root, php chown -R wodby: web/private)
$(call message,$(PROJECT_NAME): Downloading private files from Platform.sh...)
-platform mount:download -y --project=$(PLATFORM_PROJECT_ID) --environment=$(PLATFORM_ENVIRONMENT) --app=$(PLATFORM_APPLICATION_BACKEND) \
--mount=private --target=drupal/web/private
$(call message,$(PROJECT_NAME): Changing private files ownership to www-data...)
$(call docker-root, php chown -R www-data: web/private)
#######################
# Code quality checks #
#######################
code\:check:
$(call message,$(PROJECT_NAME): Checking PHP code for compliance with coding standards...)
docker run --rm \
-v $(shell pwd)/drupal/web/modules/custom:/app/modules \
$(DOCKER_PHPCS) phpcs \
-s --colors --standard=Drupal,DrupalPractice .
$(call message,$(PROJECT_NAME): Checking Drupal Javascript code for compliance with coding standards...)
docker run --rm \
-v $(shell pwd)/drupal/web/modules/custom:/eslint/modules \
-v $(shell pwd)/drupal/.eslintrc.json:/eslint/.eslintrc.json \
$(DOCKER_ESLINT) .
$(call message,$(PROJECT_NAME): Checking React.js code for compliance with coding standards...)
docker-compose run -T --rm node npm --silent run eslint
code\:fix:
$(call message,$(PROJECT_NAME): Auto-fixing Drupal PHP code issues...)
docker run --rm \
-v $(shell pwd)/drupal/web/modules/custom:/app/modules \
$(DOCKER_PHPCS) phpcbf \
-s --colors --standard=Drupal,DrupalPractice .
$(call message,$(PROJECT_NAME): Auto-fixing Drupal JS code issues...)
docker run --rm \
-v $(shell pwd)/drupal/web/modules/custom:/eslint/modules \
-v $(shell pwd)/drupal/.eslintrc.json:/eslint/.eslintrc.json \
$(DOCKER_ESLINT) --fix .
$(call message,$(PROJECT_NAME): Auto-fixing React.js code issues...)
docker-compose run -T --rm node npm --silent run eslint --fix
#######################
# Frontend operations #
#######################
npm:
$(call message,$(PROJECT_NAME): Running npm command...)
$(eval ARGS := $(filter-out $@,$(MAKECMDGOALS)))
docker-compose run --rm node npm $(ARGS)
logs:
$(call message,$(PROJECT_NAME): Streaming the Next.js application logs...)
docker-compose logs -f node
##############################
# Testing framework commands #
##############################
tests\:prepare:
$(call message,$(PROJECT_NAME): Preparing Codeception framework for testing...)
docker-compose run --rm codecept build
tests\:run:
$(call message,$(PROJECT_NAME): Running Codeception tests...)
$(eval ARGS := $(filter-out $@,$(MAKECMDGOALS)))
docker-compose run --rm codecept run $(ARGS) --debug
tests\:cli:
$(call message,$(PROJECT_NAME): Opening Codeception container CLI...)
docker-compose run --rm --entrypoint bash codecept
tests\:autocomplete:
$(call message,$(PROJECT_NAME): Copying Codeception codbasee in .codecept folder to enable IDE autocomplete...)
docker-compose up -d codecept
rm -rf .codecept
docker cp $(PROJECT_NAME)_codecept:/repo/ .codecept
rm -rf .codecept/.git
# https://stackoverflow.com/a/6273809/1826109
%:
@: