listen 3.3.1 → 3.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e11b5e927f693205d422a0ea65147a53f65b08acde3a5dec7b370ac3f66b9707
4
- data.tar.gz: 29bf36d64485cbbdc806451381d402067796430d8a5cd6ca1903a394d73d2c84
3
+ metadata.gz: a573b56820a3108a7d35562e1d502da64c15f110ce755119e0a7f8dde18d76fc
4
+ data.tar.gz: 2be3e9a4e9f6e46171ccb1429fde63c07255c11923a882c63dd1c1ba8afb2ec3
5
5
  SHA512:
6
- metadata.gz: 18fa31e48db9c6b9f99108e2dabed785ba15409d0d79b3b2bbcc47be0ea6c55b82418e44ce48639b32fe137fa0871623b8e593c1ffbaeee18fa4847c484cd3d9
7
- data.tar.gz: a2cec7e23abba56a0b47e2f736d8fe37cb01a574da6a54049156eace74af67cf3783f2cbded1f7a2778ae1e26bbc75342144f2c16b05a30ced06ad7f91fa4e9e
6
+ metadata.gz: 17013d995bc4e0b4a0b0b30199854960e2eb4240a21ee8329ad04be4ab3db778f908f1a583e53956f838d0bba956f6ad6133bac406aebd77caf3f400ed7d82c8
7
+ data.tar.gz: 38d254a77bbfcc28046ffd7a396cf26215bdaa95a9165f20ec1c0cd932f2e6eb88d29c5b3f4db45c6729a6f7e454ab0621c5e9959f5f70521477a8b8baa21497
data/bin/listen CHANGED
@@ -1,12 +1,11 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'listen'
4
5
  require 'listen/cli'
5
6
 
6
- unless defined?(JRUBY_VERSION)
7
- if Signal.list.keys.include?('INT')
8
- Signal.trap('INT') { Thread.new { Listen.stop } }
9
- end
7
+ if !defined?(JRUBY_VERSION) && Signal.list.keys.include?('INT')
8
+ Signal.trap('INT') { Thread.new { Listen.stop } }
10
9
  end
11
10
 
12
11
  Listen::CLI.start
@@ -11,7 +11,7 @@ module Listen
11
11
  module Adapter
12
12
  OPTIMIZED_ADAPTERS = [Darwin, Linux, BSD, Windows].freeze
13
13
  POLLING_FALLBACK_MESSAGE = 'Listen will be polling for changes.'\
14
- 'Learn more at https://github.com/guard/listen#listen-adapters.'.freeze
14
+ 'Learn more at https://github.com/guard/listen#listen-adapters.'
15
15
 
16
16
  class << self
17
17
  def select(options = {})
@@ -24,14 +24,14 @@ module Listen
24
24
  Polling
25
25
  rescue
26
26
  Listen.logger.warn format('Adapter: failed: %s:%s', $ERROR_POSITION.inspect,
27
- $ERROR_POSITION * "\n")
27
+ $ERROR_POSITION * "\n")
28
28
  raise
29
29
  end
30
30
 
31
31
  private
32
32
 
33
33
  def _usable_adapter_class
34
- OPTIMIZED_ADAPTERS.detect(&:usable?)
34
+ OPTIMIZED_ADAPTERS.find(&:usable?)
35
35
  end
36
36
 
37
37
  def _warn_polling_fallback(options)
@@ -8,13 +8,11 @@ require 'listen/thread'
8
8
  module Listen
9
9
  module Adapter
10
10
  class Base
11
- attr_reader :options
11
+ attr_reader :options, :config
12
12
 
13
13
  # TODO: only used by tests
14
14
  DEFAULTS = {}.freeze
15
15
 
16
- attr_reader :config
17
-
18
16
  def initialize(config)
19
17
  @started = false
20
18
  @config = config
@@ -31,6 +29,7 @@ module Listen
31
29
  end
32
30
 
33
31
  # TODO: it's a separate method as a temporary workaround for tests
32
+ # rubocop:disable Metrics/MethodLength
34
33
  def configure
35
34
  if @configured
36
35
  Listen.logger.warn('Adapter already configured!')
@@ -57,6 +56,7 @@ module Listen
57
56
  @snapshots[dir] = snapshot
58
57
  end
59
58
  end
59
+ # rubocop:enable Metrics/MethodLength
60
60
 
61
61
  def started?
62
62
  @started
@@ -73,7 +73,7 @@ module Listen
73
73
  @started = true
74
74
 
75
75
  @run_thread = Listen::Thread.new("run_thread") do
76
- @snapshots.values.each do |snapshot|
76
+ @snapshots.each_value do |snapshot|
77
77
  _timed('Record.build()') { snapshot.record.build }
78
78
  end
79
79
  _run
@@ -7,7 +7,7 @@
7
7
  module Listen
