/
linux.rb
62 lines (53 loc) · 1.37 KB
/
linux.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
module Guard
class Linux < Listener
attr_reader :inotify, :files, :latency, :callback
def initialize
@inotify = INotify::Notifier.new
@files = []
@latency = 0.5
end
def on_change(&callback)
@callback = callback
inotify.watch(Dir.pwd, :recursive, :modify, :create, :delete, :move) do |event|
unless event.name == "" # Event on root directory
@files << event.absolute_name
end
end
rescue Interrupt
end
def start
@stop = false
watch_change
end
def stop
@stop = true
inotify.stop
end
def self.usable?
require 'rb-inotify'
if !defined?(INotify::VERSION) || Gem::Version.new(INotify::VERSION.join('.')) < Gem::Version.new('0.5.1')
UI.info "Please update rb-inotify (>= 0.5.1)"
false
else
true
end
rescue LoadError
UI.info "Please install rb-inotify gem for Linux inotify support"
false
end
private
def watch_change
while !@stop
if Config::CONFIG['build'] =~ /java/ || IO.select([inotify.to_io], [], [], latency)
inotify.process
unless files.empty?
files.map! { |file| file.gsub("#{Dir.pwd}/", '') }
callback.call(files)
files.clear
end
sleep latency unless @stop
end
end
end
end
end