source: misc/scripts/minify_if_changed.sh @ 753

Last change on this file since 753 was 745, checked in by duh, 12 years ago

added documentation

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Rev
File size: 2.0 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# @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
33SOURCE=$1
34ONLY_IF_MINIFIED_ALREADY_EXISTS=$2
35DESTINATION=`echo $1|sed 's/\.[A-Za-z]*$/.min\0/g'`
36MD5SUM=`which md5sum`
37MD5_CURRENT=
38MD5_NEW=
39TMP_FILE=~/tmp/minified
40
41# got a parameter?
42if [ ! $1 ]; then
43        echo "Usage: "$0" <input file> [only create if a minified already exists]"
44        echo "Example: "$0" bla.js 1"
45        exit;
46fi
47
48# does the input file exist?
49if [ ! -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;
54fi
55
56# does the destination exist?
57if [ -f $DESTINATION ]; then
58        # yes, create an MD5 sum
59        MD5_CURRENT=`$MD5SUM -t $DESTINATION | sed 's/ .*$//g'`
60elif [ $ONLY_IF_MINIFIED_ALREADY_EXISTS ]; then
61        # no need to continue
62        exit;
63fi
64
65# minify sourcefile
66java -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
69MD5_NEW=`$MD5SUM -t $TMP_FILE | sed 's/ .*$//g'`
70
71# do the MD5 hashes differ?
72if [ "$MD5_CURRENT" != "$MD5_NEW" ]; then
73        cp $TMP_FILE $DESTINATION
74        echo ".created minified version of "$SOURCE" ("$MD5_CURRENT" > "$MD5_NEW")"
75fi
76
77# remove temp file
78if [ -f $TMP_FILE ]; then
79        rm $TMP_FILE > /dev/null
80fi
Note: See TracBrowser for help on using the repository browser.