-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
157 lines (135 loc) · 3.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
#
# Current working directory
#
PWD=$(shell pwd)
#
# Defaults
#
src=build
from=master
target=gh-pages
message=Release: $(shell date)
#
# Templates
#
_src=src/$(patsubst js%,resources/scripts%,\
$(patsubst css%,resources/styles%,\
$(patsubst res%,resources%,\
$(patsubst page%,pages%,$(NAME)))))
_path=$(patsubst %/,%,$(_src))
_basedir=$(dir $(_path))
#
# Directories
#
dirname=$(patsubst %/,%,$(_basedir))
filepath=$(patsubst $(_basedir),,$(_path))
#
# Environment vars
#
SOURCE_VERSION ?= $(shell git rev-parse --short=7 HEAD)
NODE_ENV ?= development
export NODE_ENV SOURCE_VERSION
#
# Targets
#
.PHONY: ? add rm dev test deps clean prune dist pages deploy
#
# Utils
#
define iif
@(($1 > /dev/null 2>&1) && printf "\r* $2\n") || printf "\r* $3\n"
endef
#
# Input
#
ifeq ($(BODY),)
BODY := $(shell bash -c 'if test ! -t 0; then cat -; fi')
endif
#
# Validation
#
check_defined = $(strip $(foreach 1,$1, $(call __check_defined,$1,$(strip $(value 2)))))
__check_defined = $(if $(value $1),, $(error $2, e.g. $1=test))
#
# Display all targets in this file
#
?: Makefile
@awk -F':.*?##' '/^[a-z\\%!:-]+:.*##/{gsub("%","*",$$1);gsub("\\\\",":*",$$1);printf "\033[36m%8s\033[0m %s\n",$$1,$$2}' $<
@printf "\n Examples:"
@printf "\n make add:page NAME=example.md BODY='# It works!'"
@printf "\n make rm:Dockerfile"
@printf "\n make clean dev"
@printf "\n\n"
#
# Adding files to the project
#
add: ## Create files, scripts or resources
@$(call check_defined, NAME, Missing file name)
@$(call check_defined, BODY, Missing file content)
@mkdir -p $(PWD)/$(dirname)
@echo $(BODY) > $(PWD)/$(filepath)
@printf "\r* File $(filepath) was created\n"
add\:%: ## Shortcut for adding files
@make -s add NAME=$(subst :,/,$*)/$(NAME) BODY=$(BODY)
#
# Remove files from the project
#
rm: ## Remove **any** stuff from your workspace
@$(call check_defined, NAME, Missing file name)
@$(call iif,rm -r $(PWD)/$(filepath),File $(filepath) was deleted,Failed to delete $(filepath))
@$(call iif,rmdir $(PWD)/$(dirname),Parent directory clear,Parent directory is not empty...)
rm\:%: ## Shortcut for removing files
@make -s rm NAME=$(subst :,/,$*)/$(NAME)
#
# Development tasks
#
dev: deps ## Start development
@npm run dev
#
# Testing tasks
#
test: deps ## Test for syntax issues
@npm run check
#
# Build task
#
dist: deps ## Compile sources for production
@NODE_ENV=production npm run dist -- $(DIST_FLAGS)
#
# Check dependencies
#
deps: ## Check for installed dependencies
@(((ls node_modules | grep .) > /dev/null 2>&1) || npm i) || true
#
# Cleanup
#
clean: ## Remove cache and generated artifacts
@$(call iif,rm -rf $(src),Built artifacts were deleted,Artifacts already deleted)
@$(call iif,unlink cache.json,Cache file was deleted,Cache file already deleted)
#
# Clean dependencies
#
prune: clean ## Remove all stuff from node_modules/*
@printf "\r* Removing all dependencies... "
@rm -rf node_modules/.{bin,cache}
@rm -rf node_modules/*
@echo "OK"
#
# GitHub Pages branch
#
pages: ## Fetch or create the target branch
@(git fetch origin $(target) 2> /dev/null || (\
git checkout --orphan $(target);\
git rm -rf . > /dev/null;\
git commit --allow-empty -m "initial commit";\
git checkout $(from)))
#
# Deployment to GitHub Pages
#
deploy: pages ## Prepare and push changes on target branch
@(mv $(src) .backup > /dev/null 2>&1) || true
@(git worktree remove $(src) --force > /dev/null 2>&1) || true
@(git worktree add $(src) $(target) && (cp -r .backup/* $(src) > /dev/null 2>&1)) || true
@cd $(src) && git add . && git commit -m "$(message)" || true
@(mv .backup $(src) > /dev/null 2>&1) || true
@git push origin $(target) -f || true