Skip to content

Instantly share code, notes, and snippets.

@hjr3
Created September 20, 2011 21:59
Show Gist options
  • Save hjr3/1230541 to your computer and use it in GitHub Desktop.
Save hjr3/1230541 to your computer and use it in GitHub Desktop.
Javascript/CSS packaing script
#!/bin/bash
##
# Package multiple javascript/css files into a single file.
#
# This script will search a file for @PACKAGE-START and @PACKAGE-END annotations
# and merge all references files between those annotations into a single file.
# The first file in the list will be treated as the master file. References to
# the subsequent files will be removed.
BASEDIR=httpdocs
PACKAGE=0
root=""
file=$1
if [ -z $file ]; then
echo "Must specify file to package"
exit 1
fi
if [ ! -f $file ]; then
echo "Unable to read $file"
exit 1
fi
cnt=1
while read line
do
echo $line | grep '@PACKAGE-END' > /dev/null
if [ $? -eq 0 ]; then
#echo "root is ${root}"
root=""
PACKAGE=0
fi
if [ $PACKAGE -eq 1 ]; then
js=$(echo $line | sed -s 's#.* "\(.*\.js\).*#\1#g')
js=$BASEDIR$js
if [ -z $root ]; then
root=$js
else
cat $js >> $root
rm $js
#echo "deleting line $cnt"
sed -ie "${cnt}d" $file
# decrement the line number since we just removed a line from the
# file
cnt=`expr $cnt - 1`
fi
fi
echo $line | grep '@PACKAGE-START' > /dev/null
if [ $? -eq 0 ]; then
PACKAGE=1
fi
cnt=`expr $cnt + 1`
done < $file
@charlesgarvin
Copy link

This can be summed up with a little sed hacking:
sed -n -e ':x;/^@PACKAGE-START$/,/^@PACKAGE-END$/ p;tx' input_file | sed -e '/^@PACKAGE-(START|END)$/ d' > output_file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment