-
Notifications
You must be signed in to change notification settings - Fork 1
/
dev_build.sh
210 lines (156 loc) · 4.61 KB
/
dev_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
#!/bin/bash
name="js13k2024"
tmp=`readlink -f "$0"`
dir=`dirname $tmp`
source_dir="${dir}/src"
target_dir="/tmp/build"
extra_dir="/home/gheja/works_local/extra"
final_dir="${dir}"
min_dir="${dir}/tmp/min"
csv="${final_dir}/build_stats.csv"
advzip_iterations="3000"
if [ $TERM == "xterm" ] || [ $TERM == "screen" ]; then
color_error='\033[1;31m'
color_success='\033[1;32m'
color_title='\033[1;38m'
color_default='\033[0m'
else
color_error=''
color_success=''
color_title=''
color_default=''
fi
_title()
{
echo ""
echo -ne "${color_title}"
echo "$@"
echo -ne "${color_default}"
}
_error()
{
echo -ne "${color_error}"
echo "$@"
echo -ne "${color_default}"
}
_success()
{
echo -ne "${color_success}"
echo "$@"
echo -ne "${color_default}"
}
try()
{
$@
result=$?
if [ $result != 0 ]; then
_error "ERROR: \"$@\" failed with exit code $result."
exit 1
fi
}
function get_size()
{
local files="$@"
cat $files | wc -c | awk '{ print $1; }'
}
function get_lines()
{
local files="$@"
cat $files | wc -l | awk '{ print $1; }'
}
if [ -e "$target_dir" ]; then
rm -r "$target_dir"
fi
mkdir "$target_dir"
cd "$target_dir"
### stage1 - compilation of typescript to javascript, minimization of javascript and css files
mkdir stage1
mkdir stage1/3rdparty
cd stage1
now=`date +%Y%m%d_%H%M%S`
now2=`date '+%Y-%m-%d %H:%M:%S %:z'`
zip_prefix="${name}_${now}"
_title "Copying files to build directory..."
# try rsync -xa --exclude '*.js' --exclude '*.js.map' --exclude '*.zip' "${source_dir}/" ./
try rsync -xa --exclude '*.js.map' --exclude '*.zip' "${source_dir}/" ./
# try rsync -xa "${source_dir}/3rdparty/" ./3rdparty/
# try rsync -xa "${source_dir}/bonus/" ./bonus/
# try cp "${source_dir}/server/server.min.js" ./
try cp "${source_dir}/externs.js" ./
try cp "${source_dir}/exports.js" ./
zip -r9 ${zip_prefix}_original.zip .
if [ -d "${extra_dir}" ]; then
try rsync -xa "${extra_dir}/" ./
fi
_title "Checking and installing node packages..."
echo "travis_fold:start:npm"
try npm install typescript-closure-compiler google-closure-compiler
echo "travis_fold:end:npm"
export PATH="${target_dir}/stage1/node_modules/.bin:${PATH}"
# note: near.js has a near.min.js version
files_html="index.html"
# files_javascript=`cat index.html | grep -E '<script.* src="([^"]+)"' | grep -Eo 'src=\".*\"' | cut -d \" -f 2 | grep -vE '/socket.io|https:|bonus/near\.js' | grep -vE '^(3rdparty|bonus|https:)/'`
files_javascript=`cat index.html | grep -E '<script.* src="([^"]+)"' | grep -Eo 'src=\".*\"' | cut -d \" -f 2`
# files_javascript_extra=`cat index.html | grep -E '<script.* src="([^"]+)"' | grep -Eo 'src=\".*\"' | cut -d \" -f 2 | grep -vE '/socket.io|https:|bonus/near\.js' | grep -E '^(3rdparty|bonus)/'`
# files_typescript=`echo "$files_javascript" | sed -r 's/\.js$/.ts/g'`
files_css=`cat index.html | grep -E '<link type="text/css" rel="stylesheet" href="([^"]+)"' | grep -Eo 'href=\".*\"' | cut -d \" -f 2`
# cat ./src/$i | sed -e '/DEBUG BEGIN/,/\DEBUG END/{d}' | grep -vE '^\"use strict\";$' >> ./build/stage1/merged.js
lines_html=`get_lines $files_html`
# lines_typescript=`get_lines $files_typescript`
lines_typescript=0
lines_css=`get_lines $files_css`
size_html=`get_size $files_html`
# size_typescript=`get_size $files_typescript`
size_typescript="0"
size_css=`get_size $files_css`
cat $files_javascript > merged.js
cp merged.js min.js
_title "Embedding files into final HTML..."
cat index.min.html | sed \
-e '/<!-- insert minified javascript here -->/{' \
-e 'i <script>' \
-e 'r min.js' \
-e 'a </script>' \
-e 'd}' \
-e '/<!-- insert minified css here -->/{' \
-e 'i <style>' \
-e 'r min.css' \
-e 'a </style>' \
-e 'd}' \
> index.html
_title "Minimizing CSS..."
cat $files_css | \
sed -s 's,/\*.*\*/,,g' | \
sed -r 's/^\s+//g' | \
sed -r 's/\s+$//g' | \
tr -d '\r\n' | \
sed -r 's/}/}\n/g' | \
sed -r 's/ *\{ */{/g' | \
sed -r 's/; *}/}/g' | \
sed -r 's/ *, */,/g' | \
sed -r 's/ *: */:/g' \
> min.css
cd ..
### stage2 - compiling the final html and creating zip files
mkdir stage2
cd stage2
# cp ../stage1/min.css ../stage1/min.js ../stage1/index.min.html ../stage1/server.min.js ./
cp ../stage1/min.css ../stage1/min.js ../stage1/index.min.html ./
# mv server.min.js server.js
_title "Embedding files into final HTML..."
cat index.min.html | sed \
-e '/<!-- insert minified javascript here -->/{' \
-e 'i <script>' \
-e 'r min.js' \
-e 'a </script>' \
-e 'd}' \
-e '/<!-- insert minified css here -->/{' \
-e 'i <style>' \
-e 'r min.css' \
-e 'a </style>' \
-e 'd}' \
> index.html
_title "Creating ZIP files..."
# try zip -9 ${zip_prefix}.zip index.html server.js
try zip -9 ${zip_prefix}.zip index.html
exit 0