-
Notifications
You must be signed in to change notification settings - Fork 314
/
make
executable file
·295 lines (253 loc) · 8.02 KB
/
make
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
#!/usr/bin/env bash
# shellcheck disable=SC2230
##############################################
## Dependency Check
which gulp > /dev/null
GULP_INSTALLED=$?
# Some things are part of dev dependencies (js-yaml, uglify, etc.)
PATH="./node_modules/.bin:$PATH"
which gifsicle > /dev/null
GIFSICLE_INSTALLED=$?
which md5 > /dev/null
MD5_INSTALLED=$?
if [ $MD5_INSTALLED -ne 0 ]; then
which md5sum > /dev/null
MD5_INSTALLED=$?
fi
which pngcrush > /dev/null
PNGCRUSH_INSTALLED=$?
which svgo > /dev/null
SVGO_INSTALLED=$?
which jpegtran > /dev/null
JPEGTRAN_INSTALLED=$?
which jq > /dev/null
JQ_INSTALLED=$?
which uglifycss > /dev/null
UGLIFYCSS_INSTALLED=$?
which uglifyjs > /dev/null
UGLIFYJS_INSTALLED=$?
which magick > /dev/null
IMAGEMAGICKV7_INSTALLED=$?
which convert > /dev/null
IMAGEMAGICK_INSTALLED=$?
which js-yaml > /dev/null
JS_YAML_INSTALLED=$?
if [ $GULP_INSTALLED -ne 0 ] || \
[ $GIFSICLE_INSTALLED -ne 0 ] || \
[ $PNGCRUSH_INSTALLED -ne 0 ] || \
[ $SVGO_INSTALLED -ne 0 ] || \
[ $JPEGTRAN_INSTALLED -ne 0 ] || \
[ $MD5_INSTALLED -ne 0 ] || \
[ $JQ_INSTALLED -ne 0 ] || \
[ $UGLIFYCSS_INSTALLED -ne 0 ] || \
[ $UGLIFYJS_INSTALLED -ne 0 ] || \
[ $IMAGEMAGICK_INSTALLED -ne 0 ] || \
[ $JS_YAML_INSTALLED -ne 0 ]; then
echo "The following software is required:"
echo
echo " gulp: $([ $GULP_INSTALLED -eq 0 ] && echo "Installed" || echo "Not Installed")"
echo " jq: $([ $JQ_INSTALLED -eq 0 ] && echo "Installed" || echo "Not Installed")"
echo " gifsicle: $([ $GIFSICLE_INSTALLED -eq 0 ] && echo "Installed" || echo "Not Installed")"
echo " md5/md5sum: $([ $MD5_INSTALLED -eq 0 ] && echo "Installed" || echo "Not Installed")"
echo " pngcrush: $([ $PNGCRUSH_INSTALLED -eq 0 ] && echo "Installed" || echo "Not Installed")"
echo " svgo: $([ $SVGO_INSTALLED -eq 0 ] && echo "Installed" || echo "Not Installed")"
echo " jpegtran: $([ $JPEGTRAN_INSTALLED -eq 0 ] && echo "Installed" || echo "Not Installed")"
echo " uglifycss: $([ $UGLIFYCSS_INSTALLED -eq 0 ] && echo "Installed" || echo "Not Installed")"
echo " uglifyjs: $([ $UGLIFYJS_INSTALLED -eq 0 ] && echo "Installed" || echo "Not Installed")"
echo "imagemagick: $([ $IMAGEMAGICK_INSTALLED -eq 0 ] && echo "Installed" || echo "Not Installed")"
echo " js-yaml: $([ $JS_YAML_INSTALLED -eq 0 ] && echo "Installed" || echo "Not Installed")"
exit 1
fi
##############################################
## Preamble
set -o errexit
set -o nounset
set -o pipefail
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CACHE_DIR="${__dir}/.cache"
if ! [ -d "$CACHE_DIR" ]; then
mkdir -p "$CACHE_DIR"
fi
##############################################
## Utility
function header {
echo -e "\\033[96m==[ ${1} ]==\\033[0m"
}
function update_manifest () {
FILE_NAME=${1#"dist/"}
DIR_NAME="$(dirname "$FILE_NAME")/"
EXTENSION=${FILE_NAME##*.} # Assume it's a single-dot extension, i.e. no .tar.gz
FILE_PREFIX=${FILE_NAME%%".$EXTENSION"}
FILE_SHA=$(shasum "$1" | awk '{print $1}' | cut -b 1-10)
OUTPUT="${DIR_NAME#"./"}$(basename "$FILE_PREFIX")-${FILE_SHA}.${EXTENSION}"
(cat dist/manifest.json 2>/dev/null || echo "{}") | jq ".[\"$FILE_NAME\"]=\"${OUTPUT}\"" > dist/_tmp.json
mv dist/_tmp.json dist/manifest.json
echo "$OUTPUT"
}
function md5_hash {
if which md5 > /dev/null; then
# Prefer `md5`
md5 -q "$1"
else
# Fall back to `md5sum`, but cut the first field
md5sum "$1" | cut -d' ' -f 1
fi
}
##############################################
## Tasks
# Uglify and compress CSS to the dist folder.
function css () {
header "css"
uglifycss src/parrot.css > dist/assets/parrot.css
mv dist/assets/parrot.css "dist/$(update_manifest dist/assets/parrot.css)"
}
# Uglify and compress JavaScript to the dist folder.
function js () {
header "js"
for f in src/*.js; do
distfile="dist/assets/${f/src\//}"
uglifyjs "$f" > "$distfile"
mv "$distfile" "dist/$(update_manifest "$distfile")"
done
}
# Copy and compress asset images to the dist folder.
function images () {
header "images"
cp src/favicon.ico dist/favicon.ico
while IFS= read -r -d '' FILE; do
CACHE_FILE="$CACHE_DIR/${FILE##"src/"}-$(md5_hash "$FILE")"
if ! [ -f "$CACHE_FILE" ]; then
case ${FILE##*.} in
"gif")
gifsicle --optimize=3 "$FILE" > "$CACHE_FILE"
;;
"jpg")
jpegtran -progressive -optimize -copy none -outfile "$CACHE_FILE" "$FILE"
;;
"png")
pngcrush -reduce -s "$FILE" "$CACHE_FILE"
;;
"svg")
svgo --input="$FILE" --output="$CACHE_FILE"
;;
*)
"Unknown file type: $FILE"
;;
esac
fi
cp "$CACHE_FILE" "dist/assets/${FILE##"src/"}"
done < <(find src \( -name '*.gif' -o -name '*.png' -o -name '*.jpg' -o -name '*.svg' \) -print0)
cp -r parrots dist/parrots
cp -r guests dist/guests
cp -r flags dist/flags
cp -r other-parrots dist/other-parrots
cp -r still dist/still
}
# Make gif's into zip files.
function compress () {
header "compress"
rm -f "$CACHE_DIR"/{parrots,guests,flags,other-parrots}.zip
printf " ~= Party or Die =~\\n~= cultofthepartyparrot.com =~" | zip -v -o -r -z "$CACHE_DIR/parrots.zip" ./parrots/*
checkzip parrots
cp "$CACHE_DIR/parrots.zip" "dist/parrots.zip"
mv dist/parrots.zip "dist/$(update_manifest dist/parrots.zip)"
printf " ~= Party or Die =~\\n~= cultofthepartyparrot.com =~" | zip -v -o -r -z "$CACHE_DIR/guests.zip" ./guests/*
checkzip guests
cp "$CACHE_DIR/guests.zip" "dist/guests.zip"
mv dist/guests.zip "dist/$(update_manifest dist/guests.zip)"
printf " ~= Party or Die =~\\n~= cultofthepartyparrot.com =~" | zip -v -o -r -z "$CACHE_DIR/flags.zip" ./flags/*
checkzip flags
cp "$CACHE_DIR/flags.zip" "dist/flags.zip"
mv dist/flags.zip "dist/$(update_manifest dist/flags.zip)"
printf " ~= Party or Die =~\\n~= cultofthepartyparrot.com =~" | zip -v -o -r -z "$CACHE_DIR/other-parrots.zip" ./other-parrots/*
checkzip other-parrots
cp "$CACHE_DIR/other-parrots.zip" "dist/other-parrots.zip"
mv dist/other-parrots.zip "dist/$(update_manifest dist/other-parrots.zip)"
}
function checkzip () {
local found_manifest zip_manifest
found_manifest="$(mktemp)"
zip_manifest="$(mktemp)"
find "$1" -name '*.gif' | sort > "$found_manifest"
unzip -l "$CACHE_DIR/$1.zip" | grep .gif | awk '{print $4}' | sort > "$zip_manifest"
diff "$found_manifest" "$zip_manifest"
}
# Run tests with gulp
function test () {
header "test"
gulp test
}
# Delete all the dist files.
function clean () {
header "clean"
rm -rf dist/*
mkdir -p dist/assets
}
# Delete all the cached compressed images
function clean-cache () {
header "clean-cache"
rm -rf "${CACHE_DIR:?}"/*
mkdir -p "$CACHE_DIR"
}
function render-still () {
sources=( parrots guests flags other-parrots )
for src in "${sources[@]}"; do
mkdir -p "still/$src/"
find "$src" -name '*.gif' -print0 |
while IFS= read -r -d '' f; do
bn="$(basename "$f")"
if [ $IMAGEMAGICKV7_INSTALLED -eq 0 ]; then
magick "${f}[0]" -coalesce "still/$src/${bn/.gif/.png}"
else
convert -coalesce "${f}[0]" "still/$src/${bn/.gif/.png}"
fi
done
done
}
function render-json () {
sources=( parrots guests flags other-parrots )
for src in "${sources[@]}"; do
js-yaml "$src.yaml" > "dist/$src.json"
done
}
##############################################
## GTD
function main () {
case ${1:-} in
"test")
test
;;
"clean")
clean
clean-cache
;;
"readme")
gulp render-readme
;;
"build")
header "build"
test
clean
render-still
js
css
images
compress
gulp render-humans
gulp render-web
cp parrots.yaml dist/
cp guests.yaml dist/
cp flags.yaml dist/
cp other-parrots.yaml dist/
render-json
;;
"verify-install")
echo "All dependencies are installed!"
;;
*)
echo "usage: $0 <test|clean|readme|build>"
;;
esac
}
# shellcheck disable=SC2068
main $@