guard-helpers 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/guard/helpers/formatter.rb +85 -80
- data/lib/guard/helpers/starter.rb +126 -115
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ODVhMmIyOThjMDE5MGY0ZWVlMDNiMjhiMmE0NDE4OTBlMWQyYmU1Zg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YmY3N2MxYWM3N2E3OGY0ZmI4MWQ0YmQyZDE0NWZlOTkyMzRkMjU1Yw==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NzhiODg0YTBlZjUxY2Y2MjQ0MTBhNTc5ZjlmMzNhMTE2NTgxZmQ3MjdmYjA4
|
10
|
+
NDE4YTkxMGJkMjMyNzU4ODQzYmJkZmUzZjA1MDMzNDlhOWQ0NWM2MGI0ODBi
|
11
|
+
ZWRhMTY1Y2IwMThhMmE0OGE4ZmVjZDViMDUzYmY3ZDEyMTQxYWU=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZDlkZDgyM2I5OWUwNjgyMDMwN2MwNDAzOTZiMGI4MGU1YzVmMDVkYWY5OGU2
|
14
|
+
OWIxNWVmYTBlNjdhOWYzNmRhM2RjNzFiYTMzMTVlZWY2MWIwYzc5ODk5NDJk
|
15
|
+
MmQyYTBkNThiMmFiYWQ5MzhkYzA1MTFmZWM2ODQzZmY2NmFiMmM=
|
@@ -1,91 +1,96 @@
|
|
1
|
+
require 'guard'
|
1
2
|
require 'guard/guard'
|
3
|
+
module ::Guard
|
4
|
+
module Helpers
|
5
|
+
module Formatter
|
6
|
+
class << self
|
7
|
+
# Copyright (c) 2010-2012 Michael Kessler
|
8
|
+
#
|
9
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
10
|
+
# a copy of this software and associated documentation files (the
|
11
|
+
# "Software"), to deal in the Software without restriction, including
|
12
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
13
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
14
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
15
|
+
# the following conditions:
|
16
|
+
#
|
17
|
+
# The above copyright notice and this permission notice shall be
|
18
|
+
# included in all copies or substantial portions of the Software.
|
19
|
+
#
|
20
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
21
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
22
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
23
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
24
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
25
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
26
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
27
|
+
# Collects console and system notification methods and enhances them with
|
28
|
+
# some color information.
|
2
29
|
|
3
|
-
#
|
4
|
-
#
|
5
|
-
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
#
|
13
|
-
# The above copyright notice and this permission notice shall be
|
14
|
-
# included in all copies or substantial portions of the Software.
|
15
|
-
#
|
16
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
-
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
-
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
-
# Collects console and system notification methods and enhances them with
|
24
|
-
# some color information.
|
25
|
-
module ::Guard::Helpers::Formatter class << self
|
30
|
+
# Print an info message to the console.
|
31
|
+
#
|
32
|
+
# @param [String] message the message to print
|
33
|
+
# @param [Hash] options the output options
|
34
|
+
# @option options [Boolean] :reset reset the UI
|
35
|
+
#
|
36
|
+
def info(message, options = { })
|
37
|
+
::Guard::UI.info(message, options)
|
38
|
+
end
|
26
39
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
40
|
+
# Print a debug message to the console.
|
41
|
+
#
|
42
|
+
# @param [String] message the message to print
|
43
|
+
# @param [Hash] options the output options
|
44
|
+
# @option options [Boolean] :reset reset the UI
|
45
|
+
#
|
46
|
+
def debug(message, options = { })
|
47
|
+
::Guard::UI.debug(message, options)
|
48
|
+
end
|
36
49
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
50
|
+
# Print a red error message to the console.
|
51
|
+
#
|
52
|
+
# @param [String] message the message to print
|
53
|
+
# @param [Hash] options the output options
|
54
|
+
# @option options [Boolean] :reset reset the UI
|
55
|
+
#
|
56
|
+
def error(message, options = { })
|
57
|
+
::Guard::UI.error(color(message, ';31'), options)
|
58
|
+
end
|
46
59
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
60
|
+
# Print a green success message to the console.
|
61
|
+
#
|
62
|
+
# @param [String] message the message to print
|
63
|
+
# @param [Hash] options the output options
|
64
|
+
# @option options [Boolean] :reset reset the UI
|
65
|
+
#
|
66
|
+
def success(message, options = { })
|
67
|
+
stamped_message = "#{Time.now.strftime('%r')} #{message}"
|
68
|
+
::Guard::UI.info(color(stamped_message, ';32'), options)
|
69
|
+
end
|
56
70
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
71
|
+
# Outputs a system notification.
|
72
|
+
#
|
73
|
+
# @param [String] message the message to print
|
74
|
+
# @param [Hash] options the output options
|
75
|
+
# @option options [Symbol, String] :image the image to use, either :failed, :pending or :success, or an image path
|
76
|
+
# @option options [String] :title the title of the system notification
|
77
|
+
#
|
78
|
+
def notify(message, options = { })
|
79
|
+
::Guard::Notifier.notify(message, options)
|
80
|
+
end
|
67
81
|
|
68
|
-
|
69
|
-
#
|
70
|
-
# @param [String] message the message to print
|
71
|
-
# @param [Hash] options the output options
|
72
|
-
# @option options [Symbol, String] :image the image to use, either :failed, :pending or :success, or an image path
|
73
|
-
# @option options [String] :title the title of the system notification
|
74
|
-
#
|
75
|
-
def notify(message, options = { })
|
76
|
-
::Guard::Notifier.notify(message, options)
|
77
|
-
end
|
82
|
+
private
|
78
83
|
|
79
|
-
|
84
|
+
# Print a info message to the console.
|
85
|
+
#
|
86
|
+
# @param [String] text the text to colorize
|
87
|
+
# @param [String] color_code the color code
|
88
|
+
#
|
89
|
+
def color(text, color_code)
|
90
|
+
::Guard::UI.send(:color_enabled?) ? "\e[0#{ color_code }m#{ text }\e[0m" : text
|
91
|
+
end
|
80
92
|
|
81
|
-
|
82
|
-
|
83
|
-
# @param [String] text the text to colorize
|
84
|
-
# @param [String] color_code the color code
|
85
|
-
#
|
86
|
-
def color(text, color_code)
|
87
|
-
::Guard::UI.send(:color_enabled?) ? "\e[0#{ color_code }m#{ text }\e[0m" : text
|
93
|
+
end
|
94
|
+
end
|
88
95
|
end
|
89
|
-
|
90
96
|
end
|
91
|
-
end
|
@@ -1,133 +1,144 @@
|
|
1
|
+
require 'guard/guard'
|
2
|
+
require 'guard/helpers/formatter'
|
1
3
|
# Starter code that can be included into a Guard plugin
|
2
|
-
module ::Guard
|
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
|
-
|
4
|
+
module ::Guard
|
5
|
+
module Helpers
|
6
|
+
module Starter
|
7
|
+
|
8
|
+
## Helper methods
|
9
|
+
|
10
|
+
# Copyright (c) 2010-2012 Michael Kessler
|
11
|
+
#
|
12
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
13
|
+
# a copy of this software and associated documentation files (the
|
14
|
+
# "Software"), to deal in the Software without restriction, including
|
15
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
16
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
17
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
18
|
+
# the following conditions:
|
19
|
+
#
|
20
|
+
# The above copyright notice and this permission notice shall be
|
21
|
+
# included in all copies or substantial portions of the Software.
|
22
|
+
# Detects the output directory for each CoffeeScript file. Builds
|
23
|
+
# the product of all watchers and assigns to each directory
|
24
|
+
# the files to which it belongs to.
|
25
|
+
#
|
26
|
+
# @param [Array<Guard::Watcher>] watchers the Guard watchers in the block
|
27
|
+
# @param [Array<String>] files the CoffeeScript files
|
28
|
+
# @param [Hash] options the options for the execution
|
29
|
+
# @option options [String] :output the output directory
|
30
|
+
# @option options [Boolean] :shallow do not create nested directories
|
31
|
+
#
|
32
|
+
def detect_nested_directories(watchers, files, options)
|
33
|
+
return { options[:output] => files } if options[:shallow]
|
34
|
+
|
35
|
+
directories = { }
|
36
|
+
|
37
|
+
watchers.product(files).each do |watcher, file|
|
38
|
+
if matches = file.match(watcher.pattern)
|
39
|
+
target = matches[1] ? File.join(options[:output], File.dirname(matches[1])).gsub(/\/\.$/, '') : options[:output] || File.dirname(file)
|
40
|
+
if directories[target]
|
41
|
+
directories[target] << file
|
42
|
+
else
|
43
|
+
directories[target] = [file]
|
44
|
+
end
|
45
|
+
end
|
40
46
|
end
|
41
|
-
end
|
42
|
-
end
|
43
47
|
|
44
|
-
|
45
|
-
|
46
|
-
|
48
|
+
directories
|
49
|
+
end
|
50
|
+
# End of copyrighted code
|
47
51
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
+
# The filename of the target file
|
53
|
+
def target_filename(directory, file)
|
54
|
+
File.join(directory, File.basename(file))
|
55
|
+
end
|
52
56
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
+
# The action to be performed on a file (e.g., compilation)
|
58
|
+
def act_on(directory, file)
|
59
|
+
raise NotImplementedError.new('act_on not implemented')
|
60
|
+
end
|
57
61
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
62
|
+
# Informs the user of an error
|
63
|
+
def error(message, path)
|
64
|
+
message = "#{self.class.name} ERROR: #{path} with error '#{message}'"
|
65
|
+
::Guard::Helpers::Formatter.error message
|
66
|
+
::Guard::Helpers::Formatter.notify message, :image => :failed
|
67
|
+
end
|
64
68
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
69
|
+
# Informs the user that an action has succeeded.
|
70
|
+
def notify(paths, action="COMPILED")
|
71
|
+
message = if paths.length <= 8
|
72
|
+
"#{self.class.name} #{action}: #{paths.join(', ')}"
|
73
|
+
else
|
74
|
+
"#{self.class.name} #{action}: #{paths[0..7].join(', ')}, and others."
|
75
|
+
end
|
76
|
+
::Guard::Helpers::Formatter.success message
|
77
|
+
::Guard::Helpers::Formatter.notify message, :image => :success
|
78
|
+
end
|
75
79
|
|
76
80
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
81
|
+
## Defaults
|
82
|
+
# Calls #run_all if the :all_on_start option is present.
|
83
|
+
def start
|
84
|
+
run_all if options[:all_on_start]
|
85
|
+
end
|
82
86
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
+
# Call #run_on_change for all files which match this guard.
|
88
|
+
def run_all
|
89
|
+
run_on_changes(Watcher.match_files(self, Dir.glob('**{,/*/**}/*').uniq.compact))
|
90
|
+
end
|
87
91
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
92
|
+
# The default action when a path is removed is to remove the matching
|
93
|
+
# compiled file
|
94
|
+
def run_on_removals(paths)
|
95
|
+
paths = paths.select {|path| not options[:exclude] =~ path and File.file? path}
|
96
|
+
|
97
|
+
directories = detect_nested_directories(watchers, paths, options)
|
98
|
+
removed = []
|
99
|
+
|
100
|
+
directories.each do |directory, files|
|
101
|
+
files.each do |file|
|
102
|
+
begin
|
103
|
+
File.delete file
|
104
|
+
removed << file
|
105
|
+
rescue Error::ENOENT
|
106
|
+
# Ignore
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
if removed.length > 0
|
111
|
+
notify(removed, "REMOVED")
|
103
112
|
end
|
104
113
|
end
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
114
|
+
|
115
|
+
# Run `act_on` on each file that is watched
|
116
|
+
def run_on_changes(paths)
|
117
|
+
if options[:all_on_change]
|
118
|
+
paths = Watcher.match_files(self, Dir.glob('**{,/*/**}/*').uniq.compact)
|
119
|
+
end
|
120
|
+
paths = paths.select {|path| not options[:exclude] =~ path and File.file? path}
|
121
|
+
|
122
|
+
directories = detect_nested_directories(watchers, paths, options)
|
123
|
+
written = []
|
124
|
+
|
125
|
+
directories.each do |directory, files|
|
126
|
+
files.each do |file|
|
127
|
+
begin
|
128
|
+
act_on(directory, file)
|
129
|
+
puts 'Wrote!'
|
130
|
+
written << file
|
131
|
+
rescue Exception => e
|
132
|
+
puts 'Error!'
|
133
|
+
error(e.message, file)
|
134
|
+
throw :task_has_failed
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
if written.length > 0
|
139
|
+
notify(written)
|
126
140
|
end
|
127
141
|
end
|
128
142
|
end
|
129
|
-
if written.length > 0
|
130
|
-
notify(written)
|
131
|
-
end
|
132
143
|
end
|
133
144
|
end
|