lib/fontcustom/watcher.rb in fontcustom-0.1.4 vs lib/fontcustom/watcher.rb in fontcustom-1.0.0.pre

- old
+ new

@@ -1,37 +1,62 @@ -require 'listen' +require "fontcustom" +require "listen" module Fontcustom class Watcher - def self.watch(*args) - callback = Proc.new do |modified, added, removed| - puts ' >> Changed: ' + modified.join(' ') unless modified.empty? - puts ' >> Added: ' + added.join(' ') unless added.empty? - puts ' >> Removed: ' + removed.join(' ') unless removed.empty? + def initialize(opts) + @opts = opts + @listener = Listen.to(@opts[:input]) + .relative_paths(true) + .filter(/\.(eps|svg)$/) + .change(&callback) + @opts[:blocking] = @opts[:blocking] == false ? false : true + @listener = @listener.polling_fallback_message(false) unless @opts[:blocking] + end - changed = modified + added + removed - Fontcustom.compile(*args) unless changed.empty? - end + def watch + puts "Font Custom is watching your icons at #{@opts[:input]}. Press Ctrl + C to stop." + compile unless @opts[:skip_first] + @listener.start @opts[:blocking] - dir = args.first - @listener = Listen.to(dir).filter(/\.(eps|svg)$/).change(&callback) + rescue Fontcustom::Error => e + show_error e - begin - puts 'Fontcustom is watching your icons at ' + dir - puts 'Press Ctrl + C to stop.' - Fontcustom.compile(*args) - @listener.start() + # Catches Ctrl + C + # TODO Does the listen gem have a better way of handling this? + rescue SignalException + stop + end - # Catches Ctrl + C - # Does listen gem have a better way of handling this? - rescue SignalException - stop + def stop + # Adding a newline so message is not prepended with ^C on SIGTERM + puts "\nFont Custom is signing off. Good night and good luck." + @listener.stop + end + + private + + def callback + Proc.new do |modified, added, removed| + begin + puts " >> Changed: " + modified.join(", ") unless modified.empty? + puts " >> Added: " + added.join(", ") unless added.empty? + puts " >> Removed: " + removed.join(", ") unless removed.empty? + + changed = modified + added + removed + compile unless changed.empty? + rescue Fontcustom::Error => e + show_error e + end end end - def self.stop - # Newline exists so message is not prepended with ^C on SIGTERM - puts "\nFontcustom is signing off. Goodnight and good luck." - @listener.stop + def compile + Fontcustom::Generator::Font.start [@opts] + Fontcustom::Generator::Template.start [@opts] + end + + def show_error(err) + puts "ERROR: #{err.message}" end end end