Skip to content

Instantly share code, notes, and snippets.

@jeffrafter
Created November 15, 2012 10:17
Show Gist options
  • Save jeffrafter/4077840 to your computer and use it in GitHub Desktop.
Save jeffrafter/4077840 to your computer and use it in GitHub Desktop.
A script that recurses though asset:pipeline style trees, joins and compresses the result
#!/bin/sh
# Make the structure
mkdir -p public/assets/javascripts
mkdir -p public/assets/stylesheets
mkdir -p public/assets/images
# Images - handle these first so that the resulting md5 hashed names can be inserted
# JavaScripts
# TODO files with ' in the require
# TODO erb files
# TODO uniq the files
function expand_dir()
{
if test -d "$1"
then
for k in $1/*
do
expand_file $k $2 "$3"
done
fi
}
# $1 = file name, $2 = base dir, $3 = indent level
function expand_file()
{
if test -f "$1"
then
echo "$1"
DIR=$(dirname $1)
REQUIRES=`awk '/^(\/\/=|\*=) *require/ {
{print $3}
}' "$1"`
for j in $REQUIRES
do
if test -f "$DIR/$j"
then
expand_file "$DIR/$j" $2 "$3 "
elif test -d "$DIR/$j"
then
expand_dir "$DIR/$j" $2 "$3 "
elif test -f "$2/$j"
then
expand_file "$2/$j" $2 "$3 "
elif test -d "$2/$j"
then
expand_dir "$2/$j" $2 "$3 "
else
echo "$3 UNKNOWN: $j"
fi
done
fi
}
GRAY="\033[1;30m"
LIGHT_CYAN="\033[1;36m"
CLEAR="\033[0m"
# Primary kick-off
for i in app/assets/javascripts/*
do
if test -f "$i"
then
filename="${i##*/}"
echo "$GRAY** $filename$CLEAR"
REFS=$(expand_file $i "app/assets/javascripts")
for f in $REFS
do
cat "$f" >> "./public/assets/javascripts/$filename"
done
fi
done
echo "Compressing js"
`java -jar bin/yuicompressor-2.4.7.jar -o '.js.erb$:-min.js' --type css public/assets/javascripts/*.js.erb`
for i in app/assets/stylesheets/*
do
if test -f "$i"
then
filename="${i##*/}"
echo "$LIGHT_CYAN** $filename$CLEAR"
REFS=$(expand_file $i "app/assets/stylesheets")
for f in $REFS
do
cat "$f" >> "./public/assets/stylesheets/$filename"
done
fi
done
echo "Compressing css"
`java -jar bin/yuicompressor-2.4.7.jar -o '.css.erb$:-min.css' --type css public/assets/stylesheets/*.css.erb`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment