davidhaslem.com

Changing of the Guard

I was trying to get my site live tonight, but decided to get sidetracked by adding CoffeeScript support. Turns out my previous setup (Mr Hyde) wasn’t cut out for that sort of thing, so I started looking elsewhere.

As a replacement, I found a nifty new project known as Guard. Guard watches your filesystem for changes and acts on those changes. You simply define some files to watch and some actions to perform in a guardfile, and then run the command line app: guard. From there, it starts up a server and watches for changes.

There are lots of nifty plugins for guard, allowing it to do everything I need - compile sass, coffeescript, run sprockets, and then finally run jekyll… and on top of that - it can run a web server and auto-refresh my browser!

Here’s the setup:

guard 'coffeescript', :input => '_coffee', :output => '_javascripts', :bare => true do
end


guard 'sprockets', :destination => "js", :asset_root => "." do
  watch (%r{^_javascripts/[^\/]+\.js})
end

guard 'sass' do
  watch(%r{^_sass/.+\.s[ac]ss})
end

guard 'webrick', :port => 9294, :docroot => '_site' do
  watch(%r{^_site/.+})
end

guard 'livereload' do
  watch(%r{_site/.+\.(css|js|html)})
end

guard 'shell' do
  watch(/(.*)/){|m| `jekyll` unless m =~ /^_site/ }
end

With that and a simple rake task, I can preview my site with guard, and then, when it’s ready, run rake deploy to deploy the site to my Dreamhost account.