8
8
  module Adapter
9
9
  class BSD < Base
10
- OS_REGEXP = /bsd|dragonfly/i
10
+ OS_REGEXP = /bsd|dragonfly/i.freeze
11
11
 
12
12
  DEFAULTS = {
13
13
  events: [
@@ -73,8 +73,7 @@ module Listen
73
73
  def _change(event_flags)
74
74
  { modified: [:attrib, :extend],
75
75
  added: [:write],
76
- removed: [:rename, :delete]
77
- }.each do |change, flags|
76
+ removed: [:rename, :delete] }.each do |change, flags|
78
77
  return change unless (flags & event_flags).empty?
79
78
  end
80
79
  nil
@@ -87,7 +86,7 @@ module Listen
87
86
  def _watch_for_new_file(event)
88
87
  queue = event.watcher.queue
89
88
  _find(_event_path(event).to_s) do |file_path|
90
- unless queue.watchers.detect { |_, v| v.path == file_path.to_s }
89
+ unless queue.watchers.find { |_, v| v.path == file_path.to_s }
91
90
  _watch_file(file_path, queue)
92
91
  end
93
92
  end
@@ -5,10 +5,7 @@ require 'pathname'
5
5
  module Listen
6
6
  module Adapter
7
7
  class Config
8
- attr_reader :directories
9
- attr_reader :silencer
10
- attr_reader :queue
11
- attr_reader :adapter_options
8
+ attr_reader :directories, :silencer, :queue, :adapter_options
12
9
 
13
10
  def initialize(directories, queue, silencer, adapter_options)
14
11
  # Default to current directory if no directories are supplied
@@ -7,7 +7,7 @@ module Listen
7
7
  # Adapter implementation for Mac OS X `FSEvents`.
8
8
  #
9
9
  class Darwin < Base
10
- OS_REGEXP = /darwin(?<major_version>(1|2)\d+)/i
10
+ OS_REGEXP = /darwin(?<major_version>(1|2)\d+)/i.freeze
11
11
 
12
12
  # The default delay between checking for changes.
13
13
  DEFAULTS = { latency: 0.1 }.freeze
@@ -51,7 +51,7 @@ module Listen
51
51
 
52
52
  def _process_changes(dirs)
53
53
  dirs.each do |dir|
54
- dir = Pathname.new(dir.sub(%r{\/$}, ''))
54
+ dir = Pathname.new(dir.sub(%r{/$}, ''))
55
55
 
56
56
  @callbacks.each do |watched_dir, callback|
57
57
  if watched_dir.eql?(dir) || Listen::Directory.ascendant_of?(watched_dir, dir)
@@ -4,7 +4,7 @@ module Listen
4
4
  module Adapter
5
5
  # @see https://github.com/nex3/rb-inotify
6
6
  class Linux < Base
7
- OS_REGEXP = /linux/i
7
+ OS_REGEXP = /linux/i.freeze
8
8
 
9
9
  DEFAULTS = {
10
10
  events: [
@@ -22,7 +22,7 @@ module Listen
22
22
  private
23
23
 
24
24
  WIKI_URL = 'https://github.com/guard/listen'\
25
- '/wiki/Increasing-the-amount-of-inotify-watchers'.freeze
25
+ '/wiki/Increasing-the-amount-of-inotify-watchers'
26
26
 
27
27
  INOTIFY_LIMIT_MESSAGE = <<-EOS.gsub(/^\s*/, '')
28
28
  FATAL: Listen error: unable to monitor directories for changes.
@@ -41,6 +41,7 @@ module Listen
41
41
  @worker.run
42
42
  end
43
43
 
44
+ # rubocop:disable Metrics/MethodLength
44
45
  def _process_event(dir, event)
45
46
  # NOTE: avoid using event.absolute_name since new API
46
47
  # will need to have a custom recursion implemented
@@ -73,6 +74,7 @@ module Listen
73
74
 
74
75
  _queue_change(:file, dir, rel_path, params)
75
76
  end
77
+ # rubocop:enable Metrics/MethodLength
76
78
 
77
79
  def _skip_event?(event)
78
80
  # Event on root directory
@@ -8,7 +8,7 @@ module Listen
8
8
  # file IO than the other implementations.
9
9
  #
10
10
  class Polling < Base
11
- OS_REGEXP = // # match every OS
11
+ OS_REGEXP = //.freeze # match every OS
12
12
 
13
13
  DEFAULTS = { latency: 1.0, wait_for_delay: 0.05 }.freeze
14
14
 
@@ -5,7 +5,7 @@ module Listen
5
5
  # Adapter implementation for Windows `wdm`.
6
6
  #
7
7
  class Windows < Base
8
- OS_REGEXP = /mswin|mingw|cygwin/i
8
+ OS_REGEXP = /mswin|mingw|cygwin/i.freeze
9
9
 
10
10
  BUNDLER_DECLARE_GEM = <<-EOS.gsub(/^ {6}/, '')
11
11
  Please add the following to your Gemfile to avoid polling for changes:
@@ -18,7 +18,7 @@ module Listen
18
18
  true
19
19
  rescue LoadError
20
20
  Listen.logger.debug format('wdm - load failed: %s:%s', $ERROR_INFO,
21
- $ERROR_POSITION * "\n")
21
+ $ERROR_POSITION * "\n")
22
22
 
23
23
  Kernel.warn BUNDLER_DECLARE_GEM
24
24
  false
@@ -38,8 +38,7 @@ module Listen
38
38
  yield([:dir, change])
39
39
  end
40
40
 
41
- events = [:attributes, :last_write]
42
- @worker.watch_recursively(dir.to_s, *events) do |change|
41
+ @worker.watch_recursively(dir.to_s, :attributes, :last_write) do |change|
43
42
  yield([:attr, change])
44
43
  end
45
44
  end
@@ -48,6 +47,7 @@ module Listen
48
47
  @worker.run!
49
48
  end
50
49
 
50
+ # rubocop:disable Metrics/MethodLength
51
51
  def _process_event(dir, event)
52
52
  Listen.logger.debug "wdm - callback: #{event.inspect}"
53
53
 
@@ -67,10 +67,11 @@ module Listen
67
67
  _queue_change(:file, dir, rel_path, options)
68
68
  end
69
69
  when :dir
70
- if change.type == :removed
70
+ case change.type
71
+ when :removed
71
72
  # TODO: check if watched dir?
72
73
  _queue_change(:dir, dir, Pathname(rel_path).dirname.to_s, {})
73
- elsif change.type == :added
74
+ when :added
74
75
  _queue_change(:dir, dir, rel_path, {})
75
76
  # do nothing - changed directory means either:
76
77
  # - removed subdirs (handled above)
@@ -80,20 +81,15 @@ module Listen
80
81
  # so what's left?
81
82
  end
82
83
  end
83
- rescue
84
- details = event.inspect
85
- Listen.logger.error format('wdm - callback (%s): %s:%s', details, $ERROR_INFO,
86
- $ERROR_POSITION * "\n")
87
- raise
88
84
  end
85
+ # rubocop:enable Metrics/MethodLength
89
86
 
90
87
  def _change(type)
91
88
  { modified: [:modified, :attrib], # TODO: is attrib really passed?
92
89
  added: [:added, :renamed_new_file],
93
- removed: [:removed, :renamed_old_file] }.each do |change, types|
94
- return change if types.include?(type)
90
+ removed: [:removed, :renamed_old_file] }.find do |change, types|
91
+ types.include?(type) and break change
95
92
  end
96
- nil
97
93
  end
98
94
  end
99
95
  end
@@ -30,13 +30,16 @@ module Listen
30
30
  end
31
31
 
32
32
  # Invalidate some part of the snapshot/record (dir, file, subtree, etc.)
33
+ # rubocop:disable Metrics/MethodLength
34
+ # rubocop:disable Metrics/CyclomaticComplexity
35
+ # rubocop:disable Metrics/PerceivedComplexity
33
36
  def invalidate(type, rel_path, options)
34
37
  watched_dir = Pathname.new(record.root)
35
38
 
36
39
  change = options[:change]
37
40
  cookie = options[:cookie]
38
41
 
39
- if !cookie && config.silenced?(rel_path, type)
42
+ if !cookie && @config.silenced?(rel_path, type)
40
43
  Listen.logger.debug { "(silenced): #{rel_path.inspect}" }
41
44
  return
42
45
  end
@@ -50,29 +53,17 @@ module Listen
50
53
 
51
54
  if change
52
55
  options = cookie ? { cookie: cookie } : {}
53
- config.queue(type, change, watched_dir, rel_path, options)
56
+ @config.queue(type, change, watched_dir, rel_path, options)
54
57
  elsif type == :dir
55
58
  # NOTE: POSSIBLE RECURSION
56
59
  # TODO: fix - use a queue instead
57
60
  Directory.scan(self, rel_path, options)
58
- else
59
- change = File.change(record, rel_path)
60
- return if !change || options[:silence]
61
- config.queue(:file, change, watched_dir, rel_path)
61
+ elsif (change = File.change(record, rel_path)) && !options[:silence]
62
+ @config.queue(:file, change, watched_dir, rel_path)
62
63
  end
63
- rescue RuntimeError => ex
64
- msg = format(
65
- '%s#%s crashed %s:%s',
66
- self.class,
67
- __method__,
68
- exinspect,
69
- ex.backtrace * "\n")
70
- Listen.logger.error(msg)
71
- raise
72
64
  end
73
-
74
- private
75
-
76
- attr_reader :config
65
+ # rubocop:enable Metrics/MethodLength
66
+ # rubocop:enable Metrics/CyclomaticComplexity
67
+ # rubocop:enable Metrics/PerceivedComplexity
77
68
  end
78
69
  end
@@ -35,6 +35,7 @@ module Listen
35
35
 
36
36
  class Forwarder
37
37
  attr_reader :logger
38
+
38
39
  def initialize(options)
39
40
  @options = options
40
41
  @logger = ::Logger.new(STDOUT, level: ::Logger::INFO)
@@ -43,6 +44,7 @@ module Listen
43
44
 
44
45
  def start
45
46
  logger.info 'Starting listen...'
47
+
46
48
  directory = @options[:directory]
47
49
  relative = @options[:relative]
48
50
  callback = proc do |modified, added, removed|
@@ -53,10 +55,7 @@ module Listen
53
55
  end
54
56
  end
55
57
 
56
- listener = Listen.to(
57
- directory,
58
- relative: relative,
59
- &callback)
58
+ listener = Listen.to(directory, relative: relative, &callback)
60
59
 
61
60
  listener.start
62
61
 
@@ -5,6 +5,7 @@ require 'set'
5
5
  module Listen
6
6
  # TODO: refactor (turn it into a normal object, cache the stat, etc)
7
7
  class Directory
8
+ # rubocop:disable Metrics/MethodLength
8
9
  def self.scan(snapshot, rel_path, options)
9
10
  record = snapshot.record
10
11
  dir = Pathname.new(record.root)
@@ -38,22 +39,19 @@ module Listen
38
39
  previous = previous.reject { |entry, _| current.include? path + entry }
39
40
 
40
41
  _async_changes(snapshot, Pathname.new(rel_path), previous, options)
41
-
42
42
  rescue Errno::ENOENT, Errno::EHOSTDOWN
43
43
  record.unset_path(rel_path)
44
44
  _async_changes(snapshot, Pathname.new(rel_path), previous, options)
45
-
46
45
  rescue Errno::ENOTDIR
47
46
  # TODO: path not tested
48
47
  record.unset_path(rel_path)
49
48
  _async_changes(snapshot, path, previous, options)
50
49
  _change(snapshot, :file, rel_path, options)
51
50
  rescue
52
- Listen.logger.warn do
53
- format('scan DIED: %s:%s', $ERROR_INFO, $ERROR_POSITION * "\n")
54
- end
51
+ Listen.logger.warn { format('scan DIED: %s:%s', $ERROR_INFO, $ERROR_POSITION * "\n") }
55
52
  raise
56
53
  end
54
+ # rubocop:enable Metrics/MethodLength
57
55
 
58
56
  def self.ascendant_of?(base, other)
59
57
  other.ascend do |ascendant|
@@ -88,8 +86,8 @@ module Listen
88
86
  # https://github.com/jruby/jruby/issues/3840
89
87
  exists = path.exist?
90
88
  directory = path.directory?
91
- return path.children unless exists && !directory
92
- raise Errno::ENOTDIR, path.to_s
89
+ exists && !directory and raise Errno::ENOTDIR, path.to_s
90
+ path.children
93
91
  end
94
92
  end
95
93
  end
@@ -3,16 +3,15 @@
3
3
  module Listen
4
4
  module Event
5
5
  class Config
6
- attr_reader :listener
7
- attr_reader :event_queue
8
- attr_reader :min_delay_between_events
6
+ attr_reader :listener, :event_queue, :min_delay_between_events
9
7
 
10
8
  def initialize(
11
9
  listener,
12
10
  event_queue,
13
11
  queue_optimizer,
14
12
  wait_for_delay,
15
- &block)
13
+ &block
14
+ )
16
15
 
17
16
  @listener = listener
18
17
  @event_queue = event_queue
@@ -26,7 +25,7 @@ module Listen
26
25
  end
27
26
 
28
27
  def call(*args)
29
- @block.call(*args) if @block
28
+ @block&.call(*args)
30
29
  end
31
30
 
32
31
  def timestamp
@@ -51,10 +51,10 @@ module Listen
51
51
  end
52
52
 
53
53
  def _check_stopped
54
- return unless @listener.stopped?
55
-
56
- _flush_wakeup_reasons
57
- raise Stopped
54
+ if @listener.stopped?
55
+ _flush_wakeup_reasons
56
+ raise Stopped
57
+ end
58
58
  end
59
59
 
60
60
  def _sleep(seconds)
@@ -70,22 +70,22 @@ module Listen
70
70
  end
71
71
 
72
72
  def _remember_time_of_first_unprocessed_event
73
- @first_unprocessed_event_time ||= _timestamp
73
+ @_remember_time_of_first_unprocessed_event ||= _timestamp
74
74
  end
75
75
 
76
76
  def _reset_no_unprocessed_events
77
- @first_unprocessed_event_time = nil
77
+ @_remember_time_of_first_unprocessed_event = nil
78
78
  end
79
79
 
80
80
  def _deadline
81
- @first_unprocessed_event_time + @latency
81
+ @_remember_time_of_first_unprocessed_event + @latency
82
82
  end
83
83
 
84
84
  # blocks until event is popped
85
85
  # returns the event or `nil` when the event_queue is closed
86
86
  def _wait_until_events
87
87
  config.event_queue.pop.tap do |_event|
88
- @first_unprocessed_event_time ||= _timestamp
88
+ @_remember_time_of_first_unprocessed_event ||= _timestamp
89
89
  end
90
90
  end
91
91
 
@@ -30,21 +30,21 @@ module Listen
30
30
  fail "Invalid change: #{change.inspect}" unless change.is_a?(Symbol)
31
31
  fail "Invalid path: #{path.inspect}" unless path.is_a?(String)
32
32
 
33
- dir = _safe_relative_from_cwd(dir)
34
- event_queue.public_send(:<<, [type, change, dir, path, options])
33
+ dir = if @config.relative?
34
+ _safe_relative_from_cwd(dir)
35
+ else
36
+ dir
37
+ end
38
+ @event_queue << [type, change, dir, path, options]
35
39
  end
36
40
 
37
- delegate empty?: :event_queue
38
- delegate pop: :event_queue
39
- delegate close: :event_queue
41
+ delegate empty?: :@event_queue
42
+ delegate pop: :@event_queue
43
+ delegate close: :@event_queue
40
44
 
41
45
  private
42
46
 
43
- attr_reader :event_queue
44
- attr_reader :config
45
-
46
47
  def _safe_relative_from_cwd(dir)
47
- return dir unless config.relative?
48
48
  dir.relative_path_from(Pathname.pwd)
49
49
  rescue ArgumentError
50
50
  dir
@@ -4,6 +4,9 @@ require 'digest/md5'
4
4
 
5
5
  module Listen
6
6
  class File
7
+ # rubocop:disable Metrics/MethodLength
8
+ # rubocop:disable Metrics/CyclomaticComplexity
9
+ # rubocop:disable Metrics/PerceivedComplexity
7
10
  def self.change(record, rel_path)
8
11
  path = Pathname.new(record.root) + rel_path
9
12
  lstat = path.lstat
@@ -74,6 +77,9 @@ module Listen
74
77
  Listen.logger.debug "lstat failed for: #{rel_path} (#{$ERROR_INFO})"
75
78
  raise
76
79
  end
80
+ # rubocop:enable Metrics/MethodLength
81
+ # rubocop:enable Metrics/CyclomaticComplexity
82
+ # rubocop:enable Metrics/PerceivedComplexity
77
83
 
78
84
  def self.inaccurate_mac_time?(stat)
79
85
  # 'mac' means Modified/Accessed/Created
@@ -113,8 +113,8 @@ module Listen
113
113
  @name = name
114
114
  @block = block
115
115
  @transitions = if transitions
116
- Array(transitions).map(&:to_sym)
117
- end
116
+ Array(transitions).map(&:to_sym)
117
+ end
118
118
  end
119
119
 
120
120
  def call(obj)
@@ -33,6 +33,7 @@ module Listen
33
33
  # @yieldparam [Array<String>] added the list of added files
34
34
  # @yieldparam [Array<String>] removed the list of removed files
35
35
  #
36
+ # rubocop:disable Metrics/MethodLength
36
37
  def initialize(*dirs, &block)
37
38
  options = dirs.last.is_a?(Hash) ? dirs.pop : {}
38
39
 
@@ -60,6 +61,7 @@ module Listen
60
61
 
61
62
  initialize_fsm
62
63
  end
64
+ # rubocop:enable Metrics/MethodLength
63
65
 
64
66
  start_state :initializing
65
67
 
@@ -25,9 +25,7 @@ module Listen
25
25
  @relative
26
26
  end
27
27
 
28
- attr_reader :min_delay_between_events
29
-
30
- attr_reader :silencer_rules
28
+ attr_reader :min_delay_between_events, :silencer_rules
31
29
 
32
30
  def adapter_instance_options(klass)
33
31
  valid_keys = klass.const_get('DEFAULTS').keys
@@ -35,7 +33,7 @@ module Listen
35
33
  end
36
34
 
37
35
  def adapter_select_options
38
- valid_keys = %w(force_polling polling_fallback_message).map(&:to_sym)
36
+ valid_keys = %w[force_polling polling_fallback_message].map(&:to_sym)
39
37
  Hash[@options.select { |key, _| valid_keys.include?(key) }]
40
38
  end
41
39
  end
@@ -16,18 +16,19 @@ module Listen
16
16
  private
