-
Notifications
You must be signed in to change notification settings - Fork 365
/
build.sh
executable file
·273 lines (211 loc) · 7.67 KB
/
build.sh
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
#!/bin/bash
set -e -o pipefail
shopt -s nullglob
## CHECK ARGUMENTS
if [ $# -ne 1 ]; then
printf "expecting one argument to ./build.sh like this:\n\n ./build.sh prod\n ./build.sh dev\n\n"
exit 1
fi
case $1 in
prod)
echo "Running a PROD build.";
is_prod () { return 0; } ;;
dev)
echo "Running a DEV build.";
is_prod () { return 1; } ;;
*)
printf "expecting one argument to ./build.sh like this:\n\n ./build.sh prod\n ./build.sh dev\n\n";
exit 1 ;;
esac
## MAKE PAGE HTML
function makePageHtml {
cat <<EOF > $1
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>$2</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="A delightful language with friendly error messages, great performance, small assets, and no runtime exceptions.">
<meta name="robots" content="index, follow">
<link rel="shortcut icon" sizes="16x16 32x32 48x48 64x64 128x128 256x256" href="/favicon.ico">
<link rel="stylesheet" rel="preload" href="https://fonts.googleapis.com/css?family=IBM+Plex+Sans|Courier+Prime&display=swap">
<link rel="stylesheet" href="/assets/style.css">
<link rel="stylesheet" href="/assets/highlight/styles/default.css">
<script src="/assets/highlight/highlight.pack.js"></script>
</head>
<body>
<script type="text/javascript">
$(cat $3)
var app = Elm.Main.init({ flags: { width: window.innerWidth, height : window.innerHeight } });
</script>
</body>
</html>
EOF
}
## MAKE EXAMPLE HTML
# ARGS:
# $1 = _site/examples/NAME.html
# $2 = <title>
# $3 = NAME
# $4 = dependencies.json
# $5 = code
#
function makeExampleHtml {
cat <<EOF > $1
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>$2</title>
<link rel="shortcut icon" sizes="16x16 32x32 48x48 64x64 128x128 256x256" href="/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Try out Elm: A delightful language with friendly error messages, great performance, small assets, and no runtime exceptions.">
<link rel="stylesheet" rel="preload" href="https://fonts.googleapis.com/css?family=IBM+Plex+Sans|Courier+Prime&display=swap">
<link rel="stylesheet" href="/assets/editor-styles.css"/>
</head>
<body>
<svg xmlns="http://www.w3.org/2000/svg" style="display:none;">
<symbol id="logo" viewBox="-300 -300 600 600" fill="currentColor">
<g transform="scale(1 -1)">
<polygon points="-280,-90 0,190 280,-90" transform="translate(0 -210) rotate(0)"></polygon>
<polygon points="-280,-90 0,190 280,-90" transform="translate(-210 0) rotate(-90)"></polygon>
<polygon points="-198,-66 0,132 198,-66" transform="translate(207 207) rotate(-45)"></polygon>
<polygon points="-130,0 0,-130 130,0 0,130" transform="translate(150 0) rotate(0)"></polygon>
<polygon points="-191,61 69,61 191,-61 -69,-61" transform="translate(-89 239) rotate(0)"></polygon>
<polygon points="-130,-44 0,86 130,-44" transform="translate(0 106) rotate(-180)"></polygon>
<polygon points="-130,-44 0,86 130,-44" transform="translate(256 -150) rotate(-270)"></polygon>
</g>
</symbol>
</svg>
<main id="main"></main>
<textarea id="original" style="display:none;">$(cat $5)</textarea>
<script src="/assets/editor-codemirror.js"></script>
<script src="/assets/editor-custom-elements.js"></script>
<script src="/assets/editor-elm.js"></script>
<script>
window.addEventListener('load', function() {
var originalCode = document.getElementById('original').textContent;
main = Elm.Page.Editor.init({
node: document.getElementById('main'),
flags: {
name: "$3",
width: window.innerWidth,
height: window.innerHeight,
original: document.getElementById('original').textContent,
dependencies: $(cat $4)
}
});
main.ports.submitSource.subscribe(function(source) {
var editorNode = document.getElementById('editor');
var codeNode = document.getElementById('code');
codeNode.value = source;
editorNode.submit();
});
window.addEventListener("message", gotErrors, false);
function gotErrors(event) {
if (event.origin !== "https://elm.studio" && event.origin !== "https://social.elm.studio") return;
if (event.data == "SUCCESS") {
main.ports.gotSuccess.send(null);
} else {
var message = JSON.parse(event.data);
main.ports.gotErrors.send(message);
}
}
window.addEventListener("error", function (e) {
main.ports.gotJsError.send(e.error.message);
return false;
});
});
</script>
</body>
</html>
EOF
}
## DOWNLOAD BINARIES
PATH=$(pwd)/node_modules/.bin:$PATH
if ! [ -x "$(command -v elm)" ]; then
npm install elm@latest-0.19.1
fi
if ! [ -x "$(command -v uglifyjs)" ]; then
npm install uglify-js
fi
## GENERATE HTML
mkdir -p _site
mkdir -p _temp
## static
cp -r static/* _site/
## pages
echo "PAGES"
for elm in $(find pages -type f -name "*.elm")
do
subpath="${elm#pages/}"
name="${subpath%.elm}"
js="_temp/$name.js"
html="_site/$name.html"
if [ -f $html ] && [ $(date -r $elm +%s) -le $(date -r $html +%s) ]; then
echo "Cached: $elm"
else
echo "Compiling: $elm"
mkdir -p $(dirname $js)
mkdir -p $(dirname $html)
rm -f elm-stuff/*/Main.elm*
if is_prod
then
elm make $elm --optimize --output=$js > /dev/null
uglifyjs $js --compress 'pure_funcs="F2,F3,F4,F5,F6,F7,F8,F9,A2,A3,A4,A5,A6,A7,A8,A9",pure_getters,keep_fargs=false,unsafe_comps,unsafe' \
| uglifyjs --mangle \
| makePageHtml $html $name
else
elm make $elm --output=$js > /dev/null
cat $js | makePageHtml $html $name
fi
fi
done
## editor
if ! [ -f _site/assets/editor-codemirror.js ] || ! [ -f _site/assets/editor-elm.js ] || ! [ -f _site/assets/editor-custom-elements.js ]; then
echo "EDITOR"
# code mirror
cat editor/cm/lib/codemirror.js \
editor/cm/mode/elm.js \
editor/cm/addon/edit/closebrackets.js \
editor/cm/addon/edit/matchbrackets.js \
editor/cm/addon/comment/comment.js \
editor/cm/addon/search/searchcursor.js \
editor/cm/addon/search/search.js \
editor/cm/addon/dialog/dialog.js \
editor/cm/lib/active-line.js \
editor/cm/addon/dialog/dialog.js \
editor/cm/keymap/sublime.js \
| uglifyjs -o _site/assets/editor-codemirror.js
# custom elements
cat editor/code-editor.js editor/column-divider.js | uglifyjs -o _site/assets/editor-custom-elements.js
# styles
cat editor/cm/lib/codemirror.css editor/editor.css > _site/assets/editor-styles.css
# elm
(cd editor ; elm make src/Page/Editor.elm --optimize --output=elm.js)
uglifyjs editor/elm.js --compress 'pure_funcs="F2,F3,F4,F5,F6,F7,F8,F9,A2,A3,A4,A5,A6,A7,A8,A9",pure_getters,keep_fargs=false,unsafe_comps,unsafe' | uglifyjs --mangle -o _site/assets/editor-elm.js
rm editor/elm.js
fi
## examples
echo "EXAMPLES"
for elm in $(find examples -type f -name "*.elm")
do
deps="${elm%.elm}.json"
subpath="${elm#examples/}"
name="${subpath%.elm}"
html="_site/examples/$name.html"
if [ -f $html ] && [ $(date -r $elm +%s) -le $(date -r $html +%s) ]; then
echo "Cached: $elm"
else
echo "Compiling: $elm"
rm -f elm-stuff/*/Main.elm*
elm make $elm --output=_site/examples/_compiled/$name.html > /dev/null
cat $elm | makeExampleHtml $html $name $name $deps
fi
done
## try
echo "" | makeExampleHtml _site/try.html "Try Elm!" _try "examples/try.json"
cp editor/splash.html _site/examples/_compiled/_try.html
## REMOVE TEMP FILES
rm -rf _temp