-
-
Save heygambo/1984656 to your computer and use it in GitHub Desktop.
Carefree CoffeeScript Auto-compiler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# -------------------------------------------------------------------------------------- | |
# http://blog.gantrithor.com/post/12535461464/carefree-coffeescript-auto-compiler-part-3 | |
# -------------------------------------------------------------------------------------- | |
DIR_ROOT="$(cd "$(dirname "$0")" && pwd)" | |
function compile_tree() { | |
find "$1" -name "*.coffee" -type f | while read PATH_COFFEE; do | |
DIR_JS="$(dirname "${PATH_COFFEE/#$DIR_ROOT\/public\/coffeescripts/$DIR_ROOT/public/javascripts}")" | |
FILE_JS="$(basename "${PATH_COFFEE/%.coffee/.js}")" | |
echo "--------------------------------" | |
echo " $2" | |
coffee -o "$DIR_JS" -c "$PATH_COFFEE" | |
echo " $PATH_COFFEE" | |
echo "==> $DIR_JS/$FILE_JS" | |
done | |
} | |
if [[ "$1" == "--all" ]]; then | |
compile_tree "$DIR_ROOT/public/coffeescripts" "`date '+%F %T'`" | |
else | |
inotifywait -m -r --timefmt '%F %T' --format '%w☀%f☀%e☀%T' -e ATTRIB,MOVE,CLOSE "$DIR_ROOT/public/coffeescripts" | while read LINE; do | |
IFS=☀ read -a ARRAY <<< "$LINE" | |
######## Safety Precautions ######## | |
# If ${ARRAY[0]} doesn't contain $DIR_ROOT it means something is seriously fubar'd # | |
if [[ "${ARRAY[0]#$DIR_ROOT}" == "${ARRAY[0]}" ]]; then | |
continue | |
fi | |
# Do us no good if it's empty anyway # | |
if [[ "${ARRAY[1]}" == "" ]]; then | |
continue | |
fi | |
######## Safety Precautions ######## | |
EVENT="${ARRAY[2]}" | |
TIME="${ARRAY[3]}" | |
if [[ "$EVENT" == *ISDIR* ]]; then | |
DIR_COFFEE="${ARRAY[0]}${ARRAY[1]}" | |
DIR_JS="${ARRAY[0]/#$DIR_ROOT\/public\/coffeescripts/$DIR_ROOT/public/javascripts}${ARRAY[1]}" | |
case "$EVENT" in | |
# Moving Directories # | |
*MOVED_TO*) | |
compile_tree "${ARRAY[0]}${ARRAY[1]}" "$TIME / $EVENT" | |
;; | |
# Trashing Directories # | |
*MOVED_FROM*) | |
# No `rm -rf` for Safety # | |
echo "--------------------------------" | |
echo " $TIME / $EVENT" | |
find "$DIR_JS" -name "*.js" -type f -delete | |
echo " X $DIR_JS" | |
find "$DIR_JS" -type d -depth -empty -exec rmdir "{}" \; | |
;; | |
esac | |
elif [[ "${ARRAY[1]}" == *.coffee ]]; then | |
DIR_COFFEE="${ARRAY[0]%}" | |
DIR_JS="${ARRAY[0]/#$DIR_ROOT\/public\/coffeescripts/$DIR_ROOT/public/javascripts}" | |
FILE_COFFEE="${ARRAY[1]}" | |
FILE_JS="${ARRAY[1]/%.coffee/.js}" | |
case "$EVENT" in | |
# Copying Directories; Copying/Creating/Moving Files # | |
*ATTRIB*|*CLOSE_WRITE*|*MOVED_TO*) | |
echo "--------------------------------" | |
echo " $TIME / $EVENT" | |
coffee -o "$DIR_JS" -c "$DIR_COFFEE$FILE_COFFEE" | |
echo " $DIR_COFFEE$FILE_COFFEE" | |
echo "==> $DIR_JS$FILE_JS" | |
;; | |
# Trashing Files # | |
*MOVED_FROM*) | |
echo "--------------------------------" | |
echo " $TIME / $EVENT" | |
rm "$DIR_JS$FILE_JS" | |
echo " X $DIR_JS$FILE_JS" | |
rmdir "$DIR_JS" | |
;; | |
esac | |
else | |
continue | |
fi | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment