listen 1.3.1 → 2.0.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1 -368
- data/README.md +89 -221
- data/lib/listen/adapter/base.rb +40 -0
- data/lib/listen/adapter/bsd.rb +94 -0
- data/lib/listen/adapter/darwin.rb +44 -0
- data/lib/listen/adapter/linux.rb +93 -0
- data/lib/listen/adapter/polling.rb +49 -0
- data/lib/listen/adapter/windows.rb +64 -0
- data/lib/listen/adapter.rb +23 -304
- data/lib/listen/change.rb +45 -0
- data/lib/listen/directory.rb +73 -0
- data/lib/listen/file.rb +108 -0
- data/lib/listen/listener.rb +81 -250
- data/lib/listen/record.rb +41 -0
- data/lib/listen/silencer.rb +46 -0
- data/lib/listen/version.rb +1 -1
- data/lib/listen.rb +2 -36
- metadata +36 -21
- data/lib/listen/adapters/bsd.rb +0 -75
- data/lib/listen/adapters/darwin.rb +0 -48
- data/lib/listen/adapters/linux.rb +0 -81
- data/lib/listen/adapters/polling.rb +0 -58
- data/lib/listen/adapters/windows.rb +0 -91
- data/lib/listen/directory_record.rb +0 -406
- data/lib/listen/turnstile.rb +0 -32
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
module Listen
|
|
2
|
+
module Adapter
|
|
3
|
+
|
|
4
|
+
# Listener implementation for Linux `inotify`.
|
|
5
|
+
#
|
|
6
|
+
class Linux < Base
|
|
7
|
+
# Watched inotify events
|
|
8
|
+
#
|
|
9
|
+
# @see http://www.tin.org/bin/man.cgi?section=7&topic=inotify
|
|
10
|
+
# @see https://github.com/nex3/rb-inotify/blob/master/lib/rb-inotify/notifier.rb#L99-L177
|
|
11
|
+
#
|
|
12
|
+
EVENTS = [:recursive, :attrib, :create, :delete, :move, :close_write]
|
|
13
|
+
|
|
14
|
+
# The message to show when the limit of inotify watchers is not enough
|
|
15
|
+
#
|
|
16
|
+
INOTIFY_LIMIT_MESSAGE = <<-EOS.gsub(/^\s*/, '')
|
|
17
|
+
Listen error: unable to monitor directories for changes.
|
|
18
|
+
|
|
19
|
+
Please head to https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers
|
|
20
|
+
for information on how to solve this issue.
|
|
21
|
+
EOS
|
|
22
|
+
|
|
23
|
+
def self.usable?
|
|
24
|
+
RbConfig::CONFIG['target_os'] =~ /linux/i
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def initialize(listener)
|
|
28
|
+
require 'rb-inotify'
|
|
29
|
+
super
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def start
|
|
33
|
+
worker = _init_worker
|
|
34
|
+
worker.run
|
|
35
|
+
rescue Errno::ENOSPC
|
|
36
|
+
abort(INOTIFY_LIMIT_MESSAGE)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
# Initializes a INotify worker and adds a watcher for
|
|
42
|
+
# each directory passed to the adapter.
|
|
43
|
+
#
|
|
44
|
+
# @return [INotify::Notifier] initialized worker
|
|
45
|
+
#
|
|
46
|
+
def _init_worker
|
|
47
|
+
INotify::Notifier.new.tap do |worker|
|
|
48
|
+
_directories_path.each { |path| worker.watch(path, *EVENTS, &_worker_callback) }
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def _worker_callback
|
|
53
|
+
lambda do |event|
|
|
54
|
+
next if _skip_event?(event)
|
|
55
|
+
|
|
56
|
+
if _dir_event?(event)
|
|
57
|
+
_notify_change(_event_path(event), type: 'Dir')
|
|
58
|
+
else
|
|
59
|
+
_notify_change(_event_path(event), type: 'file', change: _change(event.flags))
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def _skip_event?(event)
|
|
65
|
+
# Event on root directory
|
|
66
|
+
return true if event.name == ""
|
|
67
|
+
# INotify reports changes to files inside directories as events
|
|
68
|
+
# on the directories themselves too.
|
|
69
|
+
#
|
|
70
|
+
# @see http://linux.die.net/man/7/inotify
|
|
71
|
+
return true if _dir_event?(event) && (event.flags & [:close, :modify]).any?
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def _change(event_flags)
|
|
75
|
+
{ modified: [:attrib],
|
|
76
|
+
added: [:moved_to, :create],
|
|
77
|
+
removed: [:moved_from, :delete] }.each do |change, flags|
|
|
78
|
+
return change unless (flags & event_flags).empty?
|
|
79
|
+
end
|
|
80
|
+
nil
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def _dir_event?(event)
|
|
84
|
+
event.flags.include?(:isdir)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def _event_path(event)
|
|
88
|
+
Pathname.new(event.absolute_name)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
module Listen
|
|
2
|
+
module Adapter
|
|
3
|
+
|
|
4
|
+
# Polling Adapter that works cross-platform and
|
|
5
|
+
# has no dependencies. This is the adapter that
|
|
6
|
+
# uses the most CPU processing power and has higher
|
|
7
|
+
# file IO than the other implementations.
|
|
8
|
+
#
|
|
9
|
+
class Polling < Base
|
|
10
|
+
DEFAULT_POLLING_LATENCY = 1.0
|
|
11
|
+
|
|
12
|
+
def self.usable?
|
|
13
|
+
true
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def start
|
|
17
|
+
_poll_directories
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def _latency
|
|
23
|
+
listener.options[:latency] || DEFAULT_POLLING_LATENCY
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def _poll_directories
|
|
27
|
+
_napped_loop do
|
|
28
|
+
listener.directories.each do |path|
|
|
29
|
+
_notify_change(path, type: 'Dir', recursive: true)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def _napped_loop
|
|
35
|
+
loop do
|
|
36
|
+
_nap_time { yield }
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def _nap_time
|
|
41
|
+
start = Time.now.to_f
|
|
42
|
+
yield
|
|
43
|
+
nap_time = _latency - (Time.now.to_f - start)
|
|
44
|
+
sleep(nap_time) if nap_time > 0
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
module Listen
|
|
2
|
+
module Adapter
|
|
3
|
+
|
|
4
|
+
# Adapter implementation for Windows `wdm`.
|
|
5
|
+
#
|
|
6
|
+
class Windows < Base
|
|
7
|
+
|
|
8
|
+
# The message to show when wdm gem isn't available
|
|
9
|
+
#
|
|
10
|
+
BUNDLER_DECLARE_GEM = <<-EOS.gsub(/^ {6}/, '')
|
|
11
|
+
Please add the following to your Gemfile to avoid polling for changes:
|
|
12
|
+
require 'rbconfig'
|
|
13
|
+
gem 'wdm', '>= 0.1.0' if RbConfig::CONFIG['target_os'] =~ /mswin|mingw|cygwin/i
|
|
14
|
+
EOS
|
|
15
|
+
|
|
16
|
+
def self.usable?
|
|
17
|
+
if RbConfig::CONFIG['target_os'] =~ /mswin|mingw|cygwin/i
|
|
18
|
+
require 'wdm'
|
|
19
|
+
true
|
|
20
|
+
end
|
|
21
|
+
rescue Gem::LoadError
|
|
22
|
+
Kernel.warn BUNDLER_DECLARE_GEM
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def start
|
|
26
|
+
worker = _init_worker
|
|
27
|
+
worker.run!
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
# Initializes a WDM monitor and adds a watcher for
|
|
33
|
+
# each directory passed to the adapter.
|
|
34
|
+
#
|
|
35
|
+
# @return [WDM::Monitor] initialized worker
|
|
36
|
+
#
|
|
37
|
+
def _init_worker
|
|
38
|
+
WDM::Monitor.new.tap do |worker|
|
|
39
|
+
_directories_path.each { |path| worker.watch_recursively(path, &_worker_callback) }
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def _worker_callback
|
|
44
|
+
lambda do |change|
|
|
45
|
+
_notify_change(_path(change.path), type: 'file', change: _change(change.type))
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def _path(path)
|
|
50
|
+
Pathname.new(path)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def _change(type)
|
|
54
|
+
{ modified: [:modified],
|
|
55
|
+
added: [:added, :renamed_new_file],
|
|
56
|
+
removed: [:removed, :renamed_old_file] }.each do |change, types|
|
|
57
|
+
return change if types.include?(type)
|
|
58
|
+
end
|
|
59
|
+
nil
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
end
|
|
64
|
+
end
|
data/lib/listen/adapter.rb
CHANGED
|
@@ -1,325 +1,44 @@
|
|
|
1
|
-
require '
|
|
2
|
-
require '
|
|
3
|
-
require '
|
|
4
|
-
require '
|
|
1
|
+
require 'listen/adapter/base'
|
|
2
|
+
require 'listen/adapter/bsd'
|
|
3
|
+
require 'listen/adapter/darwin'
|
|
4
|
+
require 'listen/adapter/linux'
|
|
5
|
+
require 'listen/adapter/polling'
|
|
6
|
+
require 'listen/adapter/windows'
|
|
5
7
|
|
|
6
8
|
module Listen
|
|
7
|
-
|
|
8
|
-
attr_accessor :directories, :callback, :stopped, :paused,
|
|
9
|
-
:mutex, :changed_directories, :turnstile, :latency,
|
|
10
|
-
:worker, :worker_thread, :poller_thread
|
|
11
|
-
|
|
12
|
-
# The list of existing optimized adapters.
|
|
9
|
+
module Adapter
|
|
13
10
|
OPTIMIZED_ADAPTERS = %w[Darwin Linux BSD Windows]
|
|
11
|
+
POLLING_FALLBACK_MESSAGE = "Listen will be polling for changes. Learn more at https://github.com/guard/listen#polling-fallback."
|
|
14
12
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
# The list of all existing adapters.
|
|
19
|
-
ADAPTERS = OPTIMIZED_ADAPTERS + FALLBACK_ADAPTERS
|
|
20
|
-
|
|
21
|
-
# The default delay between checking for changes.
|
|
22
|
-
DEFAULT_LATENCY = 0.25
|
|
23
|
-
|
|
24
|
-
# The default warning message when falling back to polling adapter.
|
|
25
|
-
POLLING_FALLBACK_MESSAGE = <<-EOS.gsub(/^\s*/, '')
|
|
26
|
-
Listen will be polling for changes. Learn more at https://github.com/guard/listen#polling-fallback.
|
|
27
|
-
EOS
|
|
28
|
-
|
|
29
|
-
# Selects the appropriate adapter implementation for the
|
|
30
|
-
# current OS and initializes it.
|
|
31
|
-
#
|
|
32
|
-
# @param [String, Array<String>] directories the directories to watch
|
|
33
|
-
# @param [Hash] options the adapter options
|
|
34
|
-
# @option options [Boolean] force_polling to force polling or not
|
|
35
|
-
# @option options [String, Boolean] polling_fallback_message to change polling fallback message or remove it
|
|
36
|
-
# @option options [Float] latency the delay between checking for changes in seconds
|
|
37
|
-
#
|
|
38
|
-
# @yield [changed_directories, options] callback the callback called when a change happens
|
|
39
|
-
# @yieldparam [Array<String>] changed_directories the changed directories
|
|
40
|
-
# @yieldparam [Hash] options callback options (like recursive: true)
|
|
41
|
-
#
|
|
42
|
-
# @return [Listen::Adapter] the chosen adapter
|
|
43
|
-
#
|
|
44
|
-
def self.select_and_initialize(directories, options = {}, &callback)
|
|
45
|
-
forced_adapter_class = options.delete(:force_adapter)
|
|
46
|
-
force_polling = options.delete(:force_polling)
|
|
47
|
-
|
|
48
|
-
if forced_adapter_class
|
|
49
|
-
forced_adapter_class.load_dependent_adapter
|
|
50
|
-
return forced_adapter_class.new(directories, options, &callback)
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
return Adapters::Polling.new(directories, options, &callback) if force_polling
|
|
54
|
-
|
|
55
|
-
OPTIMIZED_ADAPTERS.each do |adapter|
|
|
56
|
-
namespaced_adapter = Adapters.const_get(adapter)
|
|
57
|
-
if namespaced_adapter.send(:usable_and_works?, directories, options)
|
|
58
|
-
return namespaced_adapter.new(directories, options, &callback)
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
self.warn_polling_fallback(options)
|
|
63
|
-
Adapters::Polling.new(directories, options, &callback)
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
# Initializes the adapter.
|
|
67
|
-
#
|
|
68
|
-
# @param [String, Array<String>] directories the directories to watch
|
|
69
|
-
# @param [Hash] options the adapter options
|
|
70
|
-
# @option options [Float] latency the delay between checking for changes in seconds
|
|
71
|
-
#
|
|
72
|
-
# @yield [changed_directories, options] callback Callback called when a change happens
|
|
73
|
-
# @yieldparam [Array<String>] changed_directories the changed directories
|
|
74
|
-
# @yieldparam [Hash] options callback options (like recursive: true)
|
|
75
|
-
#
|
|
76
|
-
# @return [Listen::Adapter] the adapter
|
|
77
|
-
#
|
|
78
|
-
def initialize(directories, options = {}, &callback)
|
|
79
|
-
@directories = Array(directories)
|
|
80
|
-
@callback = callback
|
|
81
|
-
@stopped = true
|
|
82
|
-
@paused = false
|
|
83
|
-
@mutex = Mutex.new
|
|
84
|
-
@changed_directories = Set.new
|
|
85
|
-
@turnstile = Turnstile.new
|
|
86
|
-
@latency = options.fetch(:latency, default_latency)
|
|
87
|
-
@worker = initialize_worker
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
# Starts the adapter and don't block the current thread.
|
|
91
|
-
#
|
|
92
|
-
# @param [Boolean] blocking whether or not to block the current thread after starting
|
|
93
|
-
#
|
|
94
|
-
def start
|
|
95
|
-
mutex.synchronize do
|
|
96
|
-
return unless stopped
|
|
97
|
-
@stopped = false
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
start_worker
|
|
101
|
-
start_poller
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
# Starts the adapter and block the current thread.
|
|
105
|
-
#
|
|
106
|
-
# @since 1.0.0
|
|
107
|
-
#
|
|
108
|
-
def start!
|
|
109
|
-
start
|
|
110
|
-
blocking_thread.join
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
# Stops the adapter.
|
|
114
|
-
#
|
|
115
|
-
def stop
|
|
116
|
-
mutex.synchronize do
|
|
117
|
-
return if stopped
|
|
118
|
-
@stopped = true
|
|
119
|
-
turnstile.signal # ensure no thread is blocked
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
worker.stop if worker
|
|
123
|
-
Thread.kill(worker_thread) if worker_thread
|
|
124
|
-
if poller_thread
|
|
125
|
-
poller_thread.kill
|
|
126
|
-
poller_thread.join
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
# Pauses the adapter.
|
|
131
|
-
#
|
|
132
|
-
def pause
|
|
133
|
-
@paused = true
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
# Unpauses the adapter.
|
|
137
|
-
#
|
|
138
|
-
def unpause
|
|
139
|
-
@paused = false
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
# Returns whether the adapter is started or not.
|
|
143
|
-
#
|
|
144
|
-
# @return [Boolean] whether the adapter is started or not
|
|
145
|
-
#
|
|
146
|
-
def started?
|
|
147
|
-
!stopped
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
# Returns whether the adapter is paused or not.
|
|
151
|
-
#
|
|
152
|
-
# @return [Boolean] whether the adapter is paused or not
|
|
153
|
-
#
|
|
154
|
-
def paused?
|
|
155
|
-
paused
|
|
156
|
-
end
|
|
157
|
-
|
|
158
|
-
# Blocks the main thread until the poll thread
|
|
159
|
-
# runs the callback.
|
|
160
|
-
#
|
|
161
|
-
def wait_for_callback
|
|
162
|
-
turnstile.wait unless paused
|
|
163
|
-
end
|
|
164
|
-
|
|
165
|
-
# Blocks the main thread until N changes are
|
|
166
|
-
# detected.
|
|
167
|
-
#
|
|
168
|
-
def wait_for_changes(threshold = 0)
|
|
169
|
-
changes = 0
|
|
170
|
-
|
|
171
|
-
loop do
|
|
172
|
-
mutex.synchronize { changes = changed_directories.size }
|
|
173
|
-
|
|
174
|
-
return if paused || stopped
|
|
175
|
-
return if changes >= threshold
|
|
176
|
-
|
|
177
|
-
sleep(latency)
|
|
178
|
-
end
|
|
179
|
-
end
|
|
180
|
-
|
|
181
|
-
# Checks if the adapter is usable and works on the current OS.
|
|
182
|
-
#
|
|
183
|
-
# @param [String, Array<String>] directories the directories to watch
|
|
184
|
-
# @param [Hash] options the adapter options
|
|
185
|
-
# @option options [Float] latency the delay between checking for changes in seconds
|
|
186
|
-
#
|
|
187
|
-
# @return [Boolean] whether the adapter is usable and work or not
|
|
188
|
-
#
|
|
189
|
-
def self.usable_and_works?(directories, options = {})
|
|
190
|
-
usable? && Array(directories).all? { |d| works?(d, options) }
|
|
191
|
-
end
|
|
192
|
-
|
|
193
|
-
# Checks if the adapter is usable on target OS.
|
|
194
|
-
#
|
|
195
|
-
# @return [Boolean] whether usable or not
|
|
196
|
-
#
|
|
197
|
-
def self.usable?
|
|
198
|
-
load_dependent_adapter if RbConfig::CONFIG['target_os'] =~ target_os_regex
|
|
199
|
-
end
|
|
200
|
-
|
|
201
|
-
# Load the adapter gem
|
|
202
|
-
#
|
|
203
|
-
# @return [Boolean] whether loaded or not
|
|
204
|
-
#
|
|
205
|
-
def self.load_dependent_adapter
|
|
206
|
-
return true if @loaded
|
|
207
|
-
require adapter_gem
|
|
208
|
-
return @loaded = true
|
|
209
|
-
end
|
|
210
|
-
|
|
211
|
-
# Runs a tests to determine if the adapter can actually pick up
|
|
212
|
-
# changes in a given directory and returns the result.
|
|
213
|
-
#
|
|
214
|
-
# @note This test takes some time depending on the adapter latency.
|
|
215
|
-
#
|
|
216
|
-
# @param [String, Pathname] directory the directory to watch
|
|
217
|
-
# @param [Hash] options the adapter options
|
|
218
|
-
# @option options [Float] latency the delay between checking for changes in seconds
|
|
219
|
-
#
|
|
220
|
-
# @return [Boolean] whether the adapter works or not
|
|
221
|
-
#
|
|
222
|
-
def self.works?(directory, options = {})
|
|
223
|
-
work = false
|
|
224
|
-
test_file = "#{directory}/.listen_test"
|
|
225
|
-
callback = lambda { |*| work = true }
|
|
226
|
-
adapter = self.new(directory, options, &callback)
|
|
227
|
-
adapter.start
|
|
228
|
-
|
|
229
|
-
FileUtils.touch(test_file)
|
|
230
|
-
|
|
231
|
-
t = Thread.new { sleep(adapter.latency * 5); adapter.stop }
|
|
232
|
-
|
|
233
|
-
adapter.wait_for_callback
|
|
234
|
-
work
|
|
235
|
-
ensure
|
|
236
|
-
Thread.kill(t) if t
|
|
237
|
-
FileUtils.rm(test_file, :force => true)
|
|
238
|
-
adapter.stop if adapter && adapter.started?
|
|
239
|
-
end
|
|
240
|
-
|
|
241
|
-
# Runs the callback and passes it the changes if there are any.
|
|
242
|
-
#
|
|
243
|
-
def report_changes
|
|
244
|
-
changed_dirs = nil
|
|
245
|
-
|
|
246
|
-
mutex.synchronize do
|
|
247
|
-
return if @changed_directories.empty?
|
|
248
|
-
changed_dirs = @changed_directories.to_a
|
|
249
|
-
@changed_directories.clear
|
|
250
|
-
end
|
|
251
|
-
|
|
252
|
-
callback.call(changed_dirs, {})
|
|
253
|
-
turnstile.signal
|
|
13
|
+
def self.new(listener)
|
|
14
|
+
adapter_class = _select(listener.options)
|
|
15
|
+
adapter_class.new(listener)
|
|
254
16
|
end
|
|
255
17
|
|
|
256
18
|
private
|
|
257
19
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
#
|
|
262
|
-
def default_latency
|
|
263
|
-
DEFAULT_LATENCY
|
|
264
|
-
end
|
|
265
|
-
|
|
266
|
-
# The thread on which the main thread should wait
|
|
267
|
-
# when the adapter has been started in blocking mode.
|
|
268
|
-
#
|
|
269
|
-
# @note This method can be overriden on a per-adapter basis.
|
|
270
|
-
#
|
|
271
|
-
def blocking_thread
|
|
272
|
-
worker_thread
|
|
273
|
-
end
|
|
274
|
-
|
|
275
|
-
# Initialize the adpater' specific worker.
|
|
276
|
-
#
|
|
277
|
-
# @note Each adapter must override this method
|
|
278
|
-
# to initialize its own @worker.
|
|
279
|
-
#
|
|
280
|
-
def initialize_worker
|
|
281
|
-
nil
|
|
282
|
-
end
|
|
20
|
+
def self._select(options)
|
|
21
|
+
return Polling if options[:force_polling] || not_mri?
|
|
22
|
+
return _usable_adapter_class if _usable_adapter_class
|
|
283
23
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
# @note Each adapter must override this method
|
|
287
|
-
# to start its worker on a new @worker_thread thread.
|
|
288
|
-
#
|
|
289
|
-
def start_worker
|
|
290
|
-
raise NotImplementedError, "#{self.class} cannot respond to: #{__method__}"
|
|
24
|
+
_warn_polling_fallback(options)
|
|
25
|
+
Polling
|
|
291
26
|
end
|
|
292
27
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
def start_poller
|
|
297
|
-
@poller_thread = Thread.new { poll_changed_directories }
|
|
28
|
+
def self._usable_adapter_class
|
|
29
|
+
adapters = OPTIMIZED_ADAPTERS.map { |adapter| Adapter.const_get(adapter) }
|
|
30
|
+
adapters.detect { |adapter| adapter.send(:usable?) }
|
|
298
31
|
end
|
|
299
32
|
|
|
300
|
-
|
|
301
|
-
# has been set to false.
|
|
302
|
-
#
|
|
303
|
-
# @param [String] warning an existing warning message
|
|
304
|
-
# @param [Hash] options the adapter options
|
|
305
|
-
# @option options [Boolean] polling_fallback_message to change polling fallback message or remove it
|
|
306
|
-
#
|
|
307
|
-
def self.warn_polling_fallback(options)
|
|
33
|
+
def self._warn_polling_fallback(options)
|
|
308
34
|
return if options[:polling_fallback_message] == false
|
|
309
35
|
|
|
310
|
-
warning = options
|
|
36
|
+
warning = options.fetch(:polling_fallback_message, POLLING_FALLBACK_MESSAGE)
|
|
311
37
|
Kernel.warn "[Listen warning]:\n#{warning.gsub(/^(.*)/, ' \1')}"
|
|
312
38
|
end
|
|
313
39
|
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
# @note This method can be overriden on a per-adapter basis.
|
|
317
|
-
#
|
|
318
|
-
def poll_changed_directories
|
|
319
|
-
until stopped
|
|
320
|
-
sleep(latency)
|
|
321
|
-
report_changes
|
|
322
|
-
end
|
|
40
|
+
def self.not_mri?
|
|
41
|
+
RUBY_ENGINE != 'ruby'
|
|
323
42
|
end
|
|
324
43
|
end
|
|
325
44
|
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
require 'listen/file'
|
|
2
|
+
require 'listen/directory'
|
|
3
|
+
|
|
4
|
+
module Listen
|
|
5
|
+
class Change
|
|
6
|
+
include Celluloid
|
|
7
|
+
|
|
8
|
+
attr_accessor :listener
|
|
9
|
+
|
|
10
|
+
def initialize(listener)
|
|
11
|
+
@listener = listener
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def change(path, options)
|
|
15
|
+
return if _silencer.silenced?(path)
|
|
16
|
+
if change = options[:change]
|
|
17
|
+
_notify_listener(change, path)
|
|
18
|
+
else
|
|
19
|
+
send("_#{options[:type].downcase}_change", path, options)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def _file_change(path, options)
|
|
26
|
+
change = File.new(path).change
|
|
27
|
+
if change && listener.listen? && !options[:silence]
|
|
28
|
+
_notify_listener(change, path)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def _dir_change(path, options)
|
|
33
|
+
Directory.new(path, options).scan
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def _notify_listener(change, path)
|
|
37
|
+
listener.changes << { change => path }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def _silencer
|
|
41
|
+
Celluloid::Actor[:listen_silencer]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
module Listen
|
|
2
|
+
class Directory
|
|
3
|
+
attr_accessor :path, :options
|
|
4
|
+
|
|
5
|
+
def initialize(path, options = {})
|
|
6
|
+
@path = path
|
|
7
|
+
@options = options
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def scan
|
|
11
|
+
_update_record
|
|
12
|
+
_all_entries.each do |entry_path, data|
|
|
13
|
+
case data[:type]
|
|
14
|
+
when 'File' then _async_change(entry_path, options.merge(type: 'File'))
|
|
15
|
+
when 'Dir'
|
|
16
|
+
_async_change(entry_path, options.merge(type: 'Dir')) if _recursive_scan?(entry_path)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def _update_record
|
|
24
|
+
if ::Dir.exists?(path)
|
|
25
|
+
_record.async.set_path(path, { type: 'Dir'})
|
|
26
|
+
else
|
|
27
|
+
_record.async.unset_path(path)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def _all_entries
|
|
32
|
+
_record_entries.merge(_entries)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def _entries
|
|
36
|
+
return {} unless ::Dir.exists?(path)
|
|
37
|
+
entries = ::Dir.entries(path) - %w[. ..]
|
|
38
|
+
entries = entries.map { |entry| [entry, type: _entry_type(entry)] }
|
|
39
|
+
Hash[*entries.flatten]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def _entry_type(entry_path)
|
|
43
|
+
entry_path = path.join(entry_path)
|
|
44
|
+
if entry_path.file?
|
|
45
|
+
'File'
|
|
46
|
+
elsif entry_path.directory?
|
|
47
|
+
'Dir'
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def _record_entries
|
|
52
|
+
future = _record.future.dir_entries(path)
|
|
53
|
+
future.value
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def _record
|
|
57
|
+
Celluloid::Actor[:listen_record]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def _change_pool
|
|
61
|
+
Celluloid::Actor[:listen_change_pool]
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def _recursive_scan?(path)
|
|
65
|
+
!::Dir.exists?(path) || options[:recursive]
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def _async_change(entry_path, options)
|
|
69
|
+
entry_path = path.join(entry_path)
|
|
70
|
+
_change_pool.async.change(entry_path, options)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|