forked from aurium/gravity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·125 lines (103 loc) · 3.1 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
#!/bin/bash -e
if [ x$DEBUG == x1 -o x$DEBUG == xon -o x$DEBUG == xtrue ]; then
debug=true
else
debug=false
fi
cd "$(dirname "$0")"
zip=/tmp/gravity.zip
function generateJS() {
file_name="$1"
parent_sed_script="$2"
ws="$(echo -en '[ \t]')"
nws="$(echo -en '[^ \t]')"
macro_re="^.*//$ws*macro$ws+($nws+)$ws*:$ws*([^\r\n]+).*\$"
inc_re="^$ws*//$ws*include$ws+($nws+).*\$"
sed_script="$(
echo "$parent_sed_script"
egrep "$macro_re" "$file_name.metajs" |
while read macro_def; do
macro_name="$( echo "$macro_def" | sed -r "s#$macro_re#\1#" )"
macro_expr="$( echo "$macro_def" | sed -r "s#$macro_re#\2#; s#@([1-9])#\\\\\1#g" )"
if ( echo "$macro_def" | grep -q '@' ); then
echo "s#$macro_name\(([^),]+),?([^),]+)?\)#$macro_expr#g;"
else
echo "s#$macro_name#$macro_expr#g;"
fi
done
)"
$debug && echo -e "Macro replacements:$sed_script" >&2
echo "(function() { // Start $file_name"
sed -r "$sed_script; s#$ws*//$ws*macro$ws.*##g" "$file_name.metajs" |
while read line; do
inc_file="$( echo $line | egrep "$inc_re" | sed -r "s#$inc_re#\1#" )"
test -n "$inc_file" && echo ">> Including $inc_file" >&2
if test -n "$inc_file"; then
generateJS "$inc_file" "$sed_script"
else
echo "$line"
fi
done
echo "})(); // End $file_name"
}
generateJS gravity > gravity.js
$debug || sed -ri 's#^.*//\s*debug\s*$##' gravity.js
if ! ( which cssc && which uglifyjs )>/dev/null; then
echo "
You don't have some minifyer build dependence.
$ sudo npm install -g css-condense uglify-js
"
exit 1
fi
if ! ( which zip && which inotifywait )>/dev/null; then
echo "
You don't have some build dependence tools.
$ sudo aptitude install zip inotify-tools
"
exit 1
fi
if ! (
uglifyjs gravity.js --screw-ie8 --mangle --compress --output=gravity.min.js &&
cssc style.css > style.min.css
); then
echo ">>>> MINIFY ERROR! <<<<" >&2
(
which espeak &&
espeak -v en -s 150 'minifyer error' &
) >/dev/null 2>&1
else
#b='\\'
#sed -ri "
# s/$b/$b$b/g;
# s/'/$b'/g;
# s/function\(/',\n'/g;
# s/^/eval(['/;
# s/$/'].join('function('))/
#" gravity.min.js
#sed -ri 's/function/\nfunction/g' gravity.min.js
test -e $zip && rm $zip
zip $zip gravity.min.js index.html style.min.css
size=$( ls -l $zip | sed -r "s/.* $USER ([0-9]+) .*/\1/" )
sizeK=$( ls -lh $zip | sed -r 's/.* ([0-9,.]+K) .*/\1/' )
test $size -lt $((13*1024)) \
&& valuation="The package is o.k, with $sizeK." \
|| valuation="The package is bigger than the limit, with $sizeK."
echo "=> $valuation ($size of $((13*1024)) bytes)"
$debug && echo "-> debug mode may generate a bigger package then the normal."
( which espeak &&
espeak -v en -s 150 "$valuation" &
) >/dev/null 2>&1
fi
$debug && mv gravity.js gravity.min.js # non ugly code for browser debug
### Daemon #####################################################################
w=$COLUMNS
test -z "$w" && w=$(tput cols)
hr='-'
while [ ${#hr} -lt $w ]; do
hr="-$hr"
done
echo $hr
if inotifywait -r "$(dirname "$0")" -e CLOSE_WRITE; then
sleep 0.3
exec "$0"
fi