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 | # @Author Jeroen Wesbeek <J****n.W******@gmail.com> |
---|
26 | # @Since 20100729 |
---|
27 | # |
---|
28 | # Revision Information: |
---|
29 | # $Author: duh $ |
---|
30 | # $Date: 2010-07-29 09:09:56 +0000 (do, 29 jul 2010) $ |
---|
31 | # $Rev: 745 $ |
---|
32 | |
---|
33 | SOURCE=$1 |
---|
34 | ONLY_IF_MINIFIED_ALREADY_EXISTS=$2 |
---|
35 | DESTINATION=`echo $1|sed 's/\.[A-Za-z]*$/.min\0/g'` |
---|
36 | MD5SUM=`which md5sum` |
---|
37 | MD5_CURRENT= |
---|
38 | MD5_NEW= |
---|
39 | TMP_FILE=~/tmp/minified |
---|
40 | |
---|
41 | # got a parameter? |
---|
42 | if [ ! $1 ]; then |
---|
43 | echo "Usage: "$0" <input file> [only create if a minified already exists]" |
---|
44 | echo "Example: "$0" bla.js 1" |
---|
45 | exit; |
---|
46 | fi |
---|
47 | |
---|
48 | # does the input file exist? |
---|
49 | if [ ! -f $1 ]; then |
---|
50 | echo "Usage: "$0" <input file> [only create if a minified already exists]" |
---|
51 | echo "Example: "$0" bla.js 1" |
---|
52 | echo "error: could not find "$1 |
---|
53 | exit; |
---|
54 | fi |
---|
55 | |
---|
56 | # does the destination exist? |
---|
57 | if [ -f $DESTINATION ]; then |
---|
58 | # yes, create an MD5 sum |
---|
59 | MD5_CURRENT=`$MD5SUM -t $DESTINATION | sed 's/ .*$//g'` |
---|
60 | elif [ $ONLY_IF_MINIFIED_ALREADY_EXISTS ]; then |
---|
61 | # no need to continue |
---|
62 | exit; |
---|
63 | fi |
---|
64 | |
---|
65 | # minify sourcefile |
---|
66 | java -jar ~/apps/yuicompressor-2.4.2/build/yuicompressor-2.4.2.jar $SOURCE > $TMP_FILE |
---|
67 | |
---|
68 | # determine the MD5 hash of the generated file |
---|
69 | MD5_NEW=`$MD5SUM -t $TMP_FILE | sed 's/ .*$//g'` |
---|
70 | |
---|
71 | # do the MD5 hashes differ? |
---|
72 | if [ "$MD5_CURRENT" != "$MD5_NEW" ]; then |
---|
73 | cp $TMP_FILE $DESTINATION |
---|
74 | echo ".created minified version of "$SOURCE" ("$MD5_CURRENT" > "$MD5_NEW")" |
---|
75 | fi |
---|
76 | |
---|
77 | # remove temp file |
---|
78 | if [ -f $TMP_FILE ]; then |
---|
79 | rm $TMP_FILE > /dev/null |
---|
80 | fi |
---|