diff --git a/bin/listen b/bin/listen index 3ba42113..59ade96d 100755 --- a/bin/listen +++ b/bin/listen @@ -4,10 +4,8 @@ require 'listen' require 'listen/cli' -unless defined?(JRUBY_VERSION) - if Signal.list.keys.include?('INT') - Signal.trap('INT') { Thread.new { Listen.stop } } - end +if !defined?(JRUBY_VERSION) && Signal.list.keys.include?('INT') + Signal.trap('INT') { Thread.new { Listen.stop } } end Listen::CLI.start diff --git a/lib/listen/adapter/base.rb b/lib/listen/adapter/base.rb index 84d3b942..fc0226e6 100644 --- a/lib/listen/adapter/base.rb +++ b/lib/listen/adapter/base.rb @@ -8,13 +8,11 @@ module Listen module Adapter class Base - attr_reader :options + attr_reader :options, :config # TODO: only used by tests DEFAULTS = {}.freeze - attr_reader :config - def initialize(config) @started = false @config = config @@ -75,7 +73,7 @@ def start @started = true @run_thread = Listen::Thread.new("run_thread") do - @snapshots.values.each do |snapshot| + @snapshots.each_value do |snapshot| _timed('Record.build()') { snapshot.record.build } end _run diff --git a/lib/listen/adapter/config.rb b/lib/listen/adapter/config.rb index eb0c6099..e33b13ef 100644 --- a/lib/listen/adapter/config.rb +++ b/lib/listen/adapter/config.rb @@ -5,10 +5,7 @@ module Listen module Adapter class Config - attr_reader :directories - attr_reader :silencer - attr_reader :queue - attr_reader :adapter_options + attr_reader :directories, :silencer, :queue, :adapter_options def initialize(directories, queue, silencer, adapter_options) # Default to current directory if no directories are supplied diff --git a/lib/listen/adapter/darwin.rb b/lib/listen/adapter/darwin.rb index d3ec90c3..6106a145 100644 --- a/lib/listen/adapter/darwin.rb +++ b/lib/listen/adapter/darwin.rb @@ -51,7 +51,7 @@ def _run def _process_changes(dirs) dirs.each do |dir| - dir = Pathname.new(dir.sub(%r{\/$}, '')) + dir = Pathname.new(dir.sub(%r{/$}, '')) @callbacks.each do |watched_dir, callback| if watched_dir.eql?(dir) || Listen::Directory.ascendant_of?(watched_dir, dir) diff --git a/lib/listen/adapter/windows.rb b/lib/listen/adapter/windows.rb index 0d8fb366..5fac53d2 100644 --- a/lib/listen/adapter/windows.rb +++ b/lib/listen/adapter/windows.rb @@ -67,10 +67,11 @@ def _process_event(dir, event) _queue_change(:file, dir, rel_path, options) end when :dir - if change.type == :removed + case change.type + when :removed # TODO: check if watched dir? _queue_change(:dir, dir, Pathname(rel_path).dirname.to_s, {}) - elsif change.type == :added + when :added _queue_change(:dir, dir, rel_path, {}) # do nothing - changed directory means either: # - removed subdirs (handled above) diff --git a/lib/listen/cli.rb b/lib/listen/cli.rb index a528a5bd..6fcfbe46 100644 --- a/lib/listen/cli.rb +++ b/lib/listen/cli.rb @@ -35,6 +35,7 @@ def start class Forwarder attr_reader :logger + def initialize(options) @options = options @logger = ::Logger.new(STDOUT, level: ::Logger::INFO) diff --git a/lib/listen/event/config.rb b/lib/listen/event/config.rb index 12e93ece..b4035891 100644 --- a/lib/listen/event/config.rb +++ b/lib/listen/event/config.rb @@ -3,9 +3,7 @@ module Listen module Event class Config - attr_reader :listener - attr_reader :event_queue - attr_reader :min_delay_between_events + attr_reader :listener, :event_queue, :min_delay_between_events def initialize( listener, diff --git a/lib/listen/event/queue.rb b/lib/listen/event/queue.rb index c23c610e..338bc183 100644 --- a/lib/listen/event/queue.rb +++ b/lib/listen/event/queue.rb @@ -40,8 +40,7 @@ def <<(args) private - attr_reader :event_queue - attr_reader :config + attr_reader :event_queue, :config def _safe_relative_from_cwd(dir) return dir unless config.relative? diff --git a/lib/listen/listener/config.rb b/lib/listen/listener/config.rb index 1e1bc9fd..56e4b7c9 100644 --- a/lib/listen/listener/config.rb +++ b/lib/listen/listener/config.rb @@ -25,9 +25,7 @@ def relative? @relative end - attr_reader :min_delay_between_events - - attr_reader :silencer_rules + attr_reader :min_delay_between_events, :silencer_rules def adapter_instance_options(klass) valid_keys = klass.const_get('DEFAULTS').keys diff --git a/lib/listen/options.rb b/lib/listen/options.rb index 70f28bde..765e0460 100644 --- a/lib/listen/options.rb +++ b/lib/listen/options.rb @@ -5,7 +5,7 @@ class Options def initialize(opts, defaults) @options = {} given_options = opts.dup - defaults.keys.each do |key| + defaults.each_key do |key| @options[key] = given_options.delete(key) || defaults[key] end @@ -17,11 +17,11 @@ def initialize(opts, defaults) end def respond_to_missing?(name, *_) - @options.has_key?(name) + super || @options.has_key?(name) end def method_missing(name, *_) - respond_to_missing?(name) or raise NameError, "Bad option: #{name.inspect} (valid:#{@options.keys.inspect})" + super || respond_to_missing?(name) or raise NameError, "Bad option: #{name.inspect} (valid:#{@options.keys.inspect})" @options[name] end end diff --git a/lib/listen/record.rb b/lib/listen/record.rb index 88392980..60dfa0d8 100644 --- a/lib/listen/record.rb +++ b/lib/listen/record.rb @@ -10,6 +10,7 @@ class Record # TODO: deprecate attr_reader :root + def initialize(directory) @tree = _auto_hash @root = directory.to_s @@ -49,9 +50,9 @@ def dir_entries(rel_path) _sub_tree(rel_path) end - subtree.each_with_object({}) do |(key, values), result| + subtree.transform_values do |values| # only get data for file entries - result[key] = values.key?(:mtime) ? values : {} + values.key?(:mtime) ? values : {} end end diff --git a/lib/listen/silencer.rb b/lib/listen/silencer.rb index 450ea614..1f17bbce 100644 --- a/lib/listen/silencer.rb +++ b/lib/listen/silencer.rb @@ -75,16 +75,18 @@ def configure(options) def silenced?(relative_path, type) path = relative_path.to_s - if only_patterns && type == :file - return true unless only_patterns.any? { |pattern| path =~ pattern } - end - - ignore_patterns.any? { |pattern| path =~ pattern } + _ignore?(path) || (only_patterns && type == :file && !_only?(path)) end private - attr_reader :options + def _ignore?(path) + ignore_patterns.any? { |pattern| path =~ pattern } + end + + def _only?(path) + only_patterns.any? { |pattern| path =~ pattern } + end def _init_ignores(ignores, overrides) patterns = [] diff --git a/listen.gemspec b/listen.gemspec index baecf7a3..bb45148b 100644 --- a/listen.gemspec +++ b/listen.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |gem| } gem.files = `git ls-files -z`.split("\x0").select do |f| - %r{^(?:bin|lib)\/} =~ f + %r{^(?:bin|lib)/} =~ f end + %w[CHANGELOG.md CONTRIBUTING.md LICENSE.txt README.md] gem.test_files = [] diff --git a/spec/lib/listen/file_spec.rb b/spec/lib/listen/file_spec.rb index 09a2be43..5dda777a 100644 --- a/spec/lib/listen/file_spec.rb +++ b/spec/lib/listen/file_spec.rb @@ -179,7 +179,7 @@ it { should be :modified } it 'sets path in record with expected data' do - expected = expected_data. merge(md5: md5) + expected = expected_data.merge(md5: md5) expect(record).to receive(:update_file). with('file.rb', expected) subject diff --git a/spec/lib/listen/queue_optimizer_spec.rb b/spec/lib/listen/queue_optimizer_spec.rb index b13d8c25..d74a8a08 100644 --- a/spec/lib/listen/queue_optimizer_spec.rb +++ b/spec/lib/listen/queue_optimizer_spec.rb @@ -75,15 +75,15 @@ context 'with cookie' do context 'when single moved' do - let(:changes) { [[:file, :moved_to, dir, 'foo', cookie: 4321]] } + let(:changes) { [[:file, :moved_to, dir, 'foo', { cookie: 4321 }]] } it { is_expected.to eq(modified: [], added: ['foo'], removed: []) } end context 'when related moved_to' do let(:changes) do [ - [:file, :moved_from, dir, 'foo', cookie: 4321], - [:file, :moved_to, dir, 'bar', cookie: 4321] + [:file, :moved_from, dir, 'foo', { cookie: 4321 }], + [:file, :moved_to, dir, 'bar', { cookie: 4321 }] ] end it { is_expected.to eq(modified: [], added: ['bar'], removed: []) } @@ -93,8 +93,8 @@ context 'when related moved_to with ignored moved_from' do let(:changes) do [ - [:file, :moved_from, dir, 'ignored', cookie: 4321], - [:file, :moved_to, dir, 'foo', cookie: 4321] + [:file, :moved_from, dir, 'ignored', { cookie: 4321 }], + [:file, :moved_to, dir, 'foo', { cookie: 4321 }] ] end it { is_expected.to eq(modified: ['foo'], added: [], removed: []) } diff --git a/spec/lib/listen/silencer_spec.rb b/spec/lib/listen/silencer_spec.rb index 6a2a3aa7..45de8057 100644 --- a/spec/lib/listen/silencer_spec.rb +++ b/spec/lib/listen/silencer_spec.rb @@ -9,7 +9,7 @@ before { subject.configure(options) } describe '#silenced?' do - it { should accept(:file, Pathname('some_dir') + 'some_file.rb') } + it { should accept(:file, Pathname('some_dir').join("some_file.rb")) } context 'with default ignore' do hidden_ignored = %w[.git .svn .hg .rbx .bundle] @@ -69,7 +69,7 @@ end context 'when ignoring foo/bar* and *.pid' do - let(:options) { { ignore: [%r{^foo\/bar}, /\.pid$/] } } + let(:options) { { ignore: [%r{^foo/bar}, /\.pid$/] } } it { should_not accept(:file, 'foo/bar/baz') } it { should_not accept(:file, 'foo.pid') } end @@ -87,7 +87,7 @@ end context 'when accepting only foo/* and *.txt' do - let(:options) { { only: [%r{^foo\/}, /\.txt$/] } } + let(:options) { { only: [%r{^foo/}, /\.txt$/] } } it { should accept(:file, 'foo/bar.rb') } it { should accept(:file, 'bar.txt') } it { should_not accept(:file, 'bar/baz.rb') } diff --git a/spec/support/acceptance_helper.rb b/spec/support/acceptance_helper.rb index d056c6f3..8c005cdd 100644 --- a/spec/support/acceptance_helper.rb +++ b/spec/support/acceptance_helper.rb @@ -14,9 +14,7 @@ # 2. keep the queue if we're testing for existing accumulated changes # if were testing the queue (e.g. after unpause), don't reset - reset_queue = /queued_/ !~ description - - actual.listen(reset_queue) do + actual.listen(reset_queue: /queued_/ !~ description) do change_fs(type, expected) if reset_queue end actual.changes[type].include? expected @@ -116,7 +114,7 @@ def freeze_offset end # Allow changes only during specific time wine - def allow_changes(reset_queue = true) + def allow_changes(reset_queue: true) @freeze_time = nil if reset_queue # Clear to prepare for collecting new FS events @@ -178,12 +176,12 @@ def changes @timed_changes.changes end - def listen(reset_queue = true) + def listen(reset_queue: true) # Give previous events time to be received, queued and processed # so they complete and don't interfere sleep lag - @timed_changes.allow_changes(reset_queue) do + @timed_changes.allow_changes(reset_queue: reset_queue) do yield # Polling sleep (default: 1s) @@ -225,8 +223,8 @@ def _relative_path(changes) changes.map do |change| unfrozen_copy = change.dup [@paths].flatten.each do |path| - sub = path.sub(%r{\/$}, '').to_s - unfrozen_copy.gsub!(%r{^#{sub}\/}, '') + sub = path.sub(%r{/$}, '').to_s + unfrozen_copy.gsub!(%r{^#{sub}/}, '') end unfrozen_copy end