-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sh
executable file
·177 lines (143 loc) · 4.72 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
#!/bin/bash
# Check for work folder specified
if [ $# -ge 1 ]
then
workdir=$1
echo "Entering ${workdir}"
cd "${workdir}"
fi
if [ $# -ge 2 ]
then
param=$2
else
param=""
fi
zipfile="js13k.zip"
buildpath="tmpbuild"
jscat="${buildpath}/min.js"
indexcat="${buildpath}/index.html"
assetsrc="assets/tilemap_packed.png"
assetjs="tilemap.js"
leveljs="levels.js"
# See if the levels asset need to be rebuilt
mostrecentlevel=`ls -larth assets/level*.tmx | tail -1 | awk '{ print $NF }'`
srcdate=`stat -c %Y ${mostrecentlevel} 2>/dev/null`
destdate=`stat -c %Y ${leveljs} 2>/dev/null`
# If no js asset found, force build
if [ "${destdate}" == "" ]
then
destdate=0
fi
# When source is newer, rebuild
if [ ${srcdate} -gt ${destdate} ]
then
echo -n "Rebuilding levels..."
# Clear old dest
echo -n "" > "${leveljs}"
# Start new file
echo -n "var levels=[" > "${leveljs}"
for file in assets/level*.tmx
do
echo -n "{" >> "${leveljs}"
for attrib in "width" "height"
do
echo -n "${attrib}:" >> "${leveljs}"
cat "${file}" | grep "<map " | tr ' ' '\n' | grep '^'${attrib}'=' | awk -F'"' '{ print $2 }' | tr -d '\n' >> "${leveljs}"
echo -n ',' >> "${leveljs}"
done
for property in "title"
do
echo -n "${property}:\"" >> "${leveljs}"
cat "${file}" | grep "<property " | grep 'name=\"'${property}'\"' | awk -F'"' '{ print $4 }' | tr -d '\n' >> "${leveljs}"
echo -n '",' >> "${leveljs}"
done
for assettype in "tiles" "chars"
do
echo -n "${assettype}:[" >> "${leveljs}"
cat "${file}" | tr -d '\n' | sed 's/<layer /\n<layer /g' | grep "${assettype}" | sed 's/</\n</g' | grep "<data " | awk -F'>' '{ print $2 }' | sed 's/,0,/,,/g' | sed 's/,0,/,,/g' | sed 's/^0,/,/g' | sed 's/,0$/,/g' | tr -d '\n' >> "${leveljs}"
echo -n "]," >> "${leveljs}"
done
echo -n "}," >> "${leveljs}"
done
echo -n "];" >> "${leveljs}"
echo "done"
fi
# See if the tilemap asset needs to be rebuilt
srcdate=`stat -c %Y ${assetsrc} 2>/dev/null`
destdate=`stat -c %Y ${assetjs} 2>/dev/null`
# If no js asset found, force build
if [ "${destdate}" == "" ]
then
destdate=0
fi
# When source is newer, rebuild
if [ ${srcdate} -gt ${destdate} ]
then
echo -n "Rebuilding tilemap..."
# Clear old dest
echo -n "" > "${assetjs}"
# Convert from src to dest
echo -n 'const tilemap="data:image/png;base64,' > "${assetjs}"
base64 -w 0 "${assetsrc}" >> "${assetjs}"
echo '";' >> "${assetjs}"
echo "done"
fi
if [ "${param}" == "run" ]
then
curbrowser=`which xdg-open >/dev/null 2>&1`
if [ "${curbrowser}" == "" ]
then
curbrowser="firefox"
fi
${curbrowser} "index.html" >/dev/null 2>&1
exit 0
fi
# Create clean build folder
echo "Cleaning build folder"
rm -Rf "${buildpath}" >/dev/null 2>&1
mkdir "${buildpath}"
# Concatenate the JS files
echo "Concatenating JS"
touch "${jscat}" >/dev/null 2>&1
for file in "monetization.js" "timeline.js" "font.js" "writer.js" "${assetjs}" "${leveljs}" "pathfinder.js" "inputs.js" "music.js" "main.js"
do
cat "${file}" >> "${jscat}"
done
# Add the index header
echo -n '<!DOCTYPE html><html><head><meta charset="utf-8"/><meta http-equiv="Content-Type" content="text/html;charset=utf-8"/><meta name="monetization" content="$ilp.uphold.com/iFiFnRFdU8fa"><title>Bee Kind</title><style>' > "${indexcat}"
# Inject the concatenated and minified CSS files
echo "Minifying CSS"
for file in "main.css"
do
JAVA_CMD=java yui-compressor "${file}" >> "${indexcat}"
done
# Add on the rest of the index file
echo -n '</style><script type="text/javascript">' >> "${indexcat}"
# Inject the closure-ised and minified JS
echo "Using closure to minify JS"
./closeyoureyes.sh "${jscat}" | tr -d '\n' >> "${indexcat}"
# Add on the rest of the index file
echo -n '</script><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/></head><body><div id="wrapper"><canvas id="tiles" width="320" height="180"></canvas><canvas id="sprites" width="320" height="180"></canvas></div></body></html>' >> "${indexcat}"
# Remove the minified JS
rm "${jscat}" >/dev/null 2>&1
# Remove old zip
rm -Rf "${zipfile}" >/dev/null 2>&1
# Zip everything up
echo "ZIP build folder"
zip -j "${zipfile}" "${buildpath}"/*
# Re-Zip with advzip to save a bit more
echo "Trying to reduce ZIP size"
advzip -i 200 -k -z -4 "${zipfile}"
# Determine file sizes and compression
unzip -lv "${zipfile}"
stat "${zipfile}"
zipsize=`stat -c %s "${zipfile}"`
maxsize=$((13*1024))
bytesleft=$((${maxsize}-${zipsize}))
percent=$((200*${zipsize}/${maxsize} % 2 + 100*${zipsize}/${maxsize}))
if [ ${bytesleft} -ge 0 ]
then
echo "YAY ${percent}% used - it fits with ${bytesleft} bytes spare"
else
echo "OH NO ${percent}% used - it's gone ovey by "$((0-${bytesleft}))" bytes"
fi