-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
139 lines (107 loc) · 4.88 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
TEMPLATE_DIR := templates
SASS_DIR := sass
JS_DIR := js
ASSET_DIR := assets
ARCHIVE_DIR := archive
BUILD_DIR := build
NODE_BIN_DIR := ./node_modules/.bin
MUSTACHE_FILES := $(wildcard $(TEMPLATE_DIR)/[!_]*.mustache)
MUSTACHE_PARTIALS := $(wildcard $(TEMPLATE_DIR)/_*.mustache)
YAML_FILES := $(wildcard $(TEMPLATE_DIR)/*.yaml)
# Check for missing Mustache/YAML files
MUSTACHE_BASENAMES := $(basename $(MUSTACHE_FILES))
YAML_BASENAMES := $(basename $(YAML_FILES))
TEMPLATE_PAIRS := $(filter $(YAML_BASENAMES), $(MUSTACHE_BASENAMES))
SINGLE_FILES := $(strip $(addsuffix .yaml,$(filter-out $(TEMPLATE_PAIRS),$(YAML_BASENAMES))) \
$(addsuffix .mustache,$(filter-out $(TEMPLATE_PAIRS),$(MUSTACHE_BASENAMES))))
$(if $(SINGLE_FILES),$(error Some Mustache/YAML files are missing. "$(SINGLE_FILES)" cannot be built),)
$(if $(wildcard $(TEMPLATE_DIR)/index.mustache),, \
$(error $(TEMPLATE_DIR)/index.mustache and $(TEMPLATE_DIR)/index.yaml are missing))
# Place HTML files in their own directory for clean URLs (except for the root
# index.html). For example, "about.mustache" will be built as
# "/about/index.html" instead of "/about.html".
HTML_BUILD := $(foreach slug,$(filter-out index,$(notdir $(MUSTACHE_BASENAMES))),\
$(BUILD_DIR)/$(slug)/index.html)
HTML_BUILD += $(BUILD_DIR)/index.html
JS_FILES := $(wildcard $(JS_DIR)/*.js)
JS_BUILD := $(BUILD_DIR)/script.js
SASS_FILE := $(SASS_DIR)/main.scss
SASS_PARTIALS := $(wildcard $(SASS_DIR)/*/_*.scss)
CSS_BUILD := $(BUILD_DIR)/style.css
ASSET_LINKS := $(patsubst $(ASSET_DIR)/%,$(BUILD_DIR)/%,$(wildcard $(ASSET_DIR)/*))
ARCHIVE_LINKS := $(BUILD_DIR)/2016 $(BUILD_DIR)/2017
PROGRAM_DEPS := npm
MISSING_DEPS := $(strip $(foreach dep,$(PROGRAM_DEPS),\
$(if $(shell command -v $(dep) 2> /dev/null),,$(dep))))
site: deps $(BUILD_DIR) 2018-soon $(ARCHIVE_LINKS)
2018-soon: $(HTML_BUILD) $(CSS_BUILD) $(JS_BUILD) $(ASSET_LINKS)
$(BUILD_DIR):
mkdir $@
# MUSTACHE_PARTIALS are added as a prerequisite so that non-partials are rebuilt
# when partials are modified.
# Partials' filenames must have just one dot and one underscore to match the regex.
%.html: %.mustache %.yaml $(MUSTACHE_PARTIALS)
node -e " \
var fs = require('fs'), partials = {}; \
'$(MUSTACHE_PARTIALS)'.split(' ').forEach(function(p) { \
partials[p.match(/_[^./]+/)[0]] = fs.readFileSync(p, 'utf8'); }); \
fs.writeFileSync('$@', require('mustache').render( \
fs.readFileSync('$<', 'utf8'), \
require('js-yaml').safeLoad(fs.readFileSync('$*.yaml')), \
partials));"
$(NODE_BIN_DIR)/html-beautify -f $@ -r
$(BUILD_DIR)/%.html: $(TEMPLATE_DIR)/%.html
mv $^ $@
$(BUILD_DIR)/%/index.html: $(TEMPLATE_DIR)/%.html
mkdir -p $(BUILD_DIR)/$*
mv $^ $@
# The prerequesite SASS_PARTIALS ensures that CSS_BUILD is rebuilt when
# partials are modified.
$(CSS_BUILD): $(SASS_FILE) $(SASS_PARTIALS)
$(NODE_BIN_DIR)/node-sass --output-style compressed $< > $@
$(NODE_BIN_DIR)/postcss --use autoprefixer $@ -o $@
$(JS_BUILD): $(JS_FILES)
cat $(JS_FILES) > $(JS_BUILD)
$(ASSET_LINKS):
ln -s ../$(ASSET_DIR)/$(@F) $(BUILD_DIR)
$(ARCHIVE_LINKS): $(BUILD_DIR)/% :
ln -s ../$(ARCHIVE_DIR)/$* $(BUILD_DIR)
clean:
rm -rf $(BUILD_DIR)
prod: site
$(if $(REPO),,$(error Usage: make prod REPO=[directory]))
$(if $(findstring true,$(shell cd $(REPO) && git rev-parse --is-inside-work-tree 2> /dev/null)),,\
$(error $(REPO) is not the work tree of a Git repository, will not copy files))
$(if $(shell cd $(REPO) && git status --porcelain), \
$(error $(REPO) is not clean, commit or stash changes before copying files),)
find -L $(BUILD_DIR) -maxdepth 1 -type l -exec rm {} +
rsync -CavhL --del --exclude README.md --exclude LICENSE --exclude CNAME $(BUILD_DIR)/ $(REPO)
watch: site
@echo "Listening for changes..."
@$(NODE_BIN_DIR)/chokidar "$(TEMPLATE_DIR)/*.mustache" "$(TEMPLATE_DIR)/*.yaml" \
"$(SASS_DIR)" "$(JS_DIR)" "$(ASSET_DIR)" "$(LIVE_DIR)" \
--silent -c "echo; date +'%-l:%M:%S%P'; make --no-print-directory"
help:
@echo 'The Los Altos Hacks website'
@echo
@echo 'Usage: make [target...]'
@echo
@echo 'Available targets:'
@echo ' site Build the whole site (default target)'
@echo ' clean Delete build files'
@echo ' watch Rebuild the site when files change'
@echo ' prod REPO=[directory] Build site and copy to production repository'
@echo ' help Show this help dialog'
deps:
$(if $(MISSING_DEPS),$(error Dependencies missing: $(MISSING_DEPS)),)
$(if $(findstring MISSING,$(shell npm outdated)), \
$(info Some Node packages are missing. Running npm install...) \
npm install,)
.PHONY: site 2018-soon clean prod watch help deps
# Disable implicit rules to speed up processing and declutter debug output
.SUFFIXES:
%: %,v
%: RCS/%,v
%: RCS/%
%: s.%
%: SCCS/s.%