forked from apache/cordova-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
304 lines (248 loc) · 8.7 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
# Function Reference:
# https://www.gnu.org/software/make/manual/html_node/Text-Functions.html
# https://www.gnu.org/software/make/manual/html_node/File-Name-Functions.html
# Variable Reference:
# https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html
# Makefile's own config
ifeq ($(OS),Windows_NT)
WINDOWS=1
endif
ifdef WINDOWS
SHELL = cmd
JEKYLL = bundle.bat exec jekyll
CAT = type
LS = ls
else
SHELL = sh
JEKYLL = bundle exec jekyll
CAT = cat
LS = ls
endif
# macros
slugify = $(subst /,_,$(subst .,-,$(1)))
slug2language = $(subst /,,$(dir $(subst _,/,$(1))))
slug2version = $(subst -,.,$(notdir $(subst _,/,$(1))))
ifdef WINDOWS
copydir = xcopy "$(subst /,\,$(1))" "$(subst /,\,$(2))" /E /I
else
copydir = cp -r $(1) $(2)
endif
ifdef WINDOWS
copyfile = copy "$(subst /,\,$(1))" "$(subst /,\,$(2))"
else
copyfile = cp $(1) $(2)
endif
ifdef WINDOWS
makedir = mkdir $(subst /,\,$(1))
else
makedir = mkdir -p $(1)
endif
ifdef WINDOWS
printfile = type $(subst /,\,$(1))
else
printfile = cat $(1)
endif
# constants
EMPTY =
SPACE = $(EMPTY) $(EMPTY)
COMMA = ,
VERSION_VAR_NAME = latest_docs_version
# paths and files
BIN_DIR = tools/bin
NODE_BIN_DIR = ./node_modules/.bin
SRC_DIR = www
DEV_DIR = build-dev
PROD_DIR = build-prod
CONFIG_DIR = conf
DOCS_DIR = $(SRC_DIR)/docs
DATA_DIR = $(SRC_DIR)/_data
TOC_DIR = $(DATA_DIR)/toc
STATIC_DIR = $(SRC_DIR)/static
CSS_SRC_DIR = $(STATIC_DIR)/css-src
CSS_DEST_DIR = $(STATIC_DIR)/css
PLUGINS_SRC_DIR = $(STATIC_DIR)/plugins
PLUGINS_DEST_DIR = $(STATIC_DIR)/js
# executables
NODE = node
GULP = $(NODE_BIN_DIR)/gulp
LESSC = $(NODE_BIN_DIR)/lessc
SASSC = $(NODE_BIN_DIR)/node-sass
BROWSERIFY = $(NODE_BIN_DIR)/browserify
UGLIFY = $(NODE_BIN_DIR)/uglifyjs
# replace slashes in executables on Windows
ifdef WINDOWS
GULP := $(subst /,\,$(GULP))
LESSC := $(subst /,\,$(LESSC))
SASSC := $(subst /,\,$(SASSC))
BROWSERIFY := $(subst /,\,$(BROWSERIFY))
UGLIFY := $(subst /,\,$(UGLIFY))
endif
# existing files
MAIN_CONFIG = $(CONFIG_DIR)/_config.yml
DEV_CONFIG = $(CONFIG_DIR)/_dev.yml
PROD_CONFIG = $(CONFIG_DIR)/_prod.yml
DOCS_EXCLUDE_CONFIG = $(CONFIG_DIR)/_nodocs.yml
FETCH_CONFIG = $(DATA_DIR)/fetched-files.yml
REDIRECTS_FILE = $(DATA_DIR)/redirects.yml
PLUGINS_SRC = $(PLUGINS_SRC_DIR)/app.js
VERSION_FILE = VERSION
FETCH_SCRIPT = $(BIN_DIR)/fetch_docs.js
# NOTE:
# the .scss files are separate because they combine into MAIN_STYLE_FILE,
# which includes them on its own, and the SCSS compiler takes care of them;
# because of this, there is also no .scss -> .css pattern rule
ifdef WINDOWS
SCSS_SRC = $(subst $(PWD)/,,$(subst \,/,$(shell cd $(CSS_SRC_DIR) && dir *.scss /S /B)))
STYLES_SRC = $(subst $(PWD)/,,$(subst \,/,$(shell cd $(CSS_SRC_DIR) && dir *.less *.css /S /B)))
else
SCSS_SRC = $(shell find $(CSS_SRC_DIR) -name "*.scss")
STYLES_SRC = $(shell find $(CSS_SRC_DIR) -name "*.less" -or -name "*.css")
endif
LANGUAGES = $(shell $(LS) $(DOCS_DIR))
LATEST_DOCS_VERSION = $(strip $(shell $(CAT) $(VERSION_FILE)))
NEXT_DOCS_VERSION = $(shell $(NODE) $(BIN_DIR)/nextversion.js $(LATEST_DOCS_VERSION))
NEXT_DOCS_VERSION_SLUG = $(call slugify,$(NEXT_DOCS_VERSION))
DEV_DOCS = $(addprefix $(DOCS_DIR)/,$(addsuffix /dev,$(LANGUAGES)))
DEV_DOCS_TOCS = $(addprefix $(TOC_DIR)/,$(addsuffix _dev-src.yml, $(LANGUAGES)))
# generated files
VERSION_CONFIG = $(CONFIG_DIR)/_version.yml
DEFAULTS_CONFIG = $(CONFIG_DIR)/_defaults.yml
DOCS_VERSION_DATA = $(DATA_DIR)/docs-versions.yml
DOCS_PAGE_LIST = $(DATA_DIR)/all-pages.yml
PLUGINS_APP = $(PLUGINS_DEST_DIR)/plugins.js
MAIN_STYLE_FILE = $(CSS_DEST_DIR)/main.css
STYLES = $(MAIN_STYLE_FILE) $(addsuffix .css,$(basename $(subst $(CSS_SRC_DIR),$(CSS_DEST_DIR),$(STYLES_SRC))))
DOCS_VERSION_DIRS = $(filter-out %.md,$(wildcard $(DOCS_DIR)/**/*))
DOCS_VERSION_SLUGS = $(call slugify,$(subst $(DOCS_DIR)/,,$(DOCS_VERSION_DIRS)))
TOC_FILES = $(addprefix $(TOC_DIR)/,$(addsuffix -gen.yml,$(DOCS_VERSION_SLUGS)))
FETCH_FLAGS = --config $(FETCH_CONFIG) --docsRoot $(DOCS_DIR)
FETCHED_FILES = $(shell $(NODE) $(FETCH_SCRIPT) $(FETCH_FLAGS) --dump)
NEXT_DOCS = $(addprefix $(DOCS_DIR)/,$(addsuffix /$(NEXT_DOCS_VERSION),$(LANGUAGES)))
NEXT_DOCS_TOCS = $(addprefix $(TOC_DIR)/,$(addsuffix _$(NEXT_DOCS_VERSION_SLUG)-src.yml, $(LANGUAGES)))
# other variables
# NOTE:
# the order of config files matters to Jekyll
JEKYLL_CONFIGS = $(MAIN_CONFIG) $(DEFAULTS_CONFIG) $(VERSION_CONFIG)
JEKYLL_FLAGS =
BUILD_DATA = $(DOCS_VERSION_DATA) $(DOCS_PAGE_LIST) $(TOC_FILES)
# convenience targets
help usage default:
@echo ""
@echo "Usage:"
@echo ""
@echo " make build: build site with dev config"
@echo " make install: install dependencies"
@echo ""
@echo " make data: generate data files (Generated ToCs, $(DOCS_VERSION_DATA), $(DOCS_PAGE_LIST))"
@echo " make configs: generate Jekyll configs ($(DEFAULTS_CONFIG), $(VERSION_CONFIG))"
@echo " make styles: generate CSS"
@echo " make plugins: generate plugins app ($(PLUGINS_APP))"
@echo ""
@echo " make snapshot: copy dev docs to $(LATEST_DOCS_VERSION) docs"
@echo " make newversion: create $(NEXT_DOCS_VERSION) docs from dev docs"
@echo ""
@echo " make clean: remove all generated output"
@echo " make nuke: run 'make clean' and remove all dependencies"
@echo ""
@echo "Arguments:"
@echo ""
@echo " NODOCS: (defined or undefined) - excludes docs from build"
@echo " PROD: (defined or undefined) - uses production config instead of dev config"
@echo ""
fetch: $(FETCHED_FILES)
data: $(BUILD_DATA)
configs: $(DEFAULTS_CONFIG) $(VERSION_CONFIG)
styles: $(STYLES)
plugins: $(PLUGINS_APP)
toc: $(TOC_FILES)
ifdef PROD
JEKYLL_CONFIGS += $(PROD_CONFIG)
ifdef NODOCS
$(error Cannot ignore docs during a production build)
endif
else
JEKYLL_CONFIGS += $(DEV_CONFIG)
JEKYLL_FLAGS += --trace
ifdef NODOCS
JEKYLL_CONFIGS += $(DOCS_EXCLUDE_CONFIG)
endif
endif
build: JEKYLL_FLAGS += --config $(subst $(SPACE),$(COMMA),$(strip $(JEKYLL_CONFIGS)))
build: $(JEKYLL_CONFIGS) $(FETCHED_FILES) $(BUILD_DATA) $(STYLES) $(PLUGINS_APP)
$(JEKYLL) build $(JEKYLL_FLAGS)
install:
bundle install
npm install
serve:
cd $(DEV_DIR) && python -m SimpleHTTPServer 8000
# doing this in Make in a cross-platform way is pretty ugly
snapshot: $(FETCHED_FILES)
$(GULP) snapshot
newversion: $(NEXT_DOCS) $(NEXT_DOCS_TOCS)
echo $(NEXT_DOCS_VERSION) > $(VERSION_FILE)
# real targets
$(FETCHED_FILES): $(FETCH_CONFIG) $(FETCH_SCRIPT)
$(NODE) $(FETCH_SCRIPT) $(FETCH_FLAGS)
# NOTE:
# the ">>" operator appends to a file in both CMD and SH
$(PLUGINS_APP): $(PLUGINS_SRC)
echo ---> $@
echo --->> $@
$(BROWSERIFY) -t reactify -t envify $< | $(UGLIFY) >> $@
$(DOCS_VERSION_DATA): $(BIN_DIR)/gen_versions.js $(DOCS_DIR)
$(NODE) $(BIN_DIR)/gen_versions.js $(DOCS_DIR) > $@
$(DOCS_PAGE_LIST): $(BIN_DIR)/gen_pages_dict.js $(FETCHED_FILES) $(REDIRECTS_FILE) $(SRC_DIR)
$(NODE) $(BIN_DIR)/gen_pages_dict.js \
--siteRoot $(SRC_DIR) \
--redirectsFile $(REDIRECTS_FILE) \
--latestVersion $(LATEST_DOCS_VERSION) \
--languages $(subst $(SPACE),$(COMMA),$(LANGUAGES)) \
> $@
$(DEFAULTS_CONFIG): $(BIN_DIR)/gen_defaults.js $(VERSION_FILE) $(DOCS_DIR)
$(NODE) $(BIN_DIR)/gen_defaults.js $(DOCS_DIR) "$(LATEST_DOCS_VERSION)" > $@
$(VERSION_CONFIG): $(VERSION_FILE)
sed -e "s/^/$(VERSION_VAR_NAME): /" < $< > $@
$(MAIN_STYLE_FILE): $(SCSS_SRC)
# pattern rules
$(DOCS_DIR)/%/$(NEXT_DOCS_VERSION): $(DOCS_DIR)/%/dev
$(call copydir,$^,$@)
ifndef WINDOWS
touch $(DOCS_DIR)
endif
$(TOC_DIR)/%_$(NEXT_DOCS_VERSION_SLUG)-src.yml: $(TOC_DIR)/%_dev-src.yml $(DOCS_DIR)
$(call copyfile,$<,$@)
$(TOC_DIR)/%_dev-gen.yml: $(FETCHED_FILES)
$(TOC_DIR)/%-gen.yml: $(TOC_DIR)/%-src.yml $(BIN_DIR)/augment_toc.js $(DOCS_DIR)
$(NODE) $(BIN_DIR)/augment_toc.js --srcToc $< --srcRoot $(DOCS_DIR)/$(call slug2language,$*)/$(call slug2version,$*) > $@
# NOTE:
# $(@D) means "directory part of target"
$(CSS_DEST_DIR)/%.css: $(CSS_SRC_DIR)/%.less
-$(call makedir,$(@D))
echo ---> $@
echo --->> $@
$(LESSC) $< >> $@
$(CSS_DEST_DIR)/%.css: $(CSS_SRC_DIR)/%.scss
-$(call makedir,$(@D))
echo ---> $@
echo --->> $@
$(SASSC) $< >> $@
$(CSS_DEST_DIR)/%.css: $(CSS_SRC_DIR)/%.css
-$(call makedir,$(@D))
echo ---> $@
echo --->> $@
$(call printfile,$<) >> $@
# maintenance
clean:
$(RM) $(VERSION_CONFIG)
$(RM) $(DEFAULTS_CONFIG)
$(RM) $(DOCS_PAGE_LIST)
$(RM) $(DOCS_VERSION_DATA)
$(RM) -r $(PROD_DIR) $(DEV_DIR)
$(RM) $(TOC_FILES)
$(RM) $(PLUGINS_APP)
$(RM) -r $(CSS_DEST_DIR)
$(RM) $(FETCHED_FILES)
nuke: clean
$(RM) -r node_modules
$(RM) Gemfile.lock
.PHONY: clean usage help default build fetch $(DEV_DOCS)