listen 3.1.5 → 3.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/listen/record.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'thread'
2
4
  require 'listen/record/entry'
3
5
  require 'listen/record/symlink_detector'
@@ -8,14 +10,17 @@ module Listen
8
10
  # TODO: deprecate
9
11
 
10
12
  attr_reader :root
11
- def initialize(directory)
12
- @tree = _auto_hash
13
+
14
+ def initialize(directory, silencer)
15
+ reset_tree
13
16
  @root = directory.to_s
17
+ @silencer = silencer
14
18
  end
15
19
 
16
20
  def add_dir(rel_path)
17
- return if [nil, '', '.'].include? rel_path
18
- @tree[rel_path] ||= {}
21
+ if !empty_dirname?(rel_path.to_s)
22
+ @tree[rel_path.to_s]
23
+ end
19
24
  end
20
25
 
21
26
  def update_file(rel_path, data)
@@ -30,35 +35,32 @@ module Listen
30
35
 
31
36
  def file_data(rel_path)
32
37
  dirname, basename = Pathname(rel_path).split.map(&:to_s)
33
- if [nil, '', '.'].include? dirname
34
- tree[basename] ||= {}
35
- tree[basename].dup
38
+ if empty_dirname?(dirname)
39
+ @tree[basename].dup
36
40
  else
37
- tree[dirname] ||= {}
38
- tree[dirname][basename] ||= {}
39
- tree[dirname][basename].dup
41
+ @tree[dirname][basename] ||= {}
42
+ @tree[dirname][basename].dup
40
43
  end
41
44
  end
42
45
 
43
46
  def dir_entries(rel_path)
44
- subtree =
45
- if [nil, '', '.'].include? rel_path.to_s
46
- tree
47
- else
48
- tree[rel_path.to_s] ||= _auto_hash
49
- tree[rel_path.to_s]
50
- end
47
+ rel_path_s = rel_path.to_s
48
+ subtree = if empty_dirname?(rel_path_s)
49
+ @tree
50
+ else
51
+ @tree[rel_path_s]
52
+ end
51
53
 
52
- result = {}
53
- subtree.each do |key, values|
54
- # only get data for file entries
55
- result[key] = values.key?(:mtime) ? values : {}
54
+ subtree.each_with_object({}) do |(key, values), result|
55
+ # only return data for file entries inside the dir (which will each be sub-hashes)
56
+ if values.is_a?(Hash)
57
+ result[key] = values.has_key?(:mtime) ? values : {}
58
+ end
56
59
  end
57
- result
58
60
  end
59
61
 
60
62
  def build
61
- @tree = _auto_hash
63
+ reset_tree
62
64
  # TODO: test with a file name given
63
65
  # TODO: test other permissions
64
66
  # TODO: test with mixed encoding
@@ -70,35 +72,38 @@ module Listen
70
72
 
71
73
  private
72
74
 
73
- def _auto_hash
74
- Hash.new { |h, k| h[k] = Hash.new }
75
+ def empty_dirname?(dirname)
76
+ dirname == '.' || dirname == ''
75
77
  end
76
78
 
77
- attr_reader :tree
79
+ def reset_tree
80
+ @tree = Hash.new { |h, k| h[k] = {} }
81
+ end
78
82
 
79
83
  def _fast_update_file(dirname, basename, data)
80
- if [nil, '', '.'].include? dirname
81
- tree[basename] = (tree[basename] || {}).merge(data)
84
+ if empty_dirname?(dirname.to_s)
85
+ @tree[basename] = @tree[basename].merge(data)
82
86
  else
83
- tree[dirname] ||= {}
84
- tree[dirname][basename] = (tree[dirname][basename] || {}).merge(data)
87
+ @tree[dirname][basename] = (@tree[dirname][basename] || {}).merge(data)
85
88
  end
86
89
  end
87
90
 
88
91
  def _fast_unset_path(dirname, basename)
89
92
  # this may need to be reworked to properly remove
90
93
  # entries from a tree, without adding non-existing dirs to the record
91
- if [nil, '', '.'].include? dirname
92
- return unless tree.key?(basename)
93
- tree.delete(basename)
94
- else
95
- return unless tree.key?(dirname)
96
- tree[dirname].delete(basename)
94
+ if empty_dirname?(dirname.to_s)
95
+ if @tree.key?(basename)
96
+ @tree.delete(basename)
97
+ end
98
+ elsif @tree.key?(dirname)
99
+ @tree[dirname].delete(basename)
97
100
  end
98
101
  end
99
102
 
100
103
  def _fast_build_dir(remaining, symlink_detector)
101
104
  entry = remaining.pop
105
+ return if @silencer.silenced?(entry.record_dir_key, :dir)
106
+
102
107
  children = entry.children # NOTE: children() implicitly tests if dir
103
108
  symlink_detector.verify_unwatched!(entry)
104
109
  children.each { |child| remaining << child }
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Listen
2
4
  class Silencer
3
5
  class Controller
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Listen
2
4
  class Silencer
3
5
  # The default list of directories that get ignored.
4
- DEFAULT_IGNORED_DIRECTORIES = %r{^(?:
5
- \.git
6
+ DEFAULT_IGNORED_FILES = %r{\A(?:
7
+ \.git
6
8
  | \.svn
7
9
  | \.hg
8
10
  | \.rbx
@@ -11,8 +13,12 @@ module Listen
11
13
  | vendor/bundle
12
14
  | log
13
15
  | tmp
14
- |vendor/ruby
15
- )(/|$)}x
16
+ | vendor/ruby
17
+
18
+ # emacs temp files
19
+ | \#.+\#
20
+ | \.\#.+
21
+ )(/|\z)}x.freeze
16
22
 
17
23
  # The default list of files that get ignored.
18
24
  DEFAULT_IGNORED_EXTENSIONS = %r{(?:
@@ -33,7 +39,7 @@ module Listen
33
39
  | ^4913
34
40
 
35
41
  # Sed temporary files - but without actual words, like 'sedatives'
36
- | (?:^
42
+ | (?:\A
37
43
  sed
38
44
 
39
45
  (?:
@@ -46,45 +52,48 @@ module Listen
46
52
  )
47
53
  )
48
54
 
55
+ # Mutagen sync temporary files
56
+ | \.mutagen-temporary.*
57
+
49
58
  # other files
50
59
  | \.DS_Store
51
60
  | \.tmp
52
61
  | ~
53
- )$}x
62
+ )\z}x.freeze
54
63
 
64
+ # TODO: deprecate these mutators; use attr_reader instead
55
65
  attr_accessor :only_patterns, :ignore_patterns
56
66
 
57
- def initialize
58
- configure({})
67
+ def initialize(**options)
68
+ configure(options)
59
69
  end
60
70
 
71
+ # TODO: deprecate this mutator
61
72
  def configure(options)
62
73
  @only_patterns = options[:only] ? Array(options[:only]) : nil
63
74
  @ignore_patterns = _init_ignores(options[:ignore], options[:ignore!])
64
75
  end
65
76
 
66
- # Note: relative_path is temporarily expected to be a relative Pathname to
67
- # make refactoring easier (ideally, it would take a string)
68
-
69
- # TODO: switch type and path places - and verify
70
77
  def silenced?(relative_path, type)
71
- path = relative_path.to_s
72
-
73
- if only_patterns && type == :file
74
- return true unless only_patterns.any? { |pattern| path =~ pattern }
75
- end
78
+ path = relative_path.to_s # in case it is a Pathname
76
79
 
77
- ignore_patterns.any? { |pattern| path =~ pattern }
80
+ _ignore?(path) || (only_patterns && type == :file && !_only?(path))
78
81
  end
79
82
 
80
83
  private
81
84
 
82
- attr_reader :options
85
+ def _ignore?(path)
86
+ ignore_patterns.any? { |pattern| path =~ pattern }
87
+ end
88
+
89
+ def _only?(path)
90
+ only_patterns.any? { |pattern| path =~ pattern }
91
+ end
83
92
 
84
93
  def _init_ignores(ignores, overrides)
85
94
  patterns = []
86
95
  unless overrides
87
- patterns << DEFAULT_IGNORED_DIRECTORIES
96
+ patterns << DEFAULT_IGNORED_FILES
88
97
  patterns << DEFAULT_IGNORED_EXTENSIONS
89
98
  end
90
99
 
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thread'
4
+
5
+ require_relative 'logger'
6
+
7
+ module Listen
8
+ module Thread
9
+ class << self
10
+ # Creates a new thread with the given name.
11
+ # Any exceptions raised by the thread will be logged with the thread name and complete backtrace.
12
+ # rubocop:disable Style/MultilineBlockChain
13
+ def new(name, &block)
14
+ thread_name = "listen-#{name}"
15
+ caller_stack = caller
16
+
17
+ ::Thread.new do
18
+ rescue_and_log(thread_name, caller_stack: caller_stack, &block)
19
+ end.tap do |thread|
20
+ thread.name = thread_name
21
+ end
22
+ end
23
+ # rubocop:enable Style/MultilineBlockChain
24
+
25
+ def rescue_and_log(method_name, *args, caller_stack: nil)
26
+ yield(*args)
27
+ rescue => exception
28
+ _log_exception(exception, method_name, caller_stack: caller_stack)
29
+ end
30
+
31
+ private
32
+
33
+ def _log_exception(exception, thread_name, caller_stack: nil)
34
+ complete_backtrace = if caller_stack
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"}"
40
+ Listen.logger.error(message)
41
+ end
42
+
43
+ def _exception_with_causes(exception)
44
+ result = +"#{exception.class}: #{exception}"
45
+ if exception.cause
46
+ result << "\n"
47
+ result << "--- Caused by: ---\n"
48
+ result << _exception_with_causes(exception.cause)
49
+ end
50
+ result
51
+ end
52
+ end
53
+ end
54
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Listen
2
- VERSION = '3.1.5'.freeze
4
+ VERSION = '3.8.0'
3
5
  end
data/lib/listen.rb CHANGED
@@ -1,28 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'logger'
4
+ require 'weakref'
2
5
  require 'listen/logger'
3
6
  require 'listen/listener'
4
7
 
5
- require 'listen/internals/thread_pool'
6
-
7
- # Show warnings about vulnerabilities, bugs and outdated Rubies, since previous
8
- # versions aren't tested or officially supported.
9
- require 'ruby_dep/warning'
10
- RubyDep::Warning.new.show_warnings
11
-
12
- # Always set up logging by default first time file is required
13
- #
14
- # NOTE: If you need to clear the logger completely, do so *after*
15
- # requiring this file. If you need to set a custom logger,
16
- # require the listen/logger file and set the logger before requiring
17
- # this file.
18
- Listen.setup_default_logger_if_unset
19
-
20
8
  # Won't print anything by default because of level - unless you've set
21
9
  # LISTEN_GEM_DEBUGGING or provided your own logger with a high enough level
22
- Listen::Logger.info "Listen loglevel set to: #{Listen.logger.level}"
23
- Listen::Logger.info "Listen version: #{Listen::VERSION}"
10
+ Listen.logger.info "Listen loglevel set to: #{Listen.logger.level}"
11
+ Listen.logger.info "Listen version: #{Listen::VERSION}"
24
12
 
25
13
  module Listen
14
+ @listeners = Queue.new
15
+
26
16
  class << self
27
17
  # Listens to file system modifications on a either single directory or
28
18
  # multiple directories.
@@ -37,21 +27,21 @@ module Listen
37
27
  # @return [Listen::Listener] the listener
38
28
  #
39
29
  def to(*args, &block)
40
- @listeners ||= []
41
30
  Listener.new(*args, &block).tap do |listener|
42
- @listeners << listener
31
+ @listeners.enq(WeakRef.new(listener))
43
32
  end
44
33
  end
45
34
 
46
35
  # This is used by the `listen` binary to handle Ctrl-C
47
36
  #
48
37
  def stop
49
- Internals::ThreadPool.stop
50
- @listeners ||= []
51
-
52
- # TODO: should use a mutex for this
53
- @listeners.each(&:stop)
54
- @listeners = nil
38
+ while (listener = @listeners.deq(true))
39
+ begin
40
+ listener.stop
41
+ rescue WeakRef::RefError
42
+ end
43
+ end
44
+ rescue ThreadError
55
45
  end
56
46
  end
57
47
  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.1.5
4
+ version: 3.8.0
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: 2016-05-18 00:00:00.000000000 Z
11
+ date: 2023-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rb-fsevent
@@ -16,20 +16,20 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.9'
19
+ version: '0.10'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: 0.9.4
22
+ version: 0.10.3
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: '0.9'
29
+ version: '0.10'
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: 0.9.4
32
+ version: 0.10.3
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: rb-inotify
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -39,7 +39,7 @@ dependencies:
39
39
  version: '0.9'
40
40
  - - ">="
41
41
  - !ruby/object:Gem::Version
42
- version: 0.9.7
42
+ version: 0.9.10
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
@@ -49,35 +49,7 @@ dependencies:
49
49
  version: '0.9'
50
50
  - - ">="
51
51
  - !ruby/object:Gem::Version
52
- version: 0.9.7
53
- - !ruby/object:Gem::Dependency
54
- name: ruby_dep
55
- requirement: !ruby/object:Gem::Requirement
56
- requirements:
57
- - - "~>"
58
- - !ruby/object:Gem::Version
59
- version: '1.2'
60
- type: :runtime
61
- prerelease: false
62
- version_requirements: !ruby/object:Gem::Requirement
63
- requirements:
64
- - - "~>"
65
- - !ruby/object:Gem::Version
66
- version: '1.2'
67
- - !ruby/object:Gem::Dependency
68
- name: bundler
69
- requirement: !ruby/object:Gem::Requirement
70
- requirements:
71
- - - "~>"
72
- - !ruby/object:Gem::Version
73
- version: '1.12'
74
- type: :development
75
- prerelease: false
76
- version_requirements: !ruby/object:Gem::Requirement
77
- requirements:
78
- - - "~>"
79
- - !ruby/object:Gem::Version
80
- version: '1.12'
52
+ version: 0.9.10
81
53
  description: The Listen gem listens to file modifications and notifies you about the
82
54
  changes. Works everywhere!
83
55
  email: thibaud@thibaud.gg
@@ -104,16 +76,17 @@ files:
104
76
  - lib/listen/change.rb
105
77
  - lib/listen/cli.rb
106
78
  - lib/listen/directory.rb
79
+ - lib/listen/error.rb
107
80
  - lib/listen/event/config.rb
108
81
  - lib/listen/event/loop.rb
109
82
  - lib/listen/event/processor.rb
110
83
  - lib/listen/event/queue.rb
111
84
  - lib/listen/file.rb
112
85
  - lib/listen/fsm.rb
113
- - lib/listen/internals/thread_pool.rb
114
86
  - lib/listen/listener.rb
115
87
  - lib/listen/listener/config.rb
116
88
  - lib/listen/logger.rb
89
+ - lib/listen/monotonic_time.rb
117
90
  - lib/listen/options.rb
118
91
  - lib/listen/queue_optimizer.rb
119
92
  - lib/listen/record.rb
@@ -121,33 +94,35 @@ files:
121
94
  - lib/listen/record/symlink_detector.rb
122
95
  - lib/listen/silencer.rb
123
96
  - lib/listen/silencer/controller.rb
97
+ - lib/listen/thread.rb
124
98
  - lib/listen/version.rb
125
99
  homepage: https://github.com/guard/listen
126
100
  licenses:
127
101
  - MIT
128
- metadata: {}
102
+ metadata:
103
+ allowed_push_host: https://rubygems.org
104
+ bug_tracker_uri: https://github.com/guard/listen/issues
105
+ changelog_uri: https://github.com/guard/listen/releases
106
+ documentation_uri: https://www.rubydoc.info/gems/listen/3.8.0
107
+ homepage_uri: https://github.com/guard/listen
108
+ source_code_uri: https://github.com/guard/listen/tree/v3.8.0
129
109
  post_install_message:
130
110
  rdoc_options: []
131
111
  require_paths:
132
112
  - lib
133
113
  required_ruby_version: !ruby/object:Gem::Requirement
134
114
  requirements:
135
- - - "~>"
136
- - !ruby/object:Gem::Version
137
- version: '2.2'
138
115
  - - ">="
139
116
  - !ruby/object:Gem::Version
140
- version: 2.2.3
117
+ version: 2.4.0
141
118
  required_rubygems_version: !ruby/object:Gem::Requirement
142
119
  requirements:
143
120
  - - ">="
144
121
  - !ruby/object:Gem::Version
145
122
  version: '0'
146
123
  requirements: []
147
- rubyforge_project:
148
- rubygems_version: 2.5.1
124
+ rubygems_version: 3.0.1
149
125
  signing_key:
150
126
  specification_version: 4
151
127
  summary: Listen to file modifications
152
128
  test_files: []
153
- has_rdoc:
@@ -1,29 +0,0 @@
1
- module Listen
2
- # @private api
3
- module Internals
4
- module ThreadPool
5
- def self.add(&block)
6
- Thread.new { block.call }.tap do |th|
7
- (@threads ||= Queue.new) << th
8
- end
9
- end
10
-
11
- def self.stop
12
- return unless @threads ||= nil
13
- return if @threads.empty? # return to avoid using possibly stubbed Queue
14
-
15
- killed = Queue.new
16
- # You can't kill a read on a descriptor in JRuby, so let's just
17
- # ignore running threads (listen rb-inotify waiting for disk activity
18
- # before closing) pray threads die faster than they are created...
19
- limit = RUBY_ENGINE == 'jruby' ? [1] : []
20
-
21
- killed << @threads.pop.kill until @threads.empty?
22
- until killed.empty?
23
- th = killed.pop
24
- th.join(*limit) unless th[:listen_blocking_read_thread]
25
- end
26
- end
27
- end
28
- end
29
- end