-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
200 lines (179 loc) · 5.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
# Documentation
################################################################################
##
##
## make build:
## Usage: make build [ dev | prod | testing ].
## Builds a choosen enviroment using docker-compose. Accepts either of these
## three arguments:
## - dev to build a development environment.
## - prod to build a production environment.
## - test to build a testing environment where you can run tests.
## When nothing is specified, defaults to building prod.
##
## make test:
## Usage: make test [ client | server ] [ int | e2e ].
## Runs tests on the testing environment built with 'make build test'.
## Example: make test server int, make test client e2e.
##
## make clean, fclean:
## Usage: make [ clean | fclean ].
## Clean Stops all running containers and remove the images and volumes.
## Fclean does all of the above and deletes the folder node_modulesfrom the
## server and the client directories, deletes the networks and prunes the
## system.
##
## make stop:
## Stops all running containers.
##
## make getenv:
## Clones a private repo containing the .env* files.
# Commands
################################################################################
REMOVE := rm -rf
CD := cd
DOCKER := docker
DOCKERIMG := docker image
DCOMPOSE := docker-compose
TOUCH := touch
MKDIR := mkdir -p
REPLACE := sed -i
UNAME_S := $(shell uname -s)
PRINT := echo
CLONE := git clone
MOVE := mv
# Colors
################################################################################
GREEN = \033[1;32m
BLUE = \033[1;34m
CYAN = \033[1;36m
RESET = \033[0m
# Sources
################################################################################
DCOMPOSEFILE = docker-compose.prod.yml
PROJECT_REPO = [email protected]:sdummett/ft_transcendence.git
ENV_REPO_NAME = ft_transcendence_env
ENV_REPO = [email protected]:sdummett/$(ENV_REPO_NAME).git
# Directories
################################################################################
SRCS := .
# Environments
################################################################################
ifeq ($(UNAME_S),Darwin)
REPLACE = sed -i'' -e
endif
ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
ifeq (build,$(firstword $(MAKECMDGOALS)))
BUILD_ENV := $(word 1, $(ARGS))
$(eval $(ARGS):;@:)
else ifeq (clean, $(firstword $(MAKECMDGOALS)))
BUILD_ENV := $(word 1, $(ARGS))
$(eval $(ARGS):;@:)
else ifeq (fclean, $(firstword $(MAKECMDGOALS)))
BUILD_ENV := $(word 1, $(ARGS))
$(eval $(ARGS):;@:)
else ifeq (stop, $(firstword $(MAKECMDGOALS)))
BUILD_ENV := $(word 1, $(ARGS))
$(eval $(ARGS):;@:)
else ifeq (test,$(firstword $(MAKECMDGOALS)))
CLIENT_OR_SERVER := $(word 1,$(ARGS))
TEST_TYPE := $(word 2,$(ARGS))
$(eval $(ARGS):;@:)
endif
# ifeq ($(BUILD_ENV), dev)
# DCOMPOSEFILE = docker-compose.dev.yml
# ENVFILE = .env.dev
# RUNNING_ENV = dev
# else ifeq ($(BUILD_ENV), testing)
# DCOMPOSEFILE = docker-compose.dev.yml
# ENVFILE = .env.dev
# RUNNING_ENV = test.int
# else
DCOMPOSEFILE = docker-compose.yml
ENVFILE = .env
RUNNING_ENV = prod
# endif
# Executables & libraries
################################################################################
NAME := ft_transcendence
BACKEND := $(SRCS)/backend
FRONTEND := $(SRCS)/frontend
# DATABASE := $(SRCS)/database
# Flags
################################################################################
UP := up --build #-d
DOWN := down
REMOVEALL := --rmi all --remove-orphans -v
# Global Rule
################################################################################
$(NAME): all
@touch $(NAME)
.PHONY: all
all: build
.PHONY: build
build: getenv
$(REPLACE) "s/RUNNING_ENV=.*/RUNNING_ENV=${RUNNING_ENV}/" \
$(SRCS)/$(ENVFILE)
$(DCOMPOSE) -f $(SRCS)/$(DCOMPOSEFILE) \
--env-file $(SRCS)/$(ENVFILE) $(UP)
# Tests
################################################################################
.PHONY: test
test: getenv
docker exec -w /app $(CLIENT_OR_SERVER) npm run test:$(TEST_TYPE)
# Secrets
################################################################################
.PHONY: getenv
getenv:
@if [ ! -f $(SRCS)/$(ENVFILE) ]; then \
$(CLONE) $(ENV_REPO); \
$(MOVE) $(ENV_REPO_NAME)/.env* .; \
$(REMOVE) $(ENV_REPO_NAME); \
fi \
# Stop & Cleaning Rules
################################################################################
.PHONY: stop
stop:
$(DCOMPOSE) -f $(SRCS)/$(DCOMPOSEFILE) \
--env-file $(SRCS)/$(ENVFILE) $(DOWN)
.PHONY: clean
clean: getenv
# Stops containers and remove images + volumes
$(DCOMPOSE) -f $(SRCS)/$(DCOMPOSEFILE) \
--env-file $(SRCS)/$(ENVFILE) $(DOWN) $(REMOVEALL)
$(REMOVE) $(NAME)
# $(REMOVE) $(SRCS)/.env*
.PHONY: fclean
fclean: clean cleandocker cleannode
.PHONY: cleandocker
cleandocker:
# Clean all : stops containers, remove images, volumes, network
$(DOCKER) system prune --all --force --volumes
$(DOCKER) network prune --force
$(DOCKER) volume prune --force
$(DOCKER) image prune --force
.PHONY: cleannode
cleannode:
# clean node_modules
$(REMOVE) $(CLIENT)/node_modules
$(REMOVE) $(SERVER)/node_modules
.PHONY: re
re: fclean all
# Help & documentation
################################################################################
.PHONY: help
help:
@sed -ne '/@sed/!s/## //p' $(MAKEFILE_LIST) | \
while read -r line; do \
awk ' \
{ \
if ($$line !~ /^ .*/) \
{ \
printf "$(BLUE)%s$(RESET)\n", $$line \
} \
else \
{ \
printf "%s\n", $$line \
} \
}'; \
done