smparkes-watchr 0.5.7.7 → 0.5.7.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,6 +24,7 @@ module Watchr
24
24
  handler = Watchr.handler.new
25
25
  handler.add_observer self
26
26
  Watchr.debug "using %s handler" % handler.class.name
27
+ Script.handler = handler
27
28
  handler
28
29
  end
29
30
  @handler
@@ -95,7 +96,16 @@ module Watchr
95
96
  end
96
97
  watch
97
98
  end
99
+ paths.each do |path|
100
+ # $stderr.print "lookup #{path}\n"
101
+ @script.depends_on(path).each do |dependence|
102
+ # $stderr.print "add #{dependence} for #{path}\n"
103
+ paths << dependence
104
+ end
105
+ end
98
106
  paths.push(@script.path).compact!
107
+ paths.uniq!
108
+ # $stderr.print "watch #{paths.map {|path| Pathname(path).expand_path }.join(' ')}\n"
99
109
  paths.map {|path| Pathname(path).expand_path }
100
110
  end
101
111
  end
@@ -25,11 +25,14 @@ module Watchr
25
25
  attr_accessor :handler
26
26
  end
27
27
 
28
- def init first_time
28
+ def init first_time, event
29
29
  # p "w", path, first_time,(first_time ? :load : :created)
30
30
  # $stderr.puts "#{signature}: #{pathname}"
31
31
  update_reference_times
32
- SingleFileWatcher.handler.notify(pathname, (first_time ? :load : :created) )
32
+ # FIX: doesn't pass events
33
+ if !event
34
+ SingleFileWatcher.handler.notify(pathname, (first_time ? :load : :created) )
35
+ end
33
36
  end
34
37
 
35
38
  # File's path as a Pathname
@@ -82,9 +85,11 @@ module Watchr
82
85
  private
83
86
 
84
87
  def update_reference_times
85
- @reference_atime = pathname.atime
86
- @reference_mtime = pathname.mtime
87
- @reference_ctime = pathname.ctime
88
+ begin
89
+ @reference_atime = pathname.atime
90
+ @reference_mtime = pathname.mtime
91
+ @reference_ctime = pathname.ctime
92
+ rescue Exception; end
88
93
  end
89
94
 
90
95
  # Type of latest event.
@@ -155,21 +160,31 @@ module Watchr
155
160
  @old_paths.delete Pathname(path)
156
161
  end
157
162
 
158
- def watch path
163
+ def watch path, event = nil
159
164
  begin
165
+ # p "watch", path, @first_time
160
166
  ::EM.watch_file path.to_s, SingleFileWatcher do |watcher|
161
- watcher.init @first_time
167
+ watcher.init @first_time, event
162
168
  @watchers[path] = watcher
163
169
  end
164
170
  @old_paths << path
165
- rescue Errno::ENOENT; end
171
+ rescue Errno::ENOENT => e
172
+ $stderr.puts e
173
+ rescue Exception => e
174
+ $stderr.puts e
175
+ end
166
176
  end
167
177
 
178
+ def add path
179
+ @monitored_paths << path
180
+ # $stderr.print "add #{path.inspect}\n"
181
+ attach :dependence
182
+ end
183
+
168
184
  private
169
185
 
170
186
  # Binds all <tt>monitored_paths</tt> to the listening loop.
171
- def attach
172
- # p "scan"
187
+ def attach event = nil
173
188
  @monitored_paths = @monitored_paths.uniq
174
189
  new_paths = @monitored_paths - @old_paths
175
190
  remove_paths = @old_paths - @monitored_paths
@@ -182,14 +197,14 @@ module Watchr
182
197
  $stderr.puts "warning: replacing (ignoring) watcher for #{path}"
183
198
  @watchers[path].stop
184
199
  end
185
- watch path
200
+ watch path, event
186
201
  end
187
202
  remove_paths.each do |path|
188
203
  watcher = @watchers[path]
189
204
  raise "hell" if !watcher
190
205
  watcher.stop
191
206
  end
192
- @old_paths = @monitored_paths
207
+ @old_paths = @monitored_paths.dup
193
208
  @first_time = false
194
209
  end
195
210
 
@@ -4,7 +4,6 @@ module Watchr
4
4
  def batches
5
5
  @batches ||= {}
6
6
  end
7
-
8
7
  end
9
8
 
10
9
  # A script object wraps a script file, and is used by a controller.
@@ -15,8 +14,11 @@ module Watchr
15
14
  # script = Watchr::Script.new(path)
16
15
  #
17
16
  class Script
17
+
18
18
  DEFAULT_EVENT_TYPE = :modified
19
19
 
20
+
21
+
20
22
  class Batch
21
23
  def initialize rule
22
24
  @timer = nil
@@ -24,7 +26,7 @@ module Watchr
24
26
  @events = []
25
27
  end
26
28
 
27
- def call data, event
29
+ def call data, event, path
28
30
  if @timer
29
31
  @timer.cancel
30
32
  end
@@ -33,7 +35,7 @@ module Watchr
33
35
  Watchr.batches.delete self
34
36
  end
35
37
  Watchr.batches[self] = self
36
- @events << [ data, event ]
38
+ @events << [ data, event, path ]
37
39
  end
38
40
 
39
41
  def deliver
@@ -41,6 +43,9 @@ module Watchr
41
43
  @timer = nil
42
44
  @events = []
43
45
  @rule.action.call [events]
46
+ events.each do |event|
47
+ Script.learn event[2]
48
+ end
44
49
  end
45
50
  end
46
51
 
@@ -56,16 +61,20 @@ module Watchr
56
61
 
57
62
  class Rule
58
63
 
59
- def call data, event
64
+ def call data, event, path
65
+ # $stderr.print "call #{data} #{event} #{path}\n"
60
66
  if options[:batch]
61
67
  self.batch ||= Batch.new self
62
- batch.call data, event
68
+ batch.call data, event, path
63
69
  else
64
70
  if action.arity == 1
65
71
  action.call data
66
- else
72
+ elsif action.arity == 2
67
73
  action.call data, event
74
+ else
75
+ action.call data, event, path
68
76
  end
77
+ Script.learn path
69
78
  end
70
79
  end
71
80
 
@@ -81,6 +90,7 @@ module Watchr
81
90
  end
82
91
 
83
92
  def match path
93
+ # $stderr.print("match #{path}\n")
84
94
  pattern = self.pattern
85
95
  ( pattern.class == String ) and ( pattern = Regexp.new pattern )
86
96
  # p path, pattern, pattern.match(path)
@@ -102,6 +112,7 @@ module Watchr
102
112
  # path<Pathname>:: the path to the script
103
113
  #
104
114
  def initialize(path)
115
+ self.class.script = self
105
116
  @path = path
106
117
  @rules = []
107
118
  @default_action = lambda {}
@@ -211,6 +222,23 @@ module Watchr
211
222
  instance_eval(@path.read)
212
223
  end
213
224
 
225
+ class << self
226
+ attr_accessor :script, :handler
227
+ def learn path
228
+ script.depends_on(path).each do |p|
229
+ handler.add Pathname(p)
230
+ end
231
+ end
232
+ end
233
+
234
+ def depends_on path
235
+ []
236
+ end
237
+
238
+ def depended_on_by path
239
+ []
240
+ end
241
+
214
242
  # Find an action corresponding to a path and event type. The returned
215
243
  # action is actually a wrapper around the rule's action, with the
216
244
  # match_data prepopulated.
@@ -225,16 +253,17 @@ module Watchr
225
253
  # script.action_for('test/test_watchr.rb').call #=> "ruby test/test_watchr.rb"
226
254
  #
227
255
  def call_action_for(path, event_type = DEFAULT_EVENT_TYPE)
256
+ # $stderr.print "caf #{path}\n";
257
+ pathname = path
228
258
  path = rel_path(path).to_s
229
- # p path
259
+ depended_on_by(path).each { |dependence| call_action_for(dependence, event_type) }
230
260
  rules_for(path).each do |rule|
231
- # p rule
232
261
  types = rule.event_types
233
262
  !types.empty? or types = [ nil ]
234
263
  types.each do |rule_event_type|
235
264
  if ( rule_event_type.nil? && ( event_type != :load ) ) || ( rule_event_type == event_type )
236
265
  data = path.match(rule.pattern)
237
- return rule.call(data, event_type)
266
+ return rule.call(data, event_type, pathname)
238
267
  end
239
268
  end
240
269
  end
@@ -276,12 +305,7 @@ module Watchr
276
305
  # rules<Array(Rule)>:: rules corresponding to <tt>path</tt>
277
306
  #
278
307
  def rules_for(path)
279
- @rules.reverse.select do |rule|
280
- # p "K", path, rule.pattern, path.match(rule.pattern)
281
- path.match(rule.pattern)
282
- end
283
- # p "KK", path, @rules.reverse.select {|rule| path.match(rule.pattern) }
284
- @rules.reverse.select {|rule| path.match(rule.pattern) }
308
+ @rules.reverse.select do |rule| path.match(rule.pattern) end
285
309
  end
286
310
 
287
311
  # Make a path relative to current working directory.
@@ -1,7 +1,7 @@
1
1
 
2
2
  Gem::Specification.new do |s|
3
3
  s.name = 'smparkes-watchr'
4
- s.version = '0.5.7.7'
4
+ s.version = '0.5.7.8'
5
5
  s.summary = "Modern continious testing (flexible alternative to autotest)"
6
6
  s.description = "Modern continious testing (flexible alternative to autotest)."
7
7
  s.author = "mynyml"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smparkes-watchr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.7.7
4
+ version: 0.5.7.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - mynyml
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-05 00:00:00 -08:00
12
+ date: 2009-11-09 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency