summaryrefslogtreecommitdiff
path: root/Makefile
blob: 1a0776379da959435e1317bfd8d337c55ab0a76f (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
# The idea here is to be able to re-generate the exact Javascript
# files as they exist in git.  This means munging whitespace in weird
# ways, using specific versions of various JS minimizers...

# Dependencies:
#  - bsdtar
#  - coreutils
#  - gitget <https://git.parabola.nu/packages/libretools.git/tree/src/gitget/gitget>
#  - java (for closure-compiler)
#  - node (for UglifyJS 1)
#  - npm (for UglifyJS 2)
#  - patch
#  - perl
#  - pngcrush
#  - sed
#  - wget

# Where are we?
dl-cache = ../download-cache
www = $(dl-cache)/www
bin = $(dl-cache)/bin

# What versions of 3rd party libraries are we using?
jquery-ver=1.8.3
bootstrap-ver=2.1.1
tablesorter-ver=2.7
d3-ver=3.0.6
konami-ver=c0f686e647765860ff4d2fcb7b48122785432b75

targets = \
	sitestatic/rss.png \
	sitestatic/rss@2x.png \
	sitestatic/bootstrap-typeahead.js \
	sitestatic/homepage.js \
	sitestatic/jquery-$(jquery-ver).min.js \
	sitestatic/jquery.tablesorter-$(tablesorter-ver).js \
	sitestatic/jquery.tablesorter-$(tablesorter-ver).min.js \
	sitestatic/konami.min.js \
	visualize/static/d3-$(d3-ver).js \
	visualize/static/d3-$(d3-ver).min.js \
	retro/static/2013/bootstrap-typeahead.min.1aacd3d7f4db.js \
	retro/static/2013/konami.min.e165c814457d.js


# The base rules

all: $(targets)
.PHONY: all

js-basenames = $(sort $(patsubst %.min,%,$(patsubst %.js,%,$(filter %.js,$(targets)))))
generated = $(sort $(targets) $(foreach f,$(js-basenames),$f.js $f.min.js))
clean:
	rm -f -- $(generated)
.PHONY: clean

# Make directories
$(dl-cache) $(bin):
	mkdir -p '$@'

# Don't have non-minimized .js stick around unless we asked for them.
.INTERMEDIATE: $(filter-out $(targets),$(generated))

# Turn on sane error handling
.DELETE_ON_ERROR:


# How to download files

  mangle = $(subst %,^25,$(subst :,^3A,$(subst =,^3D,$(subst ^,^5E,$1))))
unmangle = $(subst ^5E,^,$(subst ^3D,=,$(subst ^3A,:,$(subst ^25,%,$1))))

$(www)/http/%:
	mkdir -p '$(@D)'
	wget 'http://$(call unmangle,$*)' -O '$@'
	test -f '$@' && touch '$@'

$(www)/https/%:
	mkdir -p '$(@D)'
	wget 'https://$(call unmangle,$*)' -O '$@'
	test -f '$@' && touch '$@'

$(www)/git/%:
	mkdir -p '$(@D)'
	gitget checkout 'git://$(call unmangle,$*)' '$@' || { rm -rf -- '$@'; false; }
	test -d '$@' && touch '$@' || { rm -rf -- '$@'; false; }


# How to install any of the minifiers in use

# Any of the UglifyJS 1.3.x releases will produce the same output for these inputs
$(bin)/uglifyjs-1.3: $(www)/git/github.com/mishoo/UglifyJS.git\#tag=v1.3.5 | $(bin)
	printf '%s\n' '#!/bin/sh' 'exec $</bin/uglifyjs --no-copyright --ascii --max-line-len 0 "$$@"' | install -m755 /dev/stdin '$@'

# Any of the UglifyJS 2.2.x releases will produce the same output for these inputs
$(dl-cache)/uglifyjs-2.2: $(www)/git/github.com/mishoo/UglifyJS2.git\#tag=v2.2.5 | $(dl-cache)
	cp -r $< $@ || { rm -rf -- '$@'; false; }
	cd $@ && npm install || { rm -rf -- '$@'; false; }
$(bin)/uglifyjs-2.2: $(dl-cache)/uglifyjs-2.2 | $(bin)
	printf '%s\n' '#!/bin/sh' 'exec $</bin/uglifyjs --mangle --compress -- "$$@"' | install -m755 /dev/stdin '$@'

# Closure compiler 20121212 produces the same output
$(dl-cache)/unzip/compiler-20121212/%: $(www)/https/web.archive.org/web/20151226141014/https^3A//closure-compiler.googlecode.com/files/compiler-20121212.tar.gz
	mkdir -p '$(@D)'
	bsdtar xfO '$<' '$*' > '$@'
$(bin)/closure-compiler: $(dl-cache)/unzip/compiler-20121212/compiler.jar | $(bin)
	printf '%s\n' '#!/bin/sh' 'exec java -jar $< "$$@"' | install -m755 /dev/stdin '$@'


# The meat of the Makefile

# Downloaded images
$(dl-cache)/unzip/Feedicons_v.2/%: $(www)/https/web.archive.org/web/20120514074507/http^3A//www.zeusboxstudio.com/file_download/1/Feedicons_v.2.zip
	mkdir -p '$(@D)'
	bsdtar xfO $< 'Feedicons v.2/$*' > '$@'
sitestatic/rss.png: $(dl-cache)/unzip/Feedicons_v.2/RSS_16.png
	cp $< $@
sitestatic/rss@2x.png: $(dl-cache)/unzip/Feedicons_v.2/RSS_32.png
	pngcrush $< $@

# Non-minified JavaScript
sitestatic/bootstrap-typeahead.js: sitestatic/%: $(www)/https/raw.github.com/twitter/bootstrap/v$(bootstrap-ver)/js/% Makefile.d/%.patch
	cp $< $@
	patch -i Makefile.d/$*.patch $@
sitestatic/jquery-$(jquery-ver).js: sitestatic/%: $(www)/http/code.jquery.com/%
	cp $< $@
sitestatic/jquery.tablesorter-$(tablesorter-ver).js: $(www)/https/raw.github.com/Mottie/tablesorter/v$(tablesorter-ver)/js/jquery.tablesorter.js
	cp $< $@
sitestatic/konami.js: sitestatic/%: $(www)/https/raw.github.com/snaptortoise/konami-js/$(konami-ver)/% Makefile.d/%.patch
	cp $< $@
	patch -i Makefile.d/$*.patch $@
visualize/static/d3-$(d3-ver).js: %: $(www)/https/raw.github.com/mbostock/d3/v$(d3-ver)/d3.js
	cp $< $@

# JavaScript minification
sitestatic/bootstrap-typeahead.min.js: %.min.js: $(bin)/uglifyjs-1.3 %.js
	$^ > $@
sitestatic/jquery-$(jquery-ver).min.js: sitestatic/%.min.js: sitestatic/%.js Makefile.d/%.min.js.preamble
	{ cat Makefile.d/$*.min.js.preamble && $(bin)/uglifyjs-1.3 $<; } > $@
sitestatic/jquery.tablesorter-$(tablesorter-ver).min.js: sitestatic/%.min.js: sitestatic/%.js Makefile.d/%.min.js.preamble $(bin)/closure-compiler
	@# The tr/sed is to turn all of the newlines except for the last one into spaces
	{ cat Makefile.d/$*.min.js.preamble && $(bin)/closure-compiler $< | tr '\n' ' ' | sed 's, $$,\n,'; } > $@
sitestatic/konami.min.js: %.min.js: $(bin)/uglifyjs-2.2 %.js
	@# The sed is to insert newlines and whitespace at the correct places; presumably to match Dan copy/pasting into an editor.
	$^ | sed -r -e 's:e\.iphone\.stop_x|s=this\.tap===:\n\t&:g' -e 's,return"string",\n&,' | sed -e 's,;$$,,' -e '$$s,$$,\n,' > $@
visualize/static/d3-$(d3-ver).min.js: %.min.js: $(bin)/uglifyjs-2.2 %.js
	$^ > $@

# Files that make use of that minified JavaScript
retro/static/2013/konami.min.e165c814457d.js: sitestatic/konami.min.js
	cp $< $@
retro/static/2013/bootstrap-typeahead.min.1aacd3d7f4db.js: sitestatic/bootstrap-typeahead.min.js
	@# Trim trailing semicolon and newline
	< $< sed 's/;$$//' | perl -pe 'chomp if eof' > $@
sitestatic/homepage.js: sitestatic/bootstrap-typeahead.min.js sitestatic/konami.min.js Makefile.d/homepage.js.in
	{ \
		cat sitestatic/bootstrap-typeahead.min.js && \
		echo && \
		sed -e 's,^\s*,,' -e 's,^return.*,&;,' sitestatic/konami.min.js && \
		echo && \
		cat Makefile.d/homepage.js.in ; \
	} > $@