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.
@@ -0,0 +1,108 @@
1
+ module Listen
2
+ class File
3
+ attr_accessor :path, :data
4
+
5
+ def initialize(path)
6
+ @path = path
7
+ @data = { type: 'File' }
8
+ end
9
+
10
+ def change
11
+ if _existing_path? && _modified?
12
+ _set_record_data
13
+ :modified
14
+ elsif _new_path?
15
+ _set_record_data
16
+ :added
17
+ elsif _removed_path?
18
+ _unset_record_data
19
+ :removed
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def _new_path?
26
+ _exist? && !_record_data?
27
+ end
28
+
29
+ def _existing_path?
30
+ _exist? && _record_data?
31
+ end
32
+
33
+ def _removed_path?
34
+ !_exist?
35
+ end
36
+
37
+ def _record_data?
38
+ !_record_data.empty?
39
+ end
40
+
41
+ def _exist?
42
+ @exist ||= ::File.exist?(path)
43
+ end
44
+
45
+ def _modified?
46
+ _mtime > _record_data[:mtime] || _mode_modified? || _content_modified?
47
+ end
48
+
49
+ def _mode_modified?
50
+ _mode != _record_data[:mode]
51
+ end
52
+
53
+ # Only useful on Darwin because of the file mtime second precision
54
+ #
55
+ def _content_modified?
56
+ _record_data[:md5] && _md5 != _record_data[:md5]
57
+ end
58
+
59
+ def _set_record_data
60
+ @data.merge!(_new_data)
61
+ _record.async.set_path(path, data)
62
+ end
63
+
64
+ def _unset_record_data
65
+ _record.async.unset_path(path)
66
+ end
67
+
68
+ # Only Darwin need md5 comparaison because of the file mtime second precision
69
+ #
70
+ def _new_data
71
+ data = { mtime: _mtime, mode: _mode }
72
+ data[:md5] = _md5 if RbConfig::CONFIG['target_os'] =~ /darwin/i
73
+ data
74
+ end
75
+
76
+ def _record_data
77
+ @_record_data ||= _record.future.file_data(path).value
78
+ end
79
+
80
+ def _record
81
+ Celluloid::Actor[:listen_record]
82
+ end
83
+
84
+ def _mtime
85
+ @mtime ||= _lstat.mtime.to_f
86
+ rescue
87
+ 0.0
88
+ end
89
+
90
+ def _mode
91
+ @mode ||= _lstat.mode
92
+ rescue
93
+ nil
94
+ end
95
+
96
+ def _lstat
97
+ @lstat ||= ::File.lstat(path)
98
+ rescue
99
+ nil
100
+ end
101
+
102
+ def _md5
103
+ @md5 ||= Digest::MD5.file(path).digest
104
+ rescue
105
+ nil
106
+ end
107
+ end
108
+ end
@@ -1,27 +1,19 @@
1
1
  require 'pathname'
2
+ require 'listen/adapter'
3
+ require 'listen/change'
4
+ require 'listen/record'
5
+ require 'listen/silencer'
2
6
 
3
7
  module Listen
4
8
  class Listener
5
- attr_reader :directories, :directories_records, :block, :adapter, :adapter_options, :use_relative_paths
6
-
7
- BLOCKING_PARAMETER_DEPRECATION_MESSAGE = <<-EOS.gsub(/^\s*/, '')
8
- The blocking parameter of Listen::Listener#start is deprecated.\n
9
- Please use Listen::Adapter#start for a non-blocking listener and Listen::Listener#start! for a blocking one.
10
- EOS
9
+ attr_accessor :options, :directories, :paused, :changes, :block, :thread
11
10
 
12
11
  RELATIVE_PATHS_WITH_MULTIPLE_DIRECTORIES_WARNING_MESSAGE = "The relative_paths option doesn't work when listening to multiple diretories."
13
12
 
14
13
  # Initializes the directories listener.
15
14
  #
16
15
  # @param [String] directory the directories to listen to
17
- # @param [Hash] options the listen options
18
- # @option options [Regexp] ignore a pattern for ignoring paths
19
- # @option options [Regexp] filter a pattern for filtering paths
20
- # @option options [Float] latency the delay between checking for changes in seconds
21
- # @option options [Boolean] relative_paths whether or not to use relative-paths in the callback
22
- # @option options [Boolean] force_polling whether to force the polling adapter or not
23
- # @option options [String, Boolean] polling_fallback_message to change polling fallback message or remove it
24
- # @option options [Class] force_adapter force the use of this adapter class, skipping usual adapter selection
16
+ # @param [Hash] options the listen options (see Listen::Listener::Options)
25
17
  #
26
18
  # @yield [modified, added, removed] the changed files
27
19
  # @yieldparam [Array<String>] modified the list of modified files
@@ -29,295 +21,134 @@ module Listen
29
21
  # @yieldparam [Array<String>] removed the list of removed files
30
22
  #
31
23
  def initialize(*args, &block)
32
- options = args.last.is_a?(Hash) ? args.pop : {}
33
- directories = args.flatten
34
- initialize_directories_and_directories_records(directories)
35
- initialize_relative_paths_usage(options)
36
- @block = block
37
-
38
- ignore(*options.delete(:ignore))
39
- filter(*options.delete(:filter))
40
-
41
- @adapter_options = options
24
+ @options = _init_options(args.last.is_a?(Hash) ? args.pop : {})
25
+ @directories = args.flatten.map { |path| Pathname.new(path).realpath }
26
+ @changes = []
27
+ @block = block
28
+ _init_debug
42
29
  end
43
30
 
44
31
  # Starts the listener by initializing the adapter and building
45
32
  # the directory record concurrently, then it starts the adapter to watch
46
33
  # for changes. The current thread is not blocked after starting.
47
34
  #
48
- # @see Listen::Listener#start!
49
- #
50
- def start(deprecated_blocking = nil)
51
- Kernel.warn "[Listen warning]:\n#{BLOCKING_PARAMETER_DEPRECATION_MESSAGE}" unless deprecated_blocking.nil?
52
- setup
53
- adapter.start
54
- end
55
-
56
- # Starts the listener by initializing the adapter and building
57
- # the directory record concurrently, then it starts the adapter to watch
58
- # for changes. The current thread is blocked after starting.
59
- #
60
- # @see Listen::Listener#start
61
- #
62
- # @since 1.0.0
63
- #
64
- def start!
65
- setup
66
- adapter.start!
35
+ def start
36
+ _signals_trap
37
+ _init_actors
38
+ unpause
39
+ Celluloid::Actor[:listen_adapter].async.start
40
+ @thread = Thread.new { _wait_for_changes }
67
41
  end
68
42
 
69
- # Stops the listener.
43
+ # Terminates all Listen actors and kill the adapter.
70
44
  #
71
45
  def stop
72
- adapter && adapter.stop
46
+ thread.kill
47
+ Celluloid::Actor.kill(Celluloid::Actor[:listen_adapter])
48
+ Celluloid::Actor[:listen_silencer].terminate
49
+ Celluloid::Actor[:listen_change_pool].terminate
50
+ Celluloid::Actor[:listen_record].terminate
73
51
  end
74
52
 
75
- # Pauses the listener.
76
- #
77
- # @return [Listen::Listener] the listener
53
+ # Pauses listening callback (adapter still running)
78
54
  #
79
55
  def pause
80
- adapter.pause
81
- self
56
+ @paused = true
82
57
  end
83
58
 
84
- # Unpauses the listener.
85
- #
86
- # @return [Listen::Listener] the listener
59
+ # Unpauses listening callback
87
60
  #
88
61
  def unpause
