master/lib/guard/watcher.rb
Rémy Coutable 1831bf752f Guard no more automatically convert String into Regexp in "watch" method patterns.
It allows to define:

watch("foo_bar.rb") without meaning /foo_bar.rb/ (where "foo_bar_rb.rb" would have been matched)

However, during the deprecation phase, strings that look like a regexes (e.g. "^foo_bar.rb", "foo_bar.rb$", "foo_.*bar.rb" or "foo_(bar|baz).rb" are converted automatically to Regexp and a *very annoying* deprecation message is displayed.
2010-12-16 01:22:42 +01:00

61 lines
1.8 KiB
Ruby

module Guard
class Watcher
attr_accessor :pattern, :action
def initialize(pattern, action = nil)
@pattern, @action = pattern, action
@@warning_printed ||= false
# deprecation warning
if @pattern.is_a?(String) && @pattern =~ /(^(\^))|(>?(\\\.)|(\.\*))|(\(.*\))|(\[.*\])|(\$$)/
unless @@warning_printed
UI.info "DEPRECATED!\nYou have strings in your Guardfile's watch patterns that seem to represent regexps.\nGuard matchs String with == and Regexp with Regexp#match.\nYou should either use plain String (without Regexp special characters) or real Regexp.\n"
@@warning_printed = true
end
UI.info "\"#{@pattern}\" has been converted to #{Regexp.new(@pattern).inspect}\n"
@pattern = Regexp.new(@pattern)
end
end
def self.match_files(guard, files)
guard.watchers.inject([]) do |paths, watcher|
files.each do |file|
if matches = watcher.match_file?(file)
if watcher.action
result = watcher.call_action(matches)
paths << Array(result) if result.respond_to?(:empty?) && !result.empty?
else
paths << matches[0]
end
end
end
paths.flatten.map { |p| p.to_s }
end
end
def self.match_files?(guards, files)
guards.any? do |guard|
guard.watchers.any? do |watcher|
files.any? { |file| watcher.match_file?(file) }
end
end
end
def match_file?(file)
if @pattern.is_a?(Regexp)
file.match(@pattern)
else
file == @pattern
end
end
def call_action(matches)
begin
@action.arity > 0 ? @action.call(matches) : @action.call
rescue
UI.error "Problem with watch action!"
end
end
end
end