aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: 53ea1b1a0a274e18c430a845375ec458704c7706 (plain)
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
include .default.env
include .env
export

build_dir  := build
js_src_dir := src

# Misc variables.
PATH                    := vendor/bin:node_modules/.bin:$(PATH)
override GIT_VERSION    := $(shell git --no-pager describe --always --tags)
override CUR_VERSION    := $(strip $(shell cat VERSION 2>/dev/null))
override WEBPACK_FLAGS  += $(if $(filter $(ENV),dist),-p)
override COMPOSER_FLAGS += $(if $(filter $(ENV),dist),--no-dev --prefer-dist)
PREV_ENV_FILE           := $(build_dir)/prev_env
PREV_ENV                := $(strip $(shell cat $(PREV_ENV_FILE) 2>/dev/null))

# Files variables.
js_sources  := $(wildcard $(js_src_dir)/*.js)
js_bundles  := $(js_sources:$(js_src_dir)/%.js=assets/js/%.bundle.js)
js_srcmaps  := $(js_sources:$(js_src_dir)/%.js=assets/js/%.bundle.js.map)
zip_release := dist/w_cms_$(GIT_VERSION).zip

# Default target. This executes everything targets needed to get a fully
# working development environment.
.PHONY: all
all: $(PREV_ENV_FILE) vendor build

# Build generated files.
.PHONY: build
build: VERSION $(PREV_ENV_FILE) $(js_bundles)

# Run webpack in watch mode.
.PHONY: watch
watch: ENV := dev
watch:
	$(MAKE) .watch ENV=$(ENV)

.PHONY: .watch
.watch: $(PREV_ENV_FILE) node_modules
	webpack --env $(ENV) --watch

# Create a new release and upload it on GitHub.
.PHONY: release
release: node_modules
	release-it

# Inform Sentry of a new release.
.PHONY: sentryrelease
sentryrelease: ENV := dist
sentryrelease:
	$(MAKE) .sentryrelease ENV=$(ENV)

.PHONY: .sentryrelease
.sentryrelease: $(PREV_ENV_FILE) build $(js_srcmaps)
	sentry-cli releases new $(GIT_VERSION)
	sentry-cli releases set-commits $(GIT_VERSION) --auto
	sentry-cli releases files $(GIT_VERSION) upload-sourcemaps assets/js --url-prefix '~/assets/js' --rewrite
	sentry-cli releases finalize $(GIT_VERSION)

# Generate the distribution files.
.PHONY: dist
dist: ENV := dist
dist:
	$(MAKE) .dist ENV=$(ENV)

.PHONY: .dist
.dist: distclean $(zip_release) $(js_srcmaps)

dist/w_cms_%.zip: all
	@echo Building Zip release...
	mkdir -p $(dir $@)
# Include git tracked files (everything except ignored) + needed files.
	zip -r $@ \
		$(shell git ls-tree -r HEAD --name-only) \
		VERSION \
		assets/js \
		vendor \
		-x "*test*" \
		-x "*docs*"
# Include non-empty git tracked directories (to keep dir permissions).
	zip $@ $(shell git ls-tree -r HEAD --name-only -d)
# Remove non-useful files.
	zip -d $@ \
		"$(js_src_dir)/*" \
		$(js_srcmaps) \
		.default.env \
		.gitignore \
		.release-it.json \
		Makefile \
		"composer*" \
		"package*" \
		webpack.config.js

# Generate the js bundles (and sourcemaps).
assets/js/%.bundle.js assets/js/%.bundle.js.map: $(js_src_dir)/%.js node_modules webpack.config.js
	@echo Building JS Bundles...
	mkdir -p $(dir $@)
	webpack $< -o $@ --env $(ENV) $(WEBPACK_FLAGS)

# Generate a .env file if none is present.
.env:
	cp .default.env $@

# Generate the VERSION file.
VERSION: .FORCE
ifneq ($(CUR_VERSION),$(GIT_VERSION))
	echo $(GIT_VERSION) > $@
endif

# Trick to force rebuild if the build environment has changed.
ifneq ($(PREV_ENV),$(ENV))
$(PREV_ENV_FILE): touch
ifdef PREV_ENV
	@echo Build env has changed !
else
	@echo New build env.
endif
	mkdir -p $(dir $@)
	echo $(ENV) > $@
endif

# Install PHP dependencies.
vendor: composer.json composer.lock
	@echo Installing PHP dependencies...
	composer install $(COMPOSER_FLAGS)

# Install JS dependencies.
node_modules: package.json package-lock.json
	@echo Installing JS dependencies...
	npm install --loglevel=error

# Clean files generated by `make all`.
.PHONY: clean
clean: buildclean
	@echo Cleaning make artifacts...
	rm -rf vendor
	rm -rf node_modules
	rm -rf VERSION

# Clean files generated by `make dist`.
.PHONY: distclean
distclean:
	@echo Cleaning dist artifacts...
	rm -rf dist

# Clean files generated by `make build`.
.PHONY: buildclean
buildclean:
	@echo Cleaning build artifacts...
	rm -rf $(js_bundles)
	rm -rf $(js_srcmaps)
	rm -rf $(build_dir)

# Touch files affected by the build environment to force the execution
# of the corresponding targets.
.PHONY: touch
touch:
	touch $(js_sources)
	touch composer.json

# Special (fake) target to always run a target but have Make consider
# this updated if it was actually rewritten (a .PHONY target is always
# considered new).
.FORCE: ;