89
- build_directories_records
90
- adapter.unpause
91
- self
62
+ Celluloid::Actor[:listen_record].build
63
+ @paused = false
92
64
  end
93
65
 
94
- # Returns whether the listener is paused or not.
66
+ # Returns true if Listener is paused
95
67
  #
96
- # @return [Boolean] adapter paused status
68
+ # @return [Boolean]
97
69
  #
98
70
  def paused?
99
- !!adapter && adapter.paused?
71
+ @paused == true
100
72
  end
101
73
 
102
- # Adds ignoring patterns to the listener.
103
- #
104
- # @param (see Listen::DirectoryRecord#ignore)
74
+ # Returns true if Listener is not paused
105
75
  #
106
- # @return [Listen::Listener] the listener
76
+ # @return [Boolean]
107
77
  #
108
- # @see Listen::DirectoryRecord#ignore
109
- #
110
- def ignore(*regexps)
111
- directories_records.each { |r| r.ignore(*regexps) }
112
- self
78
+ def listen?
79
+ @paused == false
113
80
  end
114
81
 
115
- # Replaces ignoring patterns in the listener.
116
- #
117
- # @param (see Listen::DirectoryRecord#ignore!)
118
- #
119
- # @return [Listen::Listener] the listener
82
+ # Adds ignore patterns to the existing one (See DEFAULT_IGNORED_DIRECTORIES and DEFAULT_IGNORED_EXTENSIONS in Listen::Silencer)
120
83
  #
121
- # @see Listen::DirectoryRecord#ignore!
84
+ # @param [Regexp, Hash<Regexp>] new ignoring patterns.
122
85
  #
123
- def ignore!(*regexps)
124
- directories_records.each { |r| r.ignore!(*regexps) }
125
- self
86
+ def ignore(regexps)
87
+ options[:ignore] = [options[:ignore], regexps]
88
+ Celluloid::Actor[:listen_silencer] = Silencer.new(options)
126
89
  end
127
90
 
128
- # Adds filtering patterns to the listener.
91
+ # Overwrites ignore patterns (See DEFAULT_IGNORED_DIRECTORIES and DEFAULT_IGNORED_EXTENSIONS in Listen::Silencer)
129
92
  #
130
- # @param (see Listen::DirectoryRecord#filter)
93
+ # @param [Regexp, Hash<Regexp>] new ignoring patterns.
131
94
  #
132
- # @return [Listen::Listener] the listener
133
- #
134
- # @see Listen::DirectoryRecord#filter
135
- #
136
- def filter(*regexps)
137
- directories_records.each { |r| r.filter(*regexps) }
138
- self
95
+ def ignore!(regexps)
96
+ options.delete(:ignore)
97
+ options[:ignore!] = regexps
98
+ Celluloid::Actor[:listen_silencer] = Silencer.new(options)
139
99
  end
140
100
 
141
- # Replaces filtering patterns in the listener.
142
- #
143
- # @param (see Listen::DirectoryRecord#filter!)
144
- #
145
- # @return [Listen::Listener] the listener
146
- #
147
- # @see Listen::DirectoryRecord#filter!
148
- #
149
- def filter!(*regexps)
150
- directories_records.each { |r| r.filter!(*regexps) }
151
- self
152
- end
153
-
154
- # Sets the latency for the adapter. This is a helper method
155
- # to simplify changing the latency directly from the listener.
156
- #
157
- # @example Wait 0.5 seconds each time before checking changes
158
- # latency 0.5
159
- #
160
- # @param [Float] seconds the amount of delay, in seconds
161
- #
162
- # @return [Listen::Listener] the listener
163
- #
164
- def latency(seconds)
165
- @adapter_options[:latency] = seconds
166
- self
167
- end
168
-
169
- # Sets whether the use of the polling adapter
170
- # should be forced or not.
171
- #
172
- # @example Forcing the use of the polling adapter
173
- # force_polling true
174
- #
175
- # @param [Boolean] value whether to force the polling adapter or not
176
- #
177
- # @return [Listen::Listener] the listener
178
- #
179
- def force_polling(value)
180
- @adapter_options[:force_polling] = value
181
- self
182
- end
101
+ private
183
102
 
184
- # Sets whether to force the use of a particular adapter, rather than
185
- # going through usual adapter selection process on start.
186
- #
187
- # @example Force use of Linux polling
188
- # force_adapter Listen::Adapters::Linux
189
- #
190
- # @param [Class] adapter class to use for file system event notification.
191
- #
192
- # @return [Listen::Listener] the listener
193
- #
194
- def force_adapter(adapter_class)
195
- @adapter_options[:force_adapter] = adapter_class
196
- self
103
+ def _init_options(options = {})
104
+ { debug: false,
105
+ latency: nil,
106
+ force_polling: false,
107
+ polling_fallback_message: nil }.merge(options)
197
108
  end
198
109
 
199
- # Sets whether the paths in the callback should be
200
- # relative or absolute.
201
- #
202
- # @example Enabling relative paths in the callback
203
- # relative_paths true
204
- #
205
- # @param [Boolean] value whether to enable relative paths in the callback or not
206
- #
207
- # @return [Listen::Listener] the listener
208
- #
209
- def relative_paths(value)
210
- @use_relative_paths = value
211
- self
110
+ def _init_debug
111
+ if options[:debug]
112
+ Celluloid.logger.level = Logger::INFO
113
+ else
114
+ Celluloid.logger = nil
115
+ end
212
116
  end
213
117
 
214
- # Defines a custom polling fallback message or disable it.
215
- #
216
- # @example Disabling the polling fallback message
217
- # polling_fallback_message false
218
- #
219
- # @param [String, Boolean] value to change polling fallback message or remove it
220
- #
221
- # @return [Listen::Listener] the listener
222
- #
223
- def polling_fallback_message(value)
224
- @adapter_options[:polling_fallback_message] = value
225
- self
118
+ def _init_actors
119
+ cores = Celluloid.cores || 2
120
+ Celluloid::Actor[:listen_silencer] = Silencer.new(options)
121
+ Celluloid::Actor[:listen_change_pool] = Change.pool(size: cores, args: self)
122
+ Celluloid::Actor[:listen_adapter] = Adapter.new(self)
123
+ Celluloid::Actor[:listen_record] = Record.new(self)
226
124
  end
227
125
 
228
- # Sets the callback that gets called on changes.
229
- #
230
- # @example Assign a callback to be called on changes
231
- # callback = lambda { |modified, added, removed| ... }
232
- # change &callback
233
- #
234
- # @param [Proc] block the callback proc
235
- #
236
- # @return [Listen::Listener] the listener
237
- #
238
- def change(&block) # modified, added, removed
239
- @block = block
240
- self
126
+ def _signals_trap
127
+ if Signal.list.keys.include?('INT')
128
+ Signal.trap('INT') { exit }
129
+ end
241
130
  end
242
131
 
243
- # Runs the callback passing it the changes if there are any.
244
- #
245
- # @param (see Listen::DirectoryRecord#fetch_changes)
246
- #
247
- # @see Listen::DirectoryRecord#fetch_changes
248
- #
249
- def on_change(directories, options = {})
250
- changes = fetch_records_changes(directories, options)
251
- unless changes.values.all? { |paths| paths.empty? }
252
- block.call(changes[:modified], changes[:added], changes[:removed])
132
+ def _wait_for_changes
133
+ loop do
134
+ changes = _pop_changes
135
+ unless changes.all? { |_,v| v.empty? }
136
+ block.call(changes[:modified], changes[:added], changes[:removed])
137
+ end
138
+ sleep 0.1
253
139
  end
254
140
  rescue => ex
255
141
  Kernel.warn "[Listen warning]: Change block raise an execption: #{$!}"
256
142
  Kernel.warn "Backtrace:\n\t#{ex.backtrace.join("\n\t")}"
257
143
  end
258
144
 
259
- private
260
-
261
- # Initializes the directories to watch as well as the directories records.
262
- #
263
- # @see Listen::DirectoryRecord
264
- #
265
- def initialize_directories_and_directories_records(directories)
266
- @directories = directories.map { |d| Pathname.new(d).realpath.to_s }
267
- @directories_records = directories.map { |d| DirectoryRecord.new(d) }
268
- end
269
-
270
- # Initializes whether or not using relative paths.
271
- #
272
- def initialize_relative_paths_usage(options)
273
- if directories.size > 1 && options[:relative_paths]
274
- Kernel.warn "[Listen warning]: #{RELATIVE_PATHS_WITH_MULTIPLE_DIRECTORIES_WARNING_MESSAGE}"
145
+ def _pop_changes
146
+ changes = { modified: [], added: [], removed: [] }
147
+ until @changes.empty?
148
+ change = @changes.pop
149
+ change.each { |k, v| changes[k] << v.to_s }
275
150
  end
276
- @use_relative_paths = directories.one? && options.delete(:relative_paths) { false }
277
- end
278
-
279
- # Build the directory record concurrently and initialize the adapter.
280
- #
281
- def setup
282
- t = Thread.new { build_directories_records }
283
- @adapter = initialize_adapter
284
- t.join
285
- end
286
-
287
- # Initializes an adapter passing it the callback and adapters' options.
288
- #
289
- def initialize_adapter
290
- callback = lambda { |changed_directories, options| self.on_change(changed_directories, options) }
291
- Adapter.select_and_initialize(directories, adapter_options, &callback)
292
- end
293
-
294
- # Build the watched directories' records.
295
- #
296
- def build_directories_records
297
- directories_records.each { |r| r.build }
151
+ changes.each { |_, v| v.uniq! }
298
152
  end
299
-
300
- # Returns the sum of all the changes to the directories records
301
- #
302
- # @param (see Listen::DirectoryRecord#fetch_changes)
303
- #
304
- # @return [Hash] the changes
305
- #
306
- def fetch_records_changes(directories_to_search, options)
307
- directories_records.inject({}) do |h, r|
308
- # directory records skips paths outside their range, so passing the
309
- # whole `directories` array is not a problem.
310
- record_changes = r.fetch_changes(directories_to_search, options.merge(:relative_paths => use_relative_paths))
311
-
312
- if h.empty?
313
- h.merge!(record_changes)
314
- else
315
- h.each { |k, v| h[k] += record_changes[k] }
316
- end
317
-
318
- h
319
- end
320
- end
321
-
322
153
  end
323
154
  end
@@ -0,0 +1,41 @@
1
+ module Listen
2
+ class Record
3
+ include Celluloid
4
+
5
+ attr_accessor :paths, :listener
6
+
7
+ def initialize(listener)
8
+ @listener = listener
9
+ @paths = _init_paths
10
+ end
11
+
12
+ def set_path(path, data)
13
+ @paths[::File.dirname(path)][::File.basename(path)] = file_data(path).merge(data)
14
+ end
15
+
16
+ def unset_path(path)
17
+ @paths[::File.dirname(path)].delete(::File.basename(path))
18
+ end
19
+
20
+ def file_data(path)
21
+ @paths[::File.dirname(path)][::File.basename(path)] || {}
22
+ end
23
+
24
+ def dir_entries(path)
25
+ @paths[path.to_s]
26
+ end
27
+
28
+ def build
29
+ @paths = _init_paths
30
+ listener.directories.each do |path|
31
+ Actor[:listen_change_pool].change(path, type: 'Dir', recursive: true, silence: true)
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def _init_paths
38
+ Hash.new { |h, k| h[k] = Hash.new }
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,46 @@
1
+ module Listen
2
+ class Silencer
3
+ include Celluloid
4
+
5
+ # The default list of directories that get ignored.
6
+ DEFAULT_IGNORED_DIRECTORIES = %w[.bundle .git .hg .rbx .svn bundle log tmp vendor/ruby]
7
+
8
+ # The default list of files that get ignored.
9
+ DEFAULT_IGNORED_EXTENSIONS = %w[.DS_Store]
10
+
11
+ attr_accessor :options, :patterns
12
+
13
+ def initialize(options = {})
14
+ @options = options
15
+ _init_patterns
16
+ end
17
+
18
+ def silenced?(path)
19
+ patterns.any? { |pattern| path.to_s =~ pattern }
20
+ end
21
+
22
+ private
23
+
24
+ def _init_patterns
25
+ @patterns = []
26
+ @patterns << _default_patterns unless options[:ignore!]
27
+ @patterns << options[:ignore] << options[:ignore!]
28
+ @patterns.compact!
29
+ @patterns.flatten!
30
+ end
31
+
32
+ def _default_patterns
33
+ [_default_ignored_directories_patterns, _default_ignored_extensions_patterns]
34
+ end
35
+
36
+ def _default_ignored_directories_patterns
37
+ ignored_directories = DEFAULT_IGNORED_DIRECTORIES.map { |d| Regexp.escape(d) }
38
+ %r{(?:#{ignored_directories.join('|')})}
39
+ end
40
+
41
+ def _default_ignored_extensions_patterns
42
+ ignored_extensions = DEFAULT_IGNORED_EXTENSIONS.map { |e| Regexp.escape(e) }
43
+ %r{(?:#{ignored_extensions.join('|')})$}
44
+ end
45
+ end
46
+ end
@@ -1,3 +1,3 @@
1
1
  module Listen
2
- VERSION = '1.3.1'
2
+ VERSION = "2.0.0"
3
3
  end
data/lib/listen.rb CHANGED
@@ -1,18 +1,9 @@
1
- require 'listen/turnstile'
1
+ require 'celluloid'
2
2
  require 'listen/listener'
3
- require 'listen/directory_record'
4
- require 'listen/adapter'
5
3
 
6
4
  module Listen
7
5
 
8
- module Adapters
9
- Adapter::ADAPTERS.each do |adapter|
10
- require "listen/adapters/#{adapter.downcase}"
11
- end
12
- end
13
-
14
6
  # Listens to file system modifications on a either single directory or multiple directories.
15
- # When calling this method, the current thread is not blocked.
16
7
  #
17
8
  # @param (see Listen::Listener#new)
18
9
  #
@@ -21,34 +12,9 @@ module Listen
21
12
  # @yieldparam [Array<String>] added the list of added files
22
13
  # @yieldparam [Array<String>] removed the list of removed files
23
14
  #
24
- # @return [Listen::Listener] the file listener if no block given
15
+ # @return [Listen::Listener] the listener
25
16
  #
26
17
  def self.to(*args, &block)
27
- listener = _init_listener(*args, &block)
28
-
29
- block ? listener.start : listener
30
- end
31
-
32
- # Listens to file system modifications on a either single directory or multiple directories.
33
- # When calling this method, the current thread is blocked.
34
- #
35
- # @param (see Listen::Listener#new)
36
- #
37
- # @yield [modified, added, removed] the changed files
38
- # @yieldparam [Array<String>] modified the list of modified files
39
- # @yieldparam [Array<String>] added the list of added files
40
- # @yieldparam [Array<String>] removed the list of removed files
41
- #
42
- # @since 1.0.0
43
- #
44
- def self.to!(*args, &block)
45
- _init_listener(*args, &block).start!
46
- end
47
-
48
- # @private
49
- #
50
- def self._init_listener(*args, &block)
51
18
  Listener.new(*args, &block)
52
19
  end
53
-
54
20
  end