17
17
 
18
18
  def default_logger
19
- level = case ENV['LISTEN_GEM_DEBUGGING'].to_s
20
- when /debug|2/i
21
- ::Logger::DEBUG
22
- when /info|true|yes|1/i
23
- ::Logger::INFO
24
- when /warn/i
25
- ::Logger::WARN
26
- when /fatal/i
27
- ::Logger::FATAL
28
- else
29
- ::Logger::ERROR
30
- end
19
+ level =
20
+ case ENV['LISTEN_GEM_DEBUGGING'].to_s
21
+ when /debug|2/i
22
+ ::Logger::DEBUG
23
+ when /info|true|yes|1/i
24
+ ::Logger::INFO
25
+ when /warn/i
26
+ ::Logger::WARN
27
+ when /fatal/i
28
+ ::Logger::FATAL
29
+ else
30
+ ::Logger::ERROR
31
+ end
31
32
 
32
33
  ::Logger.new(STDERR, level: level)
33
34
  end
@@ -5,21 +5,22 @@ module Listen
5
5
  def initialize(opts, defaults)
6
6
  @options = {}
7
7
  given_options = opts.dup
8
- defaults.keys.each do |key|
8
+ defaults.each_key do |key|
9
9
  @options[key] = given_options.delete(key) || defaults[key]
10
10
  end
11
11
 
12
- return if given_options.empty?
12
+ given_options.empty? or raise ArgumentError, "Unknown options: #{given_options.inspect}"
13
+ end
13
14
 
14
- msg = "Unknown options: #{given_options.inspect}"
15
- Listen.logger.warn msg
16
- fail msg
15
+ # rubocop:disable Lint/MissingSuper
16
+ def respond_to_missing?(name, *_)
17
+ @options.has_key?(name)
17
18
  end
18
19
 
19
20
  def method_missing(name, *_)
20
- return @options[name] if @options.key?(name)
21
- msg = "Bad option: #{name.inspect} (valid:#{@options.keys.inspect})"
22
- fail NameError, msg
21
+ respond_to_missing?(name) or raise NameError, "Bad option: #{name.inspect} (valid:#{@options.keys.inspect})"
22
+ @options[name]
23
23
  end
24
+ # rubocop:enable Lint/MissingSuper
24
25
  end
25
26
  end
@@ -62,7 +62,7 @@ module Listen
62
62
  actions << :added if actions.delete(:moved_to)
63
63
  actions << :removed if actions.delete(:moved_from)
64
64
 
65
- modified = actions.detect { |x| x == :modified }
65
+ modified = actions.find { |x| x == :modified }
66
66
  _calculate_add_remove_difference(actions, path, modified)
67
67
  end
68
68
 
@@ -91,10 +91,8 @@ module Listen
91
91
  def _reinterpret_related_changes(cookies)
92
92
  table = { moved_to: :added, moved_from: :removed }
93
93
  cookies.flat_map do |_, changes|
94
- data = _detect_possible_editor_save(changes)
95
- if data
96
- to_dir, to_file = data
97
- [[:modified, to_dir, to_file]]
94
+ if (editor_modified = editor_modified?(changes))
95
+ [[:modified, *editor_modified]]
98
96
  else
99
97
  not_silenced = changes.reject do |type, _, _, path, _|
100
98
  config.silenced?(Pathname(path), type)
@@ -106,7 +104,7 @@ module Listen
106
104
  end
107
105
  end
108
106
 
109
- def _detect_possible_editor_save(changes)
107
+ def editor_modified?(changes)
110
108
  return unless changes.size == 2
111
109
 
112
110
  from_type = from = nil
@@ -118,17 +116,14 @@ module Listen
118
116
  from_type, _from_change, _, from, = data
119
117
  when :moved_to
120
118
  to_type, _to_change, to_dir, to, = data
121
- else
122
- return nil
123
119
  end
124
120
  end
125
121
 
126
- return unless from && to
127
-
128
122
  # Expect an ignored moved_from and non-ignored moved_to
129
123
  # to qualify as an "editor modify"
130
- return unless config.silenced?(Pathname(from), from_type)
131
- config.silenced?(Pathname(to), to_type) ? nil : [to_dir, to]
124
+ if from && to && config.silenced?(Pathname(from), from_type) && !config.silenced?(Pathname(to), to_type)
125
+ [to_dir, to]
126
+ end
132
127
  end
133
128
  end
134
129
  end
@@ -10,14 +10,16 @@ module Listen
10
10
  # TODO: deprecate
11
11
 
12
12
  attr_reader :root
13
+
13
14
  def initialize(directory)
14
15
  @tree = _auto_hash
15
16
  @root = directory.to_s
16
17
  end
17
18
 
18
19
  def add_dir(rel_path)
19
- return if [nil, '', '.'].include? rel_path
20
- @tree[rel_path] ||= {}
20
+ if ![nil, '', '.'].include?(rel_path)
21
+ @tree[rel_path] ||= {}
22
+ end
21
23
  end
22
24
 
23
25
  def update_file(rel_path, data)
@@ -33,33 +35,30 @@ module Listen
33
35
  def file_data(rel_path)
34
36
  dirname, basename = Pathname(rel_path).split.map(&:to_s)
35
37
  if [nil, '', '.'].include? dirname
36
- tree[basename] ||= {}
37
- tree[basename].dup
38
+ @tree[basename] ||= {}
39
+ @tree[basename].dup
38
40
  else
39
- tree[dirname] ||= {}
40
- tree[dirname][basename] ||= {}
41
- tree[dirname][basename].dup
41
+ @tree[dirname] ||= {}
42
+ @tree[dirname][basename] ||= {}
43
+ @tree[dirname][basename].dup
42
44
  end
43
45
  end
44
46
 
45
47
  def dir_entries(rel_path)
46
- subtree =
47
- if [nil, '', '.'].include? rel_path.to_s
48
- tree
49
- else
50
- _sub_tree(rel_path)
51
- end
48
+ subtree = if [nil, '', '.'].include? rel_path.to_s
49
+ @tree
50
+ else
51
+ _sub_tree(rel_path)
52
+ end
52
53
 
53
- result = {}
54
- subtree.each do |key, values|
54
+ subtree.transform_values do |values|
55
55
  # only get data for file entries
56
- result[key] = values.key?(:mtime) ? values : {}
56
+ values.key?(:mtime) ? values : {}
57
57
  end
58
- result
59
58
  end
60
59
 
61
60
  def _sub_tree(rel_path)
62
- tree.each_with_object({}) do |(path, meta), result|
61
+ @tree.each_with_object({}) do |(path, meta), result|
63
62
  next unless path.start_with?(rel_path)
64
63
 
65
64
  if path == rel_path
@@ -85,29 +84,27 @@ module Listen
85
84
  private
86
85
 
87
86
  def _auto_hash
88
- Hash.new { |h, k| h[k] = Hash.new }
87
+ Hash.new { |h, k| h[k] = {} }
89
88
  end
90
89
 
91
- attr_reader :tree
92
-
93
90
  def _fast_update_file(dirname, basename, data)
94
- if [nil, '', '.'].include? dirname
95
- tree[basename] = (tree[basename] || {}).merge(data)
91
+ if [nil, '', '.'].include?(dirname)
92
+ @tree[basename] = (@tree[basename] || {}).merge(data)
96
93
  else
97
- tree[dirname] ||= {}
98
- tree[dirname][basename] = (tree[dirname][basename] || {}).merge(data)
94
+ @tree[dirname] ||= {}
95
+ @tree[dirname][basename] = (@tree[dirname][basename] || {}).merge(data)
99
96
  end
100
97
  end
101
98
 
102
99
  def _fast_unset_path(dirname, basename)
103
100
  # this may need to be reworked to properly remove
104
101
  # entries from a tree, without adding non-existing dirs to the record
105
- if [nil, '', '.'].include? dirname
106
- return unless tree.key?(basename)
107
- tree.delete(basename)
108
- else
109
- return unless tree.key?(dirname)
110
- tree[dirname].delete(basename)
102
+ if [nil, '', '.'].include?(dirname)
103
+ if @tree.key?(basename)
104
+ @tree.delete(basename)
105
+ end
106
+ elsif @tree.key?(dirname)
107
+ @tree[dirname].delete(basename)
111
108
  end
112
109
  end
113
110
 
@@ -17,7 +17,7 @@ module Listen
17
17
 
18
18
  def children
19
19
  child_relative = _join
20
- (_entries(sys_path) - %w(. ..)).map do |name|
20
+ (_entries(sys_path) - %w[. ..]).map do |name|
21
21
  Entry.new(@root, child_relative, name)
22
22
  end
23
23
  end
@@ -6,9 +6,9 @@ module Listen
6
6
  # @private api
7
7
  class Record
8
8
  class SymlinkDetector
9
- WIKI = 'https://github.com/guard/listen/wiki/Duplicate-directory-errors'.freeze
9
+ WIKI = 'https://github.com/guard/listen/wiki/Duplicate-directory-errors'
10
10
 
11
- SYMLINK_LOOP_ERROR = <<-EOS.freeze
11
+ SYMLINK_LOOP_ERROR = <<-EOS
12
12
  ** ERROR: directory is already being watched! **
13
13
 
14
14
  Directory: %s
@@ -33,8 +33,8 @@ module Listen
33
33
  private
34
34
 
35
35
  def _fail(symlinked, real_path)
36
- STDERR.puts format(SYMLINK_LOOP_ERROR, symlinked, real_path)
37
- fail Error, 'Failed due to looped symlinks'
36
+ warn(format(SYMLINK_LOOP_ERROR, symlinked, real_path))
37
+ raise Error, 'Failed due to looped symlinks'
38
38
  end
39
39
  end
40
40
  end
@@ -14,7 +14,7 @@ module Listen
14
14
  | log
15
15
  | tmp
16
16
  |vendor/ruby
17
- )(/|$)}x
17
+ )(/|$)}x.freeze
18
18
 
19
19
  # The default list of files that get ignored.
20
20
  DEFAULT_IGNORED_EXTENSIONS = %r{(?:
@@ -55,7 +55,7 @@ module Listen
55
55
  | \.DS_Store
56
56
  | \.tmp
57
57
  | ~
58
- )$}x
58
+ )$}x.freeze
59
59
 
60
60
  attr_accessor :only_patterns, :ignore_patterns
61
61
 
@@ -75,16 +75,18 @@ module Listen
75
75
  def silenced?(relative_path, type)
76
76
  path = relative_path.to_s
77
77
 
78
- if only_patterns && type == :file
79
- return true unless only_patterns.any? { |pattern| path =~ pattern }
80
- end
81
-
82
- ignore_patterns.any? { |pattern| path =~ pattern }
78
+ _ignore?(path) || (only_patterns && type == :file && !_only?(path))
83
79
  end
84
80
 
85
81
  private
86
82
 
87
- attr_reader :options
83
+ def _ignore?(path)
84
+ ignore_patterns.any? { |pattern| path =~ pattern }
85
+ end
86
+
87
+ def _only?(path)
88
+ only_patterns.any? { |pattern| path =~ pattern }
89
+ end
88
90
 
89
91
  def _init_ignores(ignores, overrides)
90
92
  patterns = []
@@ -9,6 +9,7 @@ module Listen
9
9
  class << self
10
10
  # Creates a new thread with the given name.
11
11
  # Any exceptions raised by the thread will be logged with the thread name and complete backtrace.
12
+ # rubocop:disable Style/MultilineBlockChain
12
13
  def new(name, &block)
13
14
  thread_name = "listen-#{name}"
14
15
  caller_stack = caller
@@ -19,31 +20,32 @@ module Listen
19
20
  thread.name = thread_name
20
21
  end
21
22
  end
23
+ # rubocop:enable Style/MultilineBlockChain
22
24
 
23
25
  def rescue_and_log(method_name, *args, caller_stack: nil)
24
26
  yield(*args)
25
- rescue Exception => ex
26
- _log_exception(ex, method_name, caller_stack: caller_stack)
27
+ rescue Exception => exception # rubocop:disable Lint/RescueException
28
+ _log_exception(exception, method_name, caller_stack: caller_stack)
27
29
  end
28
30
 
29
31
  private
30
32
 
31
- def _log_exception(ex, thread_name, caller_stack: nil)
33
+ def _log_exception(exception, thread_name, caller_stack: nil)
32
34
  complete_backtrace = if caller_stack
33
- [*ex.backtrace, "--- Thread.new ---", *caller_stack]
34
- else
35
- ex.backtrace
36
- end
37
- message = "Exception rescued in #{thread_name}:\n#{_exception_with_causes(ex)}\n#{complete_backtrace * "\n"}"
35
+ [*exception.backtrace, "--- Thread.new ---", *caller_stack]
36
+ else
37
+ exception.backtrace
38
+ end
39
+ message = "Exception rescued in #{thread_name}:\n#{_exception_with_causes(exception)}\n#{complete_backtrace * "\n"}"
38
40
  Listen.logger.error(message)
39
41
  end
40
42
 
41
- def _exception_with_causes(ex)
42
- result = +"#{ex.class}: #{ex}"
43
- if ex.cause
43
+ def _exception_with_causes(exception)
44
+ result = +"#{exception.class}: #{exception}"
45
+ if exception.cause
44
46
  result << "\n"
45
47
  result << "--- Caused by: ---\n"
46
- result << _exception_with_causes(ex.cause)
48
+ result << _exception_with_causes(exception.cause)
47
49
  end
48
50
  result
49
51
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Listen
4
- VERSION = '3.3.1'
4
+ VERSION = '3.3.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: listen
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.1
4
+ version: 3.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thibaud Guillaume-Gentil
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-13 00:00:00.000000000 Z
11
+ date: 2020-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rb-fsevent
@@ -51,7 +51,7 @@ dependencies:
51
51
  - !ruby/object:Gem::Version
52
52
  version: 0.9.10
53
53
  description: The Listen gem listens to file modifications and notifies you about the
54
- changegem. Works everywhere!
54
+ changes. Works everywhere!
55
55
  email: thibaud@thibaud.gg
56
56
  executables:
57
57
  - listen