guard 0.8.4 → 0.8.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,2 +1,2 @@
1
- # A sample Guardfile
2
- # More info at https://github.com/guard/guard#readme
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
data/lib/guard/ui.rb CHANGED
@@ -1,188 +1,193 @@
1
- module Guard
2
-
3
- # The UI class helps to format messages for the user.
4
- #
5
- module UI
6
- class << self
7
-
8
- color_enabled = nil
9
-
10
- # Show an info message.
11
- #
12
- # @param [String] message the message to show
13
- # @option options [Boolean] reset whether to clean the output before
14
- #
15
- def info(message, options = { })
16
- unless ENV['GUARD_ENV'] == 'test'
17
- reset_line if options[:reset]
18
- puts color(message) if message != ''
19
- end
20
- end
21
-
22
- # Show a red error message that is prefixed with ERROR.
23
- #
24
- # @param [String] message the message to show
25
- # @option options [Boolean] reset whether to clean the output before
26
- #
27
- def error(message, options = { })
28
- unless ENV['GUARD_ENV'] == 'test'
29
- reset_line if options[:reset]
30
- puts color('ERROR: ', :red) + message
31
- end
32
- end
33
-
34
- # Show a red deprecation message that is prefixed with DEPRECATION.
35
- #
36
- # @param [String] message the message to show
37
- # @option options [Boolean] reset whether to clean the output before
38
- #
39
- def deprecation(message, options = { })
40
- unless ENV['GUARD_ENV'] == 'test'
41
- reset_line if options[:reset]
42
- puts color('DEPRECATION: ', :red) + message
43
- end
44
- end
45
-
46
- # Show a debug message that is prefixed with DEBUG and a timestamp.
47
- #
48
- # @param [String] message the message to show
49
- # @option options [Boolean] reset whether to clean the output before
50
- #
51
- def debug(message, options = { })
52
- unless ENV['GUARD_ENV'] == 'test'
53
- reset_line if options[:reset]
54
- puts color("DEBUG (#{Time.now.strftime('%T')}): ", :yellow) + message if ::Guard.options && ::Guard.options[:debug]
55
- end
56
- end
57
-
58
- # Reset a line.
59
- #
60
- def reset_line
61
- print(color_enabled? ? "\r\e[0m" : "\r\n")
62
- end
63
-
64
- # Clear the output.
65
- #
66
- def clear
67
- system('clear;')
68
- end
69
-
70
- private
71
-
72
- # Reset a color sequence.
73
- #
74
- # @deprecated
75
- # @param [String] text the text
76
- #
77
- def reset_color(text)
78
- deprecation('UI.reset_color(text) is deprecated, please use color(text, ' ') instead.')
79
- color(text, '')
80
- end
81
-
82
- # Checks if color output can be enabled.
83
- #
84
- # @return [Boolean] whether color is enabled or not
85
- #
86
- def color_enabled?
87
- if @color_enabled.nil?
88
- if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i
89
- if ENV['ANSICON']
90
- @color_enabled = true
91
- else
92
- begin
93
- require 'rubygems' unless ENV['NO_RUBYGEMS']
94
- require 'Win32/Console/ANSI'
95
- @color_enabled = true
96
- rescue LoadError
97
- @color_enabled = false
98
- info "You must 'gem install win32console' to use color on Windows"
99
- end
100
- end
101
- else
102
- @color_enabled = true
103
- end
104
- end
105
-
106
- @color_enabled
107
- end
108
-
109
- # Colorizes a text message. See the constant in the UI class for possible
110
- # color_options parameters. You can pass optionally :bright, a foreground
111
- # color and a background color.
112
- #
113
- # @example
114
- #
115
- # color('Hello World', :red, :bright)
116
- #
117
- # @param [String] the text to colorize
118
- # @param [Array] color_options the color options
119
- #
120
- def color(text, *color_options)
121
- color_code = ''
122
- color_options.each do |color_option|
123
- color_option = color_option.to_s
124
- if color_option != ''
125
- if !(color_option =~ /\d+/)
126
- color_option = const_get("ANSI_ESCAPE_#{ color_option.upcase }")
127
- end
128
- color_code += ';' + color_option
129
- end
130
- end
131
- color_enabled? ? "\e[0#{ color_code }m#{ text }\e[0m" : text
132
- end
133
-
134
- end
135
-
136
- # Brighten the color
137
- ANSI_ESCAPE_BRIGHT = '1'
138
-
139
- # Black foreground color
140
- ANSI_ESCAPE_BLACK = '30'
141
-
142
- # Red foreground color
143
- ANSI_ESCAPE_RED = '31'
144
-
145
- # Green foreground color
146
- ANSI_ESCAPE_GREEN = '32'
147
-
148
- # Yellow foreground color
149
- ANSI_ESCAPE_YELLOW = '33'
150
-
151
- # Blue foreground color
152
- ANSI_ESCAPE_BLUE = '34'
153
-
154
- # Magenta foreground color
155
- ANSI_ESCAPE_MAGENTA = '35'
156
-
157
- # Cyan foreground color
158
- ANSI_ESCAPE_CYAN = '36'
159
-
160
- # White foreground color
161
- ANSI_ESCAPE_WHITE = '37'
162
-
163
- # Black background color
164
- ANSI_ESCAPE_BGBLACK = '40'
165
-
166
- # Red background color
167
- ANSI_ESCAPE_BGRED = '41'
168
-
169
- # Green background color
170
- ANSI_ESCAPE_BGGREEN = '42'
171
-
172
- # Yellow background color
173
- ANSI_ESCAPE_BGYELLOW = '43'
174
-
175
- # Blue background color
176
- ANSI_ESCAPE_BGBLUE = '44'
177
-
178
- # Magenta background color
179
- ANSI_ESCAPE_BGMAGENTA = '45'
180
-
181
- # Cyan background color
182
- ANSI_ESCAPE_BGCYAN = '46'
183
-
184
- # White background color
185
- ANSI_ESCAPE_BGWHITE = '47'
186
-
187
- end
188
- end
1
+ module Guard
2
+
3
+ # The UI class helps to format messages for the user. Everything that is logged
4
+ # through this class is considered either as an error message or a diagnostic
5
+ # message and is written to standard error (STDERR).
6
+ #
7
+ # If your Guard does some output that is piped into another process for further
8
+ # processing, please just write it to STDOUT with `puts`.
9
+ #
10
+ module UI
11
+ class << self
12
+
13
+ color_enabled = nil
14
+
15
+ # Show an info message.
16
+ #
17
+ # @param [String] message the message to show
18
+ # @option options [Boolean] reset whether to clean the output before
19
+ #
20
+ def info(message, options = { })
21
+ unless ENV['GUARD_ENV'] == 'test'
22
+ reset_line if options[:reset]
23
+ STDERR.puts color(message) if message != ''
24
+ end
25
+ end
26
+
27
+ # Show a red error message that is prefixed with ERROR.
28
+ #
29
+ # @param [String] message the message to show
30
+ # @option options [Boolean] reset whether to clean the output before
31
+ #
32
+ def error(message, options = { })
33
+ unless ENV['GUARD_ENV'] == 'test'
34
+ reset_line if options[:reset]
35
+ STDERR.puts color('ERROR: ', :red) + message
36
+ end
37
+ end
38
+
39
+ # Show a red deprecation message that is prefixed with DEPRECATION.
40
+ #
41
+ # @param [String] message the message to show
42
+ # @option options [Boolean] reset whether to clean the output before
43
+ #
44
+ def deprecation(message, options = { })
45
+ unless ENV['GUARD_ENV'] == 'test'
46
+ reset_line if options[:reset]
47
+ STDERR.puts color('DEPRECATION: ', :red) + message
48
+ end
49
+ end
50
+
51
+ # Show a debug message that is prefixed with DEBUG and a timestamp.
52
+ #
53
+ # @param [String] message the message to show
54
+ # @option options [Boolean] reset whether to clean the output before
55
+ #
56
+ def debug(message, options = { })
57
+ unless ENV['GUARD_ENV'] == 'test'
58
+ reset_line if options[:reset]
59
+ STDERR.puts color("DEBUG (#{Time.now.strftime('%T')}): ", :yellow) + message if ::Guard.options && ::Guard.options[:debug]
60
+ end
61
+ end
62
+
63
+ # Reset a line.
64
+ #
65
+ def reset_line
66
+ STDERR.print(color_enabled? ? "\r\e[0m" : "\r\n")
67
+ end
68
+
69
+ # Clear the output.
70
+ #
71
+ def clear
72
+ system('clear;')
73
+ end
74
+
75
+ private
76
+
77
+ # Reset a color sequence.
78
+ #
79
+ # @deprecated
80
+ # @param [String] text the text
81
+ #
82
+ def reset_color(text)
83
+ deprecation('UI.reset_color(text) is deprecated, please use color(text, ' ') instead.')
84
+ color(text, '')
85
+ end
86
+
87
+ # Checks if color output can be enabled.
88
+ #
89
+ # @return [Boolean] whether color is enabled or not
90
+ #
91
+ def color_enabled?
92
+ if @color_enabled.nil?
93
+ if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i
94
+ if ENV['ANSICON']
95
+ @color_enabled = true
96
+ else
97
+ begin
98
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
99
+ require 'Win32/Console/ANSI'
100
+ @color_enabled = true
101
+ rescue LoadError
102
+ @color_enabled = false
103
+ info "You must 'gem install win32console' to use color on Windows"
104
+ end
105
+ end
106
+ else
107
+ @color_enabled = true
108
+ end
109
+ end
110
+
111
+ @color_enabled
112
+ end
113
+
114
+ # Colorizes a text message. See the constant in the UI class for possible
115
+ # color_options parameters. You can pass optionally :bright, a foreground
116
+ # color and a background color.
117
+ #
118
+ # @example
119
+ #
120
+ # color('Hello World', :red, :bright)
121
+ #
122
+ # @param [String] the text to colorize
123
+ # @param [Array] color_options the color options
124
+ #
125
+ def color(text, *color_options)
126
+ color_code = ''
127
+ color_options.each do |color_option|
128
+ color_option = color_option.to_s
129
+ if color_option != ''
130
+ if !(color_option =~ /\d+/)
131
+ color_option = const_get("ANSI_ESCAPE_#{ color_option.upcase }")
132
+ end
133
+ color_code += ';' + color_option
134
+ end
135
+ end
136
+ color_enabled? ? "\e[0#{ color_code }m#{ text }\e[0m" : text
137
+ end
138
+
139
+ end
140
+
141
+ # Brighten the color
142
+ ANSI_ESCAPE_BRIGHT = '1'
143
+
144
+ # Black foreground color
145
+ ANSI_ESCAPE_BLACK = '30'
146
+
147
+ # Red foreground color
148
+ ANSI_ESCAPE_RED = '31'
149
+
150
+ # Green foreground color
151
+ ANSI_ESCAPE_GREEN = '32'
152
+
153
+ # Yellow foreground color
154
+ ANSI_ESCAPE_YELLOW = '33'
155
+
156
+ # Blue foreground color
157
+ ANSI_ESCAPE_BLUE = '34'
158
+
159
+ # Magenta foreground color
160
+ ANSI_ESCAPE_MAGENTA = '35'
161
+
162
+ # Cyan foreground color
163
+ ANSI_ESCAPE_CYAN = '36'
164
+
165
+ # White foreground color
166
+ ANSI_ESCAPE_WHITE = '37'
167
+
168
+ # Black background color
169
+ ANSI_ESCAPE_BGBLACK = '40'
170
+
171
+ # Red background color
172
+ ANSI_ESCAPE_BGRED = '41'
173
+
174
+ # Green background color
175
+ ANSI_ESCAPE_BGGREEN = '42'
176
+
177
+ # Yellow background color
178
+ ANSI_ESCAPE_BGYELLOW = '43'
179
+
180
+ # Blue background color
181
+ ANSI_ESCAPE_BGBLUE = '44'
182
+
183
+ # Magenta background color
184
+ ANSI_ESCAPE_BGMAGENTA = '45'
185
+
186
+ # Cyan background color
187
+ ANSI_ESCAPE_BGCYAN = '46'
188
+
189
+ # White background color
190
+ ANSI_ESCAPE_BGWHITE = '47'
191
+
192
+ end
193
+ end
data/lib/guard/version.rb CHANGED
@@ -1,6 +1,6 @@
1
- module Guard
2
- unless defined? Guard::VERSION
3
- # The current gem version of Guard
4
- VERSION = '0.8.4'
5
- end
6
- end
1
+ module Guard
2
+ unless defined? Guard::VERSION
3
+ # The current gem version of Guard
4
+ VERSION = '0.8.5'
5
+ end
6
+ end
data/lib/guard/watcher.rb CHANGED
@@ -1,110 +1,114 @@
1
- module Guard
2
-
3
- # The watcher defines a RegExp that will be matched against file system modifications.
4
- # When a watcher matches a change, an optional action block is executed to enable
5
- # processing the file system change result.
6
- #
7
- class Watcher
8
-
9
- attr_accessor :pattern, :action
10
-
11
- # Initialize a file watcher.
12
- #
13
- # @param [String, Regexp] pattern the pattern to be watched by the guard
14
- # @param [Block] action the action to execute before passing the result to the Guard
15
- #
16
- def initialize(pattern, action = nil)
17
- @pattern, @action = pattern, action
18
- @@warning_printed ||= false
19
-
20
- # deprecation warning
21
- if @pattern.is_a?(String) && @pattern =~ /(^(\^))|(>?(\\\.)|(\.\*))|(\(.*\))|(\[.*\])|(\$$)/
22
- unless @@warning_printed
23
- UI.info "*"*20 + "\nDEPRECATION WARNING!\n" + "*"*20
24
- UI.info <<-MSG
25
- You have a string in your Guardfile watch patterns that seem to represent a Regexp.
26
- Guard matches String with == and Regexp with Regexp#match.
27
- You should either use plain String (without Regexp special characters) or real Regexp.
28
- MSG
29
- @@warning_printed = true
30
- end
31
-
32
- UI.info "\"#{@pattern}\" has been converted to #{ Regexp.new(@pattern).inspect }\n"
33
- @pattern = Regexp.new(@pattern)
34
- end
35
- end
36
-
37
- # Finds the files that matches a Guard.
38
- #
39
- # @param [Guard::Guard] guard the guard which watchers are used
40
- # @param [Array<String>] files the changed files
41
- # @return [Array<String>] the matched files
42
- #
43
- def self.match_files(guard, files)
44
- guard.watchers.inject([]) do |paths, watcher|
45
- files.each do |file|
46
- if matches = watcher.match_file?(file)
47
- if watcher.action
48
- result = watcher.call_action(matches)
49
- paths << Array(result) if result.respond_to?(:empty?) && !result.empty?
50
- else
51
- paths << matches[0]
52
- end
53
- end
54
- end
55
-
56
- paths.flatten.map { |p| p.to_s }
57
- end
58
- end
59
-
60
- # Test if a file would be matched by any of the Guards watchers.
61
- #
62
- # @param [Array<Guard::Guard>] guards the guards to use the watchers from
63
- # @param [Array<String>] files the files to test
64
- # @return [Boolean] Whether a file matches
65
- #
66
- def self.match_files?(guards, files)
67
- guards.any? do |guard|
68
- guard.watchers.any? do |watcher|
69
- files.any? { |file| watcher.match_file?(file) }
70
- end
71
- end
72
- end
73
-
74
- # Test the watchers pattern against a file.
75
- #
76
- # @param [String] file the file to test
77
- # @return [Boolean] whether the given file is matched
78
- #
79
- def match_file?(file)
80
- if @pattern.is_a?(Regexp)
81
- file.match(@pattern)
82
- else
83
- file == @pattern ? [file] : nil
84
- end
85
- end
86
-
87
- # Test if any of the files is the Guardfile.
88
- #
89
- # @param [Array<String>] the files to test
90
- # @return [Boolean] whether one of these files is the Guardfile
91
- #
92
- def self.match_guardfile?(files)
93
- files.any? { |file| "#{ Dir.pwd }/#{ file }" == Dsl.guardfile_path }
94
- end
95
-
96
- # Executes a watcher action.
97
- #
98
- # @param [String, MatchData] the matched path or the match from the Regex
99
- # @return [String] the final paths
100
- #
101
- def call_action(matches)
102
- begin
103
- @action.arity > 0 ? @action.call(matches) : @action.call
104
- rescue Exception => e
105
- UI.error "Problem with watch action!\n#{ e.message }\n\n#{ e.backtrace.join("\n") }"
106
- end
107
- end
108
-
109
- end
110
- end
1
+ module Guard
2
+
3
+ # The watcher defines a RegExp that will be matched against file system modifications.
4
+ # When a watcher matches a change, an optional action block is executed to enable
5
+ # processing the file system change result.
6
+ #
7
+ class Watcher
8
+
9
+ attr_accessor :pattern, :action
10
+
11
+ # Initialize a file watcher.
12
+ #
13
+ # @param [String, Regexp] pattern the pattern to be watched by the guard
14
+ # @param [Block] action the action to execute before passing the result to the Guard
15
+ #
16
+ def initialize(pattern, action = nil)
17
+ @pattern, @action = pattern, action
18
+ @@warning_printed ||= false
19
+
20
+ # deprecation warning
21
+ if @pattern.is_a?(String) && @pattern =~ /(^(\^))|(>?(\\\.)|(\.\*))|(\(.*\))|(\[.*\])|(\$$)/
22
+ unless @@warning_printed
23
+ UI.info "*"*20 + "\nDEPRECATION WARNING!\n" + "*"*20
24
+ UI.info <<-MSG
25
+ You have a string in your Guardfile watch patterns that seem to represent a Regexp.
26
+ Guard matches String with == and Regexp with Regexp#match.
27
+ You should either use plain String (without Regexp special characters) or real Regexp.
28
+ MSG
29
+ @@warning_printed = true
30
+ end
31
+
32
+ UI.info "\"#{@pattern}\" has been converted to #{ Regexp.new(@pattern).inspect }\n"
33
+ @pattern = Regexp.new(@pattern)
34
+ end
35
+ end
36
+
37
+ # Finds the files that matches a Guard.
38
+ #
39
+ # @param [Guard::Guard] guard the guard which watchers are used
40
+ # @param [Array<String>] files the changed files
41
+ # @return [Array<Object>] the matched watcher response
42
+ #
43
+ def self.match_files(guard, files)
44
+ guard.watchers.inject([]) do |paths, watcher|
45
+ files.each do |file|
46
+ if matches = watcher.match_file?(file)
47
+ if watcher.action
48
+ result = watcher.call_action(matches)
49
+ if guard.options[:any_return]
50
+ paths << result
51
+ elsif result.respond_to?(:empty?) && !result.empty?
52
+ paths << Array(result)
53
+ end
54
+ else
55
+ paths << matches[0]
56
+ end
57
+ end
58
+ end
59
+
60
+ guard.options[:any_return] ? paths : paths.flatten.map { |p| p.to_s }
61
+ end
62
+ end
63
+
64
+ # Test if a file would be matched by any of the Guards watchers.
65
+ #
66
+ # @param [Array<Guard::Guard>] guards the guards to use the watchers from
67
+ # @param [Array<String>] files the files to test
68
+ # @return [Boolean] Whether a file matches
69
+ #
70
+ def self.match_files?(guards, files)
71
+ guards.any? do |guard|
72
+ guard.watchers.any? do |watcher|
73
+ files.any? { |file| watcher.match_file?(file) }
74
+ end
75
+ end
76
+ end
77
+
78
+ # Test the watchers pattern against a file.
79
+ #
80
+ # @param [String] file the file to test
81
+ # @return [Boolean] whether the given file is matched
82
+ #
83
+ def match_file?(file)
84
+ if @pattern.is_a?(Regexp)
85
+ file.match(@pattern)
86
+ else
87
+ file == @pattern ? [file] : nil
88
+ end
89
+ end
90
+
91
+ # Test if any of the files is the Guardfile.
92
+ #
93
+ # @param [Array<String>] the files to test
94
+ # @return [Boolean] whether one of these files is the Guardfile
95
+ #
96
+ def self.match_guardfile?(files)
97
+ files.any? { |file| "#{ Dir.pwd }/#{ file }" == Dsl.guardfile_path }
98
+ end
99
+
100
+ # Executes a watcher action.
101
+ #
102
+ # @param [String, MatchData] the matched path or the match from the Regex
103
+ # @return [String] the final paths
104
+ #
105
+ def call_action(matches)
106
+ begin
107
+ @action.arity > 0 ? @action.call(matches) : @action.call
108
+ rescue Exception => e
109
+ UI.error "Problem with watch action!\n#{ e.message }\n\n#{ e.backtrace.join("\n") }"
110
+ end
111
+ end
112
+
113
+ end
114
+ end