1 | #!/bin/sh |
---|
2 | # |
---|
3 | # This script creates a minified version of a javascript or css |
---|
4 | # file if the minified version should be updated (or created). |
---|
5 | # |
---|
6 | # Usage: ./minify_if_changed <input file> [option] |
---|
7 | # - input file : the non minified js / css file to be |
---|
8 | # minified |
---|
9 | # - option : optional argument. If set, the minified |
---|
10 | # version will only be created if a |
---|
11 | # minified version already exists |
---|
12 | # |
---|
13 | # Example: |
---|
14 | # ./minify_if_changed /path/to/my.js A minified version will |
---|
15 | # be created if a previous |
---|
16 | # minified version differs |
---|
17 | # or does not exist. |
---|
18 | # ./minify_if_changed /path/to/my.js 1 A minified version will |
---|
19 | # only be created if a |
---|
20 | # previous version differs. |
---|
21 | # If it does not exist a |
---|
22 | # minified version will not |
---|
23 | # be created |
---|
24 | # |
---|
25 | # Dependencies: |
---|
26 | # - YUI Compressor ( http://yuilibrary.com/downloads/#yuicompressor ) |
---|
27 | # - JAVA ( obviously ) |
---|
28 | # - md5sum ( by default available on most systems ) |
---|
29 | # |
---|
30 | # @Author Jeroen Wesbeek <J****n.W******@gmail.com> |
---|
31 | # @Since 20100729 |
---|
32 | # |
---|
33 | # Revision Information: |
---|
34 | # $Author: t.w.abma@umcutrecht.nl $ |
---|
35 | # $Date: 2010-08-02 13:06:10 +0000 (ma, 02 aug 2010) $ |
---|
36 | # $Rev: 762 $ |
---|
37 | |
---|
38 | YUI_COMPRESSOR=~/apps/yuicompressor-2.4.2/build/yuicompressor-2.4.2.jar |
---|
39 | SOURCE=$1 |
---|
40 | ONLY_IF_MINIFIED_ALREADY_EXISTS=$2 |
---|
41 | DESTINATION=`echo $1|sed 's/\.[A-Za-z]*$/.min\0/g'` |
---|
42 | MD5SUM=`which md5sum` |
---|
43 | MD5_CURRENT= |
---|
44 | MD5_NEW= |
---|
45 | TMP_FILE=~/tmp/minified |
---|
46 | |
---|
47 | # got a parameter? |
---|
48 | if [ ! $1 ]; then |
---|
49 | echo "Usage: "$0" <input file> [only create if a minified already exists]" |
---|
50 | echo "Example: "$0" bla.js 1" |
---|
51 | exit; |
---|
52 | fi |
---|
53 | |
---|
54 | # does the input file exist? |
---|
55 | if [ ! -f $1 ]; then |
---|
56 | echo "Usage: "$0" <input file> [only create if a minified already exists]" |
---|
57 | echo "Example: "$0" bla.js 1" |
---|
58 | echo "error: could not find "$1 |
---|
59 | exit; |
---|
60 | fi |
---|
61 | |
---|
62 | # does the destination exist? |
---|
63 | if [ -f $DESTINATION ]; then |
---|
64 | # yes, create an MD5 sum |
---|
65 | MD5_CURRENT=`$MD5SUM -t $DESTINATION | sed 's/ .*$//g'` |
---|
66 | elif [ $ONLY_IF_MINIFIED_ALREADY_EXISTS ]; then |
---|
67 | # no need to continue |
---|
68 | exit; |
---|
69 | fi |
---|
70 | |
---|
71 | # minify sourcefile |
---|
72 | java -jar $YUI_COMPRESSOR $SOURCE > $TMP_FILE |
---|
73 | |
---|
74 | # determine the MD5 hash of the generated file |
---|
75 | MD5_NEW=`$MD5SUM -t $TMP_FILE | sed 's/ .*$//g'` |
---|
76 | |
---|
77 | # do the MD5 hashes differ? |
---|
78 | if [ "$MD5_CURRENT" != "$MD5_NEW" ]; then |
---|
79 | cp $TMP_FILE $DESTINATION |
---|
80 | echo ".created minified version of "$SOURCE" ("$MD5_CURRENT" > "$MD5_NEW")" |
---|
81 | fi |
---|
82 | |
---|
83 | # remove temp file |
---|
84 | if [ -f $TMP_FILE ]; then |
---|
85 | rm $TMP_FILE > /dev/null |
---|
86 | fi |
---|