-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathMakefile
484 lines (383 loc) · 17.5 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
## ------------------------------------------------------------------------ ##
## Build Project ##
## ------------------------------------------------------------------------ ##
$(shell set -x)
# Since we rely on paths relative to the makefile location,
# abort if make isn't being run from there.
$(if $(findstring /,$(MAKEFILE_LIST)),$(error Please only invoke this makefile from the directory it resides in))
THIS_FILE := $(lastword $(MAKEFILE_LIST))
TO_NULL = 2>&1 >/dev/null
# $(info [THIS_FILE:${THIS_FILE}])
## ------------------------------------------------------------------------ ##
## Suppress display of executed commands
## ------------------------------------------------------------------------ ##
$(VERBOSE).SILENT:
## ------------------------------------------------------------------------ ##
.EXPORT_ALL_VARIABLES:
.IGNORE:
## ------------------------------------------------------------------------ ##
# Use one shell to run all commands in a given target rather than using
# the default setting of running each command in a separate shell
## ------------------------------------------------------------------------ ##
.ONESHELL:
## ------------------------------------------------------------------------ ##
# set -e = bash immediately exits if any command has a non-zero exit status.
# set -u = a reference to any shell variable you haven't previously
# defined -- with the exceptions of $* and $@ -- is an error, and causes
# the program to immediately exit with non-zero code.
# set -o pipefail = the first non-zero exit code emitted in one part of a
# pipeline (e.g. `cat file.txt | grep 'foo'`) will be used as the exit
# code for the entire pipeline. If all exit codes of a pipeline are zero,
# the pipeline will emit an exit code of 0.
# .SHELLFLAGS := -eu -o pipefail -c
## ------------------------------------------------------------------------ ##
# Emits a warning if you are referring to Make variables that don’t exist.
## ------------------------------------------------------------------------ ##
MAKEFLAGS += --warn-undefined-variables
## ------------------------------------------------------------------------ ##
# Removes a large number of built-in rules. Remove "magic" and only do
# what we tell Make to do.
## ------------------------------------------------------------------------ ##
MAKEFLAGS += --no-builtin-rules
## ======================================================================== ##
$(shell [ -f ./.bowerrc ] || cp -prfu config/.bowerrc ./)
$(shell [ -f ./.npmrc ] || cp -prfu config/.npmrc ./)
$(shell [ -f ./.env ] || echo "NODE_ENV=production" >> .env)
$(shell [ -f ./VERSION ] || echo "0.0.0" > VERSION)
## ======================================================================== ##
## Environment variables for the build
## ======================================================================== ##
include .env
# The shell in which to execute make rules
SHELL := /bin/sh
# The CMake executable
CMAKE_COMMAND = /usr/bin/cmake
# The command to remove a file
# RM = /usr/bin/cmake -E remove -f
# Escaping for special characters
EQUALS = =
## ======================================================================== ##
APP_NAME := cv
APP_PREF := cv_
APP_SLOG := "CV + PORTFOLIO"
APP_LOGO := ./assets/BANNER
APP_REPO := $(shell git ls-remote --get-url)
GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
GIT_COMMIT := $(shell git rev-list --remove-empty --max-count=1 --reverse --remotes --date-order)
CODE_VERSION := $(strip $(shell cat ./VERSION))
## ------------------------------------------------------------------------ ##
DT = $(shell date +'%T')
TS = $(shell date +'%s')
DZ = $(shell date +'%Y%m%dT%H%M%S%:z')
WD := $(shell pwd -P)
BD := $(WD)/bin
BUILD_FILE = BUILD-$(CODE_VERSION)
BUILD_CNTR = $(strip $(shell [ -f "$(BUILD_FILE)" ] && cat $(BUILD_FILE) || echo 0))
BUILD_CNTR := $(shell echo $$(( $(BUILD_CNTR) + 1 )))
BUILD_TMPL := config/build.tpl
BUILD_DATA := config/build.json
BUILD_FULL := $(shell date +'%Y-%m-%dT%H:%M:%S%:z')
BUILD_DATE := $(shell date +'%Y-%m-%d')
BUILD_TIME := $(shell date +'%H:%M:%S')
BUILD_YEAR := $(shell date +'%Y')
BUILD_HASH := $(shell echo "$(BUILD_FULL)" | md5sum | cut -b -4)
## ------------------------------------------------------------------------ ##
## Colors definition
## ------------------------------------------------------------------------ ##
include $(BD)/Colors
## ------------------------------------------------------------------------ ##
## Shorthands
## ------------------------------------------------------------------------ ##
LN := ln -sf --backup=simple
CP := cp -prf --backup=simple
MV := mv -f
FMP := ffmpeg -hide_banner -stats -loglevel error -y
FIGLET := figlet-toilet -t -k -f standard -F border -F gay
TOILET := figlet-toilet -t -f small -F border
GULP := gulp --color
ARGS = $(shell echo '$@' | tr [:upper:] [:lower:])
STG = $(shell echo '$@' | tr [:lower:] [:upper:])
DAT = [$(Gray)$(DT)$(NC)]
BEGIN = $(Yellow)$(On_Blue)BEGIN$(NC) RECIPE
RESULT = $(White)$(On_Purple)RESULT$(NC)
DONE = $(White)$(On_Green)DONE RECIPE$(NC)
FINE = $(Yellow)$(On_Green)FINISHED GOAL$(NC)
TARG = [$(Orange) $@ $(NC)]
THIS = [$(Red) $(THIS_FILE) $(NC)]
OKAY = [$(White) OK $(NC)]
## ------------------------------------------------------------------------ ##
## ENVIRONMENT ##
## ------------------------------------------------------------------------ ##
NODE_ENV := $(shell grep NODE_ENV ./.env | cut -d "=" -f 2)
APP_ENV := $(NODE_ENV)
DEBUG := $(shell grep DEBUG ./.env | cut -d "=" -f 2)
APP_DEBUG := $(DEBUG)
ifeq ($(APP_ENV),)
$(info $(DAT) $(Orange)APP_ENV$(NC) is $(Yellow)$(On_Red)NOT DETECTED$(NC)!)
endif
## ------------------------------------------------------------------------ ##
## BUILDs counter
## ------------------------------------------------------------------------ ##
$(file > $(BUILD_FILE),$(BUILD_CNTR))
$(info $(DAT) Write build counter in [$(Yellow)$(BUILD_FILE)$(NC):$(Red)$(BUILD_CNTR)$(NC)])
## ------------------------------------------------------------------------ ##
## BUILD information
## ------------------------------------------------------------------------ ##
BUILD_CONTENT = $(strip $(shell cat $(BUILD_TMPL)))
BUILD_CONTENT := $(subst BUILD_CNTR,$(BUILD_CNTR),$(BUILD_CONTENT))
BUILD_CONTENT := $(subst BUILD_FULL,$(BUILD_FULL),$(BUILD_CONTENT))
BUILD_CONTENT := $(subst BUILD_DATE,$(BUILD_DATE),$(BUILD_CONTENT))
BUILD_CONTENT := $(subst BUILD_TIME,$(BUILD_TIME),$(BUILD_CONTENT))
BUILD_CONTENT := $(subst BUILD_YEAR,$(BUILD_YEAR),$(BUILD_CONTENT))
BUILD_CONTENT := $(subst BUILD_HASH,$(BUILD_HASH),$(BUILD_CONTENT))
BUILD_CONTENT := $(subst APP_ENV,$(APP_ENV),$(BUILD_CONTENT))
BUILD_CONTENT := $(subst GIT_BRANCH,$(GIT_BRANCH),$(BUILD_CONTENT))
BUILD_CONTENT := $(subst GIT_COMMIT,$(GIT_COMMIT),$(BUILD_CONTENT))
BUILD_CONTENT := $(subst CODE_VERSION,$(CODE_VERSION),$(BUILD_CONTENT))
$(file > $(BUILD_DATA),$(BUILD_CONTENT))
$(info $(DAT) Created file [$(Yellow)BUILD_DATA$(NC):$(Cyan)$(BUILD_DATA)$(NC)]);
## ------------------------------------------------------------------------ ##
## COMMIT information
## ------------------------------------------------------------------------ ##
$(file > COMMIT,$(GIT_COMMIT));
$(info $(DAT) Created file [$(Yellow)COMMIT$(NC):$(Gray)$(GIT_COMMIT)$(NC)]);
## ------------------------------------------------------------------------ ##
## DIRECTORIES ##
## ------------------------------------------------------------------------ ##
ARC := arch
SRC := src
VER := v$(CODE_VERSION)-b$(BUILD_CNTR)
BLD := build-$(CODE_VERSION)-$(BUILD_CNTR)
DST := dist-$(CODE_VERSION)-$(BUILD_CNTR)
WEB := web-$(CODE_VERSION)-$(BUILD_CNTR)
DEV := dev-$(CODE_VERSION)-$(BUILD_CNTR)
APP_UID := $(shell echo "$(APP_NAME)" | tr [:lower:] [:upper:])-$(VER)-$(APP_ENV)
## ------------------------------------------------------------------------ ##
## PATHS ##
## ------------------------------------------------------------------------ ##
DIR_ARC := $(WD)/$(ARC)
DIR_SRC := $(WD)/$(SRC)
DIR_BUILD := $(WD)/$(BLD)-$(APP_ENV)
DIR_DIST := $(WD)/$(DST)-$(APP_ENV)
DIR_WEB := $(WD)/$(WEB)-$(APP_ENV)
## ------------------------------------------------------------------------ ##
## Query the default goal
## ------------------------------------------------------------------------ ##
ifeq ($(.DEFAULT_GOAL),)
.DEFAULT_GOAL := _default
endif
$(info $(DAT) $(Yellow)$(On_Purple)GOALS$(NC));
$(info $(DAT) \-- $(Orange)DEFAULT$(NC): [$(White)$(.DEFAULT_GOAL)$(NC)]);
$(info $(DAT) \-- $(Orange)CURRENT$(NC): [$(Purple)$(MAKECMDGOALS)$(NC)]);
## ------------------------------------------------------------------------ ##
## INCLUDES ##
## ------------------------------------------------------------------------ ##
include $(BD)/*.mk
## ------------------------------------------------------------------------ ##
## SET DEFAULT GOAL ##
## ------------------------------------------------------------------------ ##
PHONY := _default
# _default: run ;
_default: $(APP_ENV) ;
@ echo "$(DAT) $(FINE): $(TARG)" ;
## ------------------------------------------------------------------------ ##
## CREATE PROJECT PATHs ##
## ------------------------------------------------------------------------ ##
PHONY += mkdirs
mkdirs: ;
# @ echo "$(HR)" ;
@ [ -d $(DIR_ARC) ] || mkdir -p $(DIR_ARC)
@ [ -d $(DIR_SRC) ] || mkdir -p $(DIR_SRC)
@ [ -d $(DIR_BUILD) ] || mkdir -p $(DIR_BUILD)
@ [ -d $(DIR_DIST) ] || mkdir -p $(DIR_DIST)
@ [ -d $(DIR_WEB) ] || mkdir -p $(DIR_WEB)
@ echo "$(DAT) $(DONE): $(TARG)" ;
# @ echo "$(HR)" ;
## ------------------------------------------------------------------------ ##
PHONY += test config
test: banner state help ;
@ export NODE_ENV="${APP_ENV}"; npm run test
@ echo "$(DAT) $(FINE): $(TARG)"
config: ;
@ echo "$(DAT) $(BEGIN): $(TARG)"
export NODE_ENV="${APP_ENV}"; npm run config
@ echo "$(DAT) $(FINE): $(TARG)"
## ------------------------------------------------------------------------ ##
PHONY += tasklist tasktree critical
tasklist: ;
gulp --tasks --depth 3 --color
@ echo "$(DAT) $(FINE): $(TARG)"
tasktree: ;
gulp --tasks --depth 5 --color
@ echo "$(DAT) $(FINE): $(TARG)"
# critical: ;
# @ export NODE_ENV="${APP_ENV}"; npm run crit
# @ echo "$(DAT) $(FINE): $(TARG)"
## ------------------------------------------------------------------------ ##
PHONY += bower
bower: ;
@ echo "$(HR)" ;
$(FIGLET) "MK: $(STG)"
export NODE_ENV="${APP_ENV}"; npm run bower
@ echo "$(DAT) $(DONE): $(TARG)"
@ echo "$(HR)" ;
## ------------------------------------------------------------------------ ##
## Setup packages used by installer
## ------------------------------------------------------------------------ ##
setup: setup-deps ;
@ touch ./$(ARGS)
@ echo "$(DAT) $(DONE): $(TARG)"
setup-deps: ;
@ echo "$(DAT) $(BEGIN): $(TARG)"
sudo apt-get -qq -y install pwgen figlet toilet toilet-fonts jq
npm i --verbose gulp
npm i --verbose gulp-cli
npm i --verbose
$(FIGLET) "BOWER: $(STG)"
bower i --allow-root --production --verbose
@ touch ./$(ARGS)
@ echo "$(DAT) $(DONE): $(TARG)"
## ------------------------------------------------------------------------ ##
PHONY += pre-update update reupdate
pre-build:| clean-build setup bower ;
# @ echo "$(DAT) $(BEGIN): $(TARG)"
# @ cd ${WD} && $(RM) -vf ./build
# @ export NODE_ENV="${APP_ENV}"; npm run populate
@ echo "$(DAT) $(DONE): $(TARG)"
# build: mkdirs setup ;
# build: mkdirs setup bower ;
build: mkdirs ;
$(FIGLET) "MK: $(STG)"
@ cd ${WD} && cp -prf ${SRC}/* ${DIR_BUILD}/
export NODE_ENV="${APP_ENV}"; npm run build
@ touch ./$(ARGS)
@ echo "$(DAT) $(DONE): $(TARG) [$(Cyan)$(DIR_BUILD)$(NC)]"
@ echo "$(HR)" ;
pre-dist: ;
@ cd ${WD} && $(RM) -vf ./dist
@ cd ${WD} && $(RM) -rf ${DIR_DIST}/*
@ echo "$(DAT) $(DONE): $(TARG)"
dist: build video ;
$(FIGLET) "MK: $(STG)"
cd ${WD} && cp -prf ${DIR_BUILD}/* ${DIR_DIST}/
cd ${WD} && $(RM) -rf ${DIR_DIST}/resources
$(TOILET) "MK: BACKUP"
@ cd ${WD} && tar -c "${DST}-${APP_ENV}" | gzip -9 > "${ARC}/${APP_NAME}-${VER}-${APP_ENV}.tar.gz"
@ echo "Archived to: [${Blue}${ARC}/${APP_NAME}-${VER}-${APP_ENV}.tar.gz${NC}]"
@ cd ${WD} && touch ./$(ARGS)
@ echo "$(DAT) $(DONE): $(TARG) [$(Cyan)$(DIR_DIST)$(NC)]"
@ echo "$(HR)" ;
pre-deploy: ;
$(RM) -vf ./deploy
@ echo "$(DAT) $(DONE): $(TARG)"
deploy: dist pre-deploy ;
$(FIGLET) "MK: $(STG)"
cd ${WD} && cp -prf ${DIR_DIST}/* ${DIR_WEB}/
$(RM) -vf devroot 2>&1 >/dev/null
$(RM) -vf webroot 2>&1 >/dev/null
$(LN) ${DIR_DIST} devroot
$(LN) ${DIR_WEB} webroot
touch ./$(ARGS)
@ echo "$(DAT) $(DONE): $(TARG) [$(Cyan)$(DIR_WEB)$(NC)]"
@ echo "$(HR)" ;
pre-update: ;
@ cd ${WD} && $(RM) ./setup ./setup-deps
@ echo "$(DAT) $(DONE): $(TARG)"
update: setup ;
@ echo "$(DAT) $(FINE): $(TARG)"
reupdate: pre-update update ;
@ echo "$(DAT) $(FINE): $(TARG)"
## ------------------------------------------------------------------------ ##
DIR_IMGS := assets/img/works
GIF_FILES := $(notdir $(wildcard $(DIR_SRC)/$(DIR_IMGS)/*.gif))
BASE_NAMES := $(basename $(GIF_FILES))
MPEG_FILES := $(patsubst %.gif,%.mp4,"$(DIR_BUILD)/$(DIR_IMGS)/$(GIF_FILES)")
WEBM_FILES := $(patsubst %.gif,%.webm,"$(DIR_BUILD)/$(DIR_IMGS)/$(GIF_FILES)")
## ------------------------------------------------------------------------ ##
## Create backup archive
## ------------------------------------------------------------------------ ##
PHONY += backup
backup: ;
@ echo "$(HR)" ;
$(TOILET) "MK: $(STG)"
@ tar -cv "${DST}-${APP_ENV}" | gzip -9 > "${ARC}/${APP_NAME}-${VER}-${APP_ENV}.tar.gz"
@ echo "$(DAT) $(FINE): $(TARG) [$(APP_VER)]"
@ echo "$(HR)" ;
## ------------------------------------------------------------------------ ##
## Create videos from *.gif files
## ------------------------------------------------------------------------ ##
# PHONY += print-names video
PHONY += print-names
print-names: ;
@ echo "$(HR)" ;
@ echo "$(DAT) $(BEGIN): $(TARG)" ;
@ echo "$(DAT) DIR_IMGS \t= [$(White)$(DIR_IMGS)$(NC)]"
@ echo "$(DAT) BASE_NAMES \t= [$(Gray)$(BASE_NAMES)$(NC)]"
@ echo "$(DAT) GIF_FILES \t= [$(Cyan)$(GIF_FILES)$(NC)]"
# @ echo "$(DAT) MPEG_FILES \t= [$(Orange)$(MPEG_FILES)$(NC)]"
# @ echo "$(DAT) WEBM_FILES \t= [$(Purple)$(WEBM_FILES)$(NC)]"
@ echo "$(DAT) $(DONE): $(TARG)"
@ echo "$(HR)" ;
video: print-names ;
$(FIGLET) "MK: $(STG)"
# @ echo "$(DAT) $(BEGIN): $(TARG)" ;
# $(foreach fbase, $(BASE_NAMES), $(FMP) -i "$(DIR_BUILD)/$(DIR_IMGS)/$(fbase).gif" -b:v 0 -crf 25 -f mp4 -vcodec libx264 -y "$(DIR_BUILD)/$(DIR_IMGS)/$(fbase).mp4" -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" ;)
# @ echo "$(DAT) [$(RESULT)] $(Purple)MPEG$(NC) files:"
# ls -l $(DIR_BUILD)/$(DIR_IMGS)/*.mp4 | grep mp4 --color
$(foreach fbase, $(BASE_NAMES), $(FMP) -i "$(DIR_BUILD)/$(DIR_IMGS)/$(fbase).gif" -c libvpx-vp9 -b:v 0 -crf 41 -y "$(DIR_BUILD)/$(DIR_IMGS)/$(fbase).webm" ;)
@ echo "$(DAT) [$(RESULT)] $(Orange)WEBM$(NC) files:"
ls -l $(DIR_BUILD)/$(DIR_IMGS)/*.webm | grep webm --color
@ touch ./$(ARGS)
@ echo "$(DAT) $(DONE): $(TARG)" ;
@ echo "$(HR)" ;
## ------------------------------------------------------------------------ ##
PHONY += rebuild redeploy rb rd
# rebuild: pre-build build pre-dist dist ;
rebuild: pre-build build ;
@ echo "$(DAT) $(DONE): $(TARG) [$(Red)$(VER)$(NC)]"
# redeploy: build pre-dist dist pre-deploy deploy ;
# redeploy: pre-dist dist pre-deploy deploy ;
# redeploy: pre-dist dist deploy ;
redeploy: pre-dist deploy ;
@ echo "$(DAT) $(DONE) $(TARG): [$(Cyan)$(DIR_WEB)$(NC)]"
rb: rebuild ;
@ echo "$(DAT) $(DONE): $(TARG) [$(Red)$(VER)$(NC)]"
rd: redeploy ;
@ echo "$(DAT) $(DONE): $(TARG) [$(Red)$(VER)$(NC)]"
## ------------------------------------------------------------------------ ##
PHONY += _all all full cycle cycle-dev dev dev-setup prod production run watch
#* means the word "all" doesn't represent a file name in this Makefile;
#* means the Makefile has nothing to do with a file called "all" in the same directory.
_all: clean cycle banner ;
@ echo "$(DAT) $(DONE): $(TARG)"
all: _all ;
@ echo "$(DAT) $(DONE): $(TARG)"
full: clean-all _all banner ;
@ echo "$(DAT) $(DONE): $(TARG)"
cycle: dist ;
@ echo "$(DAT) $(DONE): $(TARG)"
cycle-dev: redeploy ;
@ echo "$(DAT) $(DONE): $(TARG)"
dev: clean-dev banner cycle-dev ;
# @ export NODE_ENV="${APP_ENV}"; npm run dev
@ echo "$(DAT) $(DONE): $(TARG) [$(Red)$(VER)$(NC)]"
dev-setup: clean-deps setup banner cycle-dev ;
@ export NODE_ENV="${APP_ENV}"; npm run dev
@ echo "$(DAT) $(DONE): $(TARG)"
# run: pre-build build pre-dist dist pre-deploy deploy banner ;
run: pre-build build pre-dist dist pre-deploy deploy banner ;
# $(FIGLET) "MK: $(STG): [$(VER)]"
@ echo "$(DAT) $(DONE): $(TARG) [$(Red)$(VER)$(NC)]"
production: run ;
@ echo "$(DAT) $(DONE): $(TARG) [$(Red)$(VER)$(NC)]"
prod: production ;
@ echo "$(DAT) $(DONE): $(TARG)"
watch:
export NODE_ENV="${APP_ENV}"; npm run watch
@ echo "$(DAT) $(DONE): $(TARG)"
## ------------------------------------------------------------------------ ##
## Declare the contents of the .PHONY variable as phony. We keep that
## information in a variable so we can use it in if_changed and friends.
## ------------------------------------------------------------------------ ##
.PHONY: $(PHONY)
## ------------------------------------------------------------------------ ##