/
watcher.rb
63 lines (54 loc) · 1.32 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
# Class: Watcher (ProScribe)
# Monitors files for changes.
#
# ## Common usage
#
# w = Watcher.new(project) { puts 'update' }
# w.join
#
module ProScribe
class Watcher
def initialize(project, &blk)
require 'fssm'
@threads = Array.new
# Monitor the project's manual
@threads << Thread.new {
monitor(project.config.manual) { |*a| project.make; yield *a }
}
# Monitor ProScribe's default theme
@threads << Thread.new {
monitor(ProScribe.root('data')) { |*a| project.make; yield *a }
}
# Monitor the project's files
project.config.files.each do |group|
threads << Thread.new {
path = project.root(group.source)
path = path.gsub(/\*.*$/, '')
path = File.realpath(path)
monitor(path) { |*a| project.make; yield *a }
}
end
end
# Attribute: threads (ProScribe::Watcher)
# The threads.
#
attr_reader :threads
# Method: join (ProScribe::Watcher)
# Asks all threads to join.
#
def join
threads.each { |t| t.join }
end
private
def monitor(path, &blk)
updated = lambda { |path, file|
yield path, file
}
FSSM.monitor(path) do
update(&updated)
create(&updated)
delete(&updated)
end
end
end
end