-
Notifications
You must be signed in to change notification settings - Fork 70
/
compressjs.sh
executable file
·78 lines (67 loc) · 2.18 KB
/
compressjs.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
#!/bin/bash
# Constants
SERVICE_URL=http://closure-compiler.appspot.com/compile
#NEWFILE="c`date +"%d%m%y"`.js"
#this specifies last arg as destination
for NEWFILE in $*; do :; done
# Check if files to compile are provided
if [ $# -eq 0 ]
then
echo 'Nothing to compile. Specify input files as command arguments. E.g.'
echo './compressjs file1.js file2.js file3.js'
exit
fi
# Itearate through all files
process_files=false
for f in $*
do
if [ ${f} != $NEWFILE ]
then
if [ -r ${f} ]
then
code="${code} --data-urlencode js_code@${f}"
# Test whether at least one of the input file is newer than the output file
if [ ${f} -nt $NEWFILE ]; then
process_files=true
fi
else
echo "File ${f} does not exist or is not readable. Skipped."
fi
fi
done
if ! ${process_files}
then
echo "Already up to date."
exit 0
fi
# Send request
curl \
--url ${SERVICE_URL} \
--header 'Content-type: application/x-www-form-urlencoded' \
${code} \
--data output_format=json \
--data output_info=compiled_code \
--data output_info=statistics \
--data output_info=errors \
--data compilation_level=SIMPLE_OPTIMIZATIONS |
python -c '
import json, sys
data = json.load(sys.stdin)
if "errors" in data:
print "### COMPILATION FAILED WITH ERRORS"
for err in data["errors"]:
file = sys.argv[int(err["file"].replace("Input_", "")) + 1]
print "File: %s, %d:%d" % (file, err["lineno"], err["charno"])
print "Error: %s" % err["error"]
print "Line: %s" % err["line"]
print "\nBuild failed.\n"
else:
print "### COMPILATION COMPLETED"
print "Original size: %db, gziped: %db" % (data["statistics"]["originalSize"], data["statistics"]["originalGzipSize"])
print "Compressed size: %db, gziped: %db" % (data["statistics"]["compressedSize"], data["statistics"]["compressedGzipSize"])
print "Compression rate: %.2f" % (float(data["statistics"]["compressedSize"]) / int(data["statistics"]["originalSize"]))
filename = "'${NEWFILE}'"
with open(filename, "w") as f:
f.write(data["compiledCode"])
print "\nBuild file %s created.\n" % filename
' $@