From 412a8e9cb63face4c30ea7a4d0b285950d42788f Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Wed, 12 Mar 2014 12:07:20 -0700 Subject: [PATCH] Support listen v1 and v2 --- fontcustom.gemspec | 2 +- lib/fontcustom/watcher.rb | 45 ++++++++++++++++++++++++--------- spec/fontcustom/watcher_spec.rb | 1 + 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/fontcustom.gemspec b/fontcustom.gemspec index 4ee1ea6a..874a2d91 100644 --- a/fontcustom.gemspec +++ b/fontcustom.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |gem| gem.add_dependency "json", "~>1.4" gem.add_dependency "thor", "~>0.14" - gem.add_dependency "listen", "~>1.0" + gem.add_dependency "listen", ">=1.0","<3.0" gem.add_development_dependency "rake" gem.add_development_dependency "bundler" diff --git a/lib/fontcustom/watcher.rb b/lib/fontcustom/watcher.rb index d446888b..d60cc3ed 100644 --- a/lib/fontcustom/watcher.rb +++ b/lib/fontcustom/watcher.rb @@ -13,17 +13,8 @@ def initialize(options, is_test = false) templates = @options[:templates].dup.map { |template| File.basename(template) } packaged = %w|preview css scss scss-rails| templates.delete_if { |template| packaged.include?(template) } - - if templates.empty? - @listener = Listen.to(@options[:input][:vectors]) - else - @listener = Listen.to(@options[:input][:vectors], @options[:input][:templates]) - end - - @listener = @listener.relative_paths(true) - @listener = @listener.filter(/(#{templates.join("|")}|.+\.svg)$/) - @listener = @listener.change(&callback) - @listener = @listener.polling_fallback_message(false) if @is_test + + create_listener(templates) end def watch @@ -34,12 +25,33 @@ def watch end private + + def create_listener(templates) + listen_options = {} + listen_options[:polling_fallback_message] = false if @is_test + + listen_dirs = [@options[:input][:vectors]] + listen_dirs << @options[:input][:templates] unless templates.empty? + + if listen_eq2 + listen_options[:only] = /(#{templates.join("|")}|.+\.svg)$/ + @listener = Listen.to(listen_dirs, listen_options, &callback) + else + listen_options[:filter] = /(#{templates.join("|")}|.+\.svg)$/ + listen_options[:relative_paths] = true + @listener = Listen::Listener.new(listen_dirs, listen_options, &callback) + end + end def start if @is_test # Non-blocking listener @listener.start else - @listener.start! + if listen_eq2 + @listener.start.join + else + @listener.start! + end end end @@ -65,5 +77,14 @@ def callback def compile @base.compile end + + def listen_eq2 + begin + require 'listen/version' + ::Listen::VERSION =~ /^2\./ + rescue LoadError + false + end + end end end diff --git a/spec/fontcustom/watcher_spec.rb b/spec/fontcustom/watcher_spec.rb index 65d53846..8db9da2a 100644 --- a/spec/fontcustom/watcher_spec.rb +++ b/spec/fontcustom/watcher_spec.rb @@ -86,6 +86,7 @@ def watcher(options) new = content + "\n.bar { color: red; }" w.watch + sleep 1 File.open(template, "w") { |file| file.write(new) } sleep 1 ensure