Skip to content

Instantly share code, notes, and snippets.

@jnous
Created June 12, 2014 13:49
Show Gist options
  • Save jnous/643e9d7131142f286dd3 to your computer and use it in GitHub Desktop.
Save jnous/643e9d7131142f286dd3 to your computer and use it in GitHub Desktop.
Bash script to watch Compass project dir using inotifywait and compile on changes. Uses Bundler if project dir has Gemfile.
#!/bin/bash
# Watch Compass project dir for changes using inotifywait,
#+which is more sustainable in the long run than 'compass watch'.
#
# In Debian: apt-get install inotify-tools
# First parameter is the project dir
project_dir="${1?Compass project dir expected as first parameter}"
# Rest of the arguments will be passed to compile command
shift
args="${@}"
# Will watch changes only in the Sass directory (and subdirs)
watch_dir="${project_dir%/}"/sass
echo "Watching for changes in ${watch_dir}"
# Routine checks
if [[ ! -d "${project_dir}" ]]
then
echo "${project_dir} does not exist or is not a directory."
exit 1
elif [[ ! -d "${watch_dir}" ]]
then
echo "${watch_dir} does not exist or is not a directory."
exit 1
fi
# If Gemfile exists in the project dir, use 'bundle exec' to compile.
# Otherwise go with plain 'compass compile'.
[[ -e "${project_dir}"/Gemfile ]] && cmd="bundle" || cmd="compass"
# For bundler, change to project dir.
[[ "${cmd}" == "bundle" ]] && cd "${project_dir}"
echo "Ctrl-C to quit"
while true
do
# Wait.
change=$(inotifywait -q --exclude .# -r -e close_write,moved_to,create "${watch_dir}")
if [[ -n "${change}" ]]
then
# Act.
echo $(date)
if [[ "${cmd}" == "bundle" ]]
then
bundle exec compass compile $args
else
compass compile $args "${project_dir}"
fi
fi
done
exit 0
@jnous
Copy link
Author

jnous commented Jun 12, 2014

Calls to inotifywait, bundle and compass should be made more robust...

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