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 Password: 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?
end
puts '>>> Update Complete <<<'
end
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)}
end
end
Invoke It
~/Projects/chriseppstein.github.com rake watch (in /Users/chris/Projects/chriseppstein.github.com) >>> Watching for Changes <<<
About Me
I am an open source hacker and stylesheet architect at LinkedIn. I live in San Jose, California with my wife and daughter.
Open Source
I'm the creator of Compass, a stylesheet authoring framework and I'm on the core team of Sass — the stylesheet syntax upon which Compass is built. I maintain about a dozen less well known ruby libraries and rails plugins on github, and am an active contributor of patches to the many open source projects that I use.