Aleksander Machniak
2016-05-22 77b5d7ee304a688a2eb115ce04b460b43c0dd700
commit | author | age
bc9c02 1 #!/bin/sh
c562a3 2 PWD=`dirname "$0"`
AM 3 JS_DIR="$PWD/../program/js"
54f1af 4 JAR_DIR='/tmp'
55ee25 5 LANG_IN='ECMASCRIPT3'
c562a3 6 # latest version requires Java 7, we'll use an older one
AM 7 #CLOSURE_COMPILER_URL='http://dl.google.com/closure-compiler/compiler-latest.zip'
d5c381 8 CLOSURE_COMPILER_URL='http://dl.google.com/closure-compiler/compiler-20131014.zip'
bc9c02 9
T 10 do_shrink() {
11     rm -f "$2"
96dee9 12     # copy the first comment block with license information for LibreJS
TB 13     grep -q '@lic' $1 && sed -n '/\/\*/,/\*\// { p; /\*\//q; }' $1 > $2
14     java -jar $JAR_DIR/compiler.jar --compilation_level=SIMPLE_OPTIMIZATIONS --js="$1" --language_in="$3" >> $2
bc9c02 15 }
T 16
17 if [ ! -d "$JS_DIR" ]; then
18     echo "Directory $JS_DIR not found."
19     exit 1
54f1af 20 fi
T 21
22 if [ ! -w "$JAR_DIR" ]; then
c562a3 23     JAR_DIR=$PWD
bc9c02 24 fi
T 25
26 if java -version >/dev/null 2>&1; then
27     :
28 else
29     echo "Java not found. Please ensure that the 'java' program is in your PATH."
30     exit 1
31 fi
32
54f1af 33 if [ ! -r "$JAR_DIR/compiler.jar" ]; then
bc9c02 34     if which wget >/dev/null 2>&1 && which unzip >/dev/null 2>&1; then
T 35         wget "$CLOSURE_COMPILER_URL" -O "/tmp/$$.zip"
36     elif which curl >/dev/null 2>&1 && which unzip >/dev/null 2>&1; then
37         curl "$CLOSURE_COMPILER_URL" -o "/tmp/$$.zip"
38     else
54f1af 39         echo "Please download $CLOSURE_COMPILER_URL and extract compiler.jar to $JAR_DIR/."
bc9c02 40         exit 1
T 41     fi
54f1af 42     (cd $JAR_DIR && unzip "/tmp/$$.zip" "compiler.jar")
bc9c02 43     rm -f "/tmp/$$.zip"
T 44 fi
45
55ee25 46 # compress single file from argument
TB 47 if [ $# -gt 0 ]; then
48     JS_DIR=`dirname "$1"`
49     JS_FILE="$1"
50
51     if [ $# -gt 1 ]; then
52         LANG_IN="$2"
53     fi
54
55     echo "Shrinking $JS_FILE"
c562a3 56     minfile=`echo $JS_FILE | sed -e 's/\.js$/\.min\.js/'`
AM 57     do_shrink "$JS_FILE" "$minfile" "$LANG_IN"
55ee25 58     exit
TB 59 fi
60
c562a3 61 DIRS="$PWD/../program/js $PWD/../skins/* $PWD/../plugins/* $PWD/../plugins/*/skins/*"
55ee25 62 # default: compress application scripts
c562a3 63 for dir in $DIRS; do
AM 64     for file in $dir/*.js; do
65         echo "$file" | grep -e '.min.js$' >/dev/null
66         if [ $? -eq 0 ]; then
67             continue
68         fi
69         if [ ! -f "$file" ]; then
70             continue
71         fi
72
73         echo "Shrinking $file"
74         minfile=`echo $file | sed -e 's/\.js$/\.min\.js/'`
75         do_shrink "$file" "$minfile" "$LANG_IN"
76     done
bc9c02 77 done