/
watcher.rb
113 lines (98 loc) · 3.51 KB
/
watcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require "guard/config"
require "guard/deprecated/watcher" unless Guard::Config.new.strict?
require "guard/ui"
module Guard
# The watcher defines a RegExp that will be matched against file system
# modifications.
# When a watcher matches a change, an optional action block is executed to
# enable processing the file system change result.
#
class Watcher
Deprecated::Watcher.add_deprecated(self) unless Config.new.strict?
attr_accessor :pattern, :action
# Initializes a file watcher.
#
# @param [String, Regexp] pattern the pattern to be watched by the Guard
# plugin
# @param [Block] action the action to execute before passing the result to
# the Guard plugin
#
def initialize(pattern, action = nil)
@pattern, @action = pattern, action
@@warning_printed ||= false
# deprecation warning
regexp = /(^(\^))|(>?(\\\.)|(\.\*))|(\(.*\))|(\[.*\])|(\$$)/
return unless @pattern.is_a?(String) && @pattern =~ regexp
unless @@warning_printed
::Guard::UI.info "*" * 20 + "\nDEPRECATION WARNING!\n" + "*" * 20
::Guard::UI.info <<-MSG
You have a string in your Guardfile watch patterns that seem to
represent a Regexp.
Guard matches String with == and Regexp with Regexp#match.
You should either use plain String (without Regexp special
characters) or real Regexp.
MSG
@@warning_printed = true
end
new_regexp = Regexp.new(@pattern).inspect
::Guard::UI.info "\"#{@pattern}\" has been converted to #{ new_regexp }\n"
@pattern = Regexp.new(@pattern)
end
# Finds the files that matches a Guard plugin.
#
# @param [Guard::Plugin] guard the Guard plugin which watchers are used
# @param [Array<String>] files the changed files
# @return [Array<Object>] the matched watcher response
#
def self.match_files(guard, files)
return [] if files.empty?
files.inject([]) do |paths, file|
guard.watchers.each do |watcher|
matches = watcher.match(file)
next unless matches
if watcher.action
result = watcher.call_action(matches)
if guard.options[:any_return]
paths << result
elsif result.respond_to?(:empty?) && !result.empty?
paths << Array(result)
else
next
end
else
paths << matches[0]
end
break if guard.options[:first_match]
end
guard.options[:any_return] ? paths : paths.flatten.map(&:to_s)
end
end
# Test the watchers pattern against a file.
#
# @param [String] file the file to test
# @return [Array<String>] an array of matches (or containing a single path
# if the pattern is a string)
#
def match(string_or_pathname)
# TODO: use only match() - and show fnmatch example
file = string_or_pathname.to_s
return (file == @pattern ? [file] : nil) unless @pattern.is_a?(Regexp)
return unless (m = @pattern.match(file))
m = m.to_a
m[0] = file
m
end
# Executes a watcher action.
#
# @param [String, MatchData] matches the matched path or the match from the
# Regex
# @return [String] the final paths
#
def call_action(matches)
@action.arity > 0 ? @action.call(matches) : @action.call
rescue => ex
::Guard::UI.error "Problem with watch action!\n#{ ex.message }"
::Guard::UI.error ex.backtrace.join("\n")
end
end
end