Building a Jekyll Watcher with FSSM

Travis Tilley’s File System State Monitor provides a fantastic API for monitoring a filesystem and performing actions when changes occur. It has multiple, platform-specific backends for dealing efficiently with filesystems and will resort to polling if it has to. I have already integrated it into Compass to implement the --watch functionality.

When building this blog, I used it to trivially build a watcher for changes to the blog contents and regenerate the site using jekyll if anything changed. Coupled with serve, I can basically forget that I’m developing a static website that has to be compiled. I’ve documented the basic steps required to build your own watcher here.

Install FSSM

~ sudo gem install ttilley-fssm
Successfully installed ttilley-fssm-0.0.6
1 gem installed
Installing ri documentation for ttilley-fssm-0.0.6...
Installing RDoc documentation for ttilley-fssm-0.0.6...

Define An Update Function

This update function uses pipes because I wanted to see the output as it was emitted.

def rebuild_site(relative)
  puts ">>> Change Detected to: #{relative} <<<"
  IO.popen('rake generate') do |io|
    print(io.readpartial(512)) until io.eof?
  puts '>>> Update Complete <<<'

Create a Watch Task

desc "Watch the site and regenerate when it changes"
task :watch do
  require 'fssm'
  puts ">>> Watching for Changes <<<"
  FSSM.monitor("#{File.dirname(__FILE__)}/_source", '**/*') do
    update {|base, relative| rebuild_site(relative)}
    delete {|base, relative| rebuild_site(relative)}
    create {|base, relative| rebuild_site(relative)}

Invoke It

~/Projects/ rake watch
(in /Users/chris/Projects/
>>> Watching for Changes <<<