Skip to content

Commit

Permalink
Support listen v1 and v2
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Mar 12, 2014
1 parent 6f07458 commit 412a8e9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion fontcustom.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
45 changes: 33 additions & 12 deletions lib/fontcustom/watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
1 change: 1 addition & 0 deletions spec/fontcustom/watcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 412a8e9

Please # to comment.