Summary
This is a fork of the Watchr gem at github.com/mynyml/watchr.
The are a number of differences. It’s been ported to EventMachine for file-events and has some extensions for how watches can be written.
To install, you can clone the repo (github.com/smparkes/watchr) or install the gem, smparkes-watchr
. Note that you’ll need my copy of EventMachine (github.com/smparkes/eventmachine or the smparkes-eventmachine
gem) for directory watches to work well. (Hoping these changes will get pulled upstream …)
EventMachine
Not much to say. It supports EM as well as Rev. The default is still Rev. To use EventMachine, after installing the EM gem, I just add
begin; require 'watchr/event_handlers/em'; rescue LoadError; end
to my watchr.cnf
. It’ll use the first required backend, or Rev if none are found (and Rev is installed). There may be better ways to do this.
Event processing extensions
I made several changes to the way events are created/processed, mostly to make my watchr.cnf
work:
#!/usr/bin/env watchr
begin; require 'watchr/event_handlers/em'; rescue LoadError; end
watch( %r(.*), :modified, lambda { |md| File.directory? md[0] } ) do |md|
raise Watchr::Refresh
end
watch( %r((public/.*)\.haml$), [ :created, :modified ] ) do |md|
cmd = "rm -f #{md[1]}.html && haml -f html5 #{md[0]} #{md[1]}.html && chmod 444 #{md[1]}.html"
puts cmd
system cmd
if $?.signaled? && $?.termsig == 2
Process.kill 2, 0
end
end
watch( %r((spec/.*[Ss]pec)\.(html|js)$), [ :load, :created, :modified ] ) do |md|
cmd = "jazrb #{md[0]}"
puts cmd
system cmd
if $?.signaled? && $?.termsig == 2
Process.kill 2, 0
end
end
The purpose of the first watch is to cause watchr to refresh when directories change. This allows new files to be picked up. The watch pattern matches all files in the current directory tree (though not .
itself; probably should), and watches for :modified
events. It adds an extra lambda that is processed while looking at file paths: only directories will get watches put on them. (Otherwise %r(.*)
would put watches on every file/directory in the tree, which seems kinda bad.)
The second watch looks for Haml files in my public directory and automatically converts them to HTML. These are static files, not served by an app server like Rails: even with static files, I hate writing raw HTML. The extension here is that instead of a single event type, I can pass an array. In addition to the :modified
, event, this will get called when watchr notes that a new file of this type has appeared. (Note this works starting from watchr’s first pass; files created while watchr wasn’t running don’t count).
Then final watch looks for JavaScript and HTML spec files and runs them under jazrb (see github.com/smparkes/jazrb if interested). This case is similar to the previous but also adds the :load
event. This event gets fired the first time a file is seen after watchr starts. This allows watchr to run all the specs when first started, similar to what autotest does.
Recent Changes
-
POC for batch events that occur within a time window
-
Pass event type to callbacks
Things to do
-
Integrate upstream (if they want it)
-
Move directory watch into watchr itself
-
Handle user interrupt processing more systematically
-
Don’t swallow config file error messages, particularly on reload. Is this still happening?
-
Implement some kind of dependence detection (probably starting to get into plugin territory)