Volume 17, Issue 5; 04 Mar 2014

Go ahead, file, change. I've got my eyes on you!

If we did not regularly re-invent the wheel, Ferrari's would have wooden cart-wheels.

Alberto Tagliaro

This is a little hack that I spent twenty minutes writing and have spent twenty days regretting that I didn't write twenty years ago. The inspiration was a question from Florent several years ago. I no longer remember the question, or the answer, but he's still the one that gets the credit.

I do lots of tasks that fit into a general pattern: edit some file, run some command, view the results. This weblog posting is one such example. I am currently editing ~/tmp/2014/03/04/watcher.xml. If I run the command post -f ~/tmp/2014/03/04/watcher.xml http://localhost:8401/pub/ then I will be able to view the posting in the local version of my weblog, http://localhost:8401/2014/03/04/watcher. But the same pattern fits presentations I write, programs I compile, and a host of other things. The only differences, really, are the files I edit and the command that produces the results.

Tangential to the question that Florent asked was the fact that he was using some Java API to “watch” the contents of a directory. That was obviously a technique that would short circuit the edit-process-view cycle, but it still took me years to get around to writing watcher.

Watcher reads a simple XML format (naturally):

  <!-- Ignore any filename that contains a # -->
  <action match="\#"/>
  <!-- If a .xml file changes, run make -->
  <action match=".*\.xml">make</action>

The files ~/.watcher.xml and ./.watcher.xml are read when watcher begins.

Anytime a matching filename changes, the specified command is run. The entire subtree under the current directory is monitored, so paths can be included in the match patterns.

It's not terribly sophisticated, but it sure is convenient!