source: misc/scripts/minify_if_changed.sh @ 936

Last change on this file since 936 was 762, checked in by duh, 13 years ago
  • added documentation
  • made yui compressor binary configurable
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Rev
File size: 2.2 KB
Line 
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
38YUI_COMPRESSOR=~/apps/yuicompressor-2.4.2/build/yuicompressor-2.4.2.jar
39SOURCE=$1
40ONLY_IF_MINIFIED_ALREADY_EXISTS=$2
41DESTINATION=`echo $1|sed 's/\.[A-Za-z]*$/.min\0/g'`
42MD5SUM=`which md5sum`
43MD5_CURRENT=
44MD5_NEW=
45TMP_FILE=~/tmp/minified
46
47# got a parameter?
48if [ ! $1 ]; then
49        echo "Usage: "$0" <input file> [only create if a minified already exists]"
50        echo "Example: "$0" bla.js 1"
51        exit;
52fi
53
54# does the input file exist?
55if [ ! -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;
60fi
61
62# does the destination exist?
63if [ -f $DESTINATION ]; then
64        # yes, create an MD5 sum
65        MD5_CURRENT=`$MD5SUM -t $DESTINATION | sed 's/ .*$//g'`
66elif [ $ONLY_IF_MINIFIED_ALREADY_EXISTS ]; then
67        # no need to continue
68        exit;
69fi
70
71# minify sourcefile
72java -jar $YUI_COMPRESSOR $SOURCE > $TMP_FILE
73
74# determine the MD5 hash of the generated file
75MD5_NEW=`$MD5SUM -t $TMP_FILE | sed 's/ .*$//g'`
76
77# do the MD5 hashes differ?
78if [ "$MD5_CURRENT" != "$MD5_NEW" ]; then
79        cp $TMP_FILE $DESTINATION
80        echo ".created minified version of "$SOURCE" ("$MD5_CURRENT" > "$MD5_NEW")"
81fi
82
83# remove temp file
84if [ -f $TMP_FILE ]; then
85        rm $TMP_FILE > /dev/null
86fi
Note: See TracBrowser for help on using the repository browser.