#!/bin/bash
# Constants
SERVICE_URL=http://closure-compiler.appspot.com/compile
NEWFILE="c`date +"%d%m%y"`.js"
OPTIONS=""
if [ "$1" = "-strict" ]
then
OPTIONS="--data language=ECMASCRIPT5_STRICT"
shift
fi
# 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
for f in $*
do
if [ -r "${f}" ]
then
code="${code} --data-urlencode js_code@${f}"
else
echo "File ${f} does not exist or is not readable. Skipped."
fi
done
# 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 \
${OPTIONS} |
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}'"
f = open(filename, "w")
f.write(data["compiledCode"])
print "\nBuild file %s created.\n" % filename
' $@
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>