def run
require 'fssm'
require 'pathname'
require_site
watcher_config = self.site.config[:watcher] || {}
@notifier = Notifier.new
rebuilder = lambda do |base, relative|
if base.nil? || relative.nil?
filename = nil
else
filename = ::Pathname.new(File.join([ base, relative ])).relative_path_from(::Pathname.new(Dir.getwd)).to_s
end
if filename
print "Change detected to #{filename}; recompiling… "
else
print "Watcher started; compiling the entire site… "
end
start = Time.now
site = Nanoc::Site.new('.')
begin
site.compile
notify_on_compilation_success = watcher_config.fetch(:notify_on_compilation_success) { true }
if notify_on_compilation_success
@notifier.notify('Compilation complete')
end
time_spent = ((Time.now - start)*1000.0).round
puts "done in #{format '%is %ims', *(time_spent.divmod(1000))}"
rescue Exception => e
notify_on_compilation_failure = watcher_config.fetch(:notify_on_compilation_failure) { true }
if notify_on_compilation_failure
@notifier.notify('Compilation failed')
end
puts
Nanoc::CLI::ErrorHandler.print_error(e)
puts
end
end
rebuilder.call(nil, nil)
dirs_to_watch = watcher_config[:dirs_to_watch] || %w( content layouts lib )
files_to_watch = watcher_config[:files_to_watch] || %w( config.yaml Rules rules Rules.rb rules.rb' )
files_to_watch.delete_if { |f| !File.file?(f) }
puts "Watching for changes…"
watcher = lambda do |*args|
update(&rebuilder)
delete(&rebuilder)
create(&rebuilder)
end
monitor = FSSM::Monitor.new
dirs_to_watch.each { |dir| monitor.path(dir, '**/*', &watcher) }
files_to_watch.each { |filename| monitor.file(filename, &watcher) }
monitor.run
end