guard 2.10.0 → 2.10.1

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