Skip to content

Commit

Permalink
Merge pull request #371 from guard/e2-mri2.2-rubocop_overhaul
Browse files Browse the repository at this point in the history
Update to Ruby 2.2 and RuboCop 0.38
  • Loading branch information
e2 committed Apr 22, 2016
2 parents 3dea1ba + e21066c commit b3899f7
Show file tree
Hide file tree
Showing 41 changed files with 394 additions and 421 deletions.
3 changes: 2 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ inherit_from:

# Rails cops
AllCops:
RunRailsCops: true
RunRailsCops: false

# Files you want to exclude
AllCops:
TargetRubyVersion: 2.2
Exclude:
- db/schema.rb
- Gemfile
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ end
group :development do
gem 'yard', require: false
gem 'guard-rspec', require: false
gem 'rubocop', '0.25.0' # TODO: should match Gemfile HoundCi
gem 'rubocop', '0.38.0' # TODO: should match Gemfile HoundCi
gem 'guard-rubocop'
gem 'pry-rescue'
gem 'pry-stack_explorer', platforms: [:mri, :rbx]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The Listen gem listens to file modifications and notifies you about the changes.
* You can watch multiple directories.
* Regexp-patterns for ignoring paths for more accuracy and speed
* Increased change detection accuracy on OS X HFS and VFAT volumes.
* Tested on MRI Ruby environments (2.0+ only) via [Travis CI](https://travis-ci.org/guard/listen),
* Tested on MRI Ruby environments (2.2+ only) via [Travis CI](https://travis-ci.org/guard/listen),

## Issues / limitations

Expand All @@ -27,7 +27,7 @@ The Listen gem listens to file modifications and notifies you about the changes.
* Specs suite on JRuby and Rubinius aren't reliable on Travis CI, but should work.
* Windows and \*BSD adapter aren't continuously and automatically tested.
* OSX adapter has some performance limitations ([#342](https://github.com/guard/listen/issues/342)).
* Ruby 1.9.3 is no longer maintained (and may not work with Listen) - it's best to upgrade to Ruby 2.2.2.
* Ruby < 2.2.x is no longer supported - upgrade to Ruby 2.2 or 2.3

Pull requests or help is very welcome for these.

Expand Down
5 changes: 1 addition & 4 deletions lib/listen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ def stop
@listeners ||= []

# TODO: should use a mutex for this
@listeners.each do |listener|
# call stop to halt the main loop
listener.stop
end
@listeners.each(&:stop)
@listeners = nil
end
end
Expand Down
52 changes: 27 additions & 25 deletions lib/listen/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,39 @@

module Listen
module Adapter
OPTIMIZED_ADAPTERS = [Darwin, Linux, BSD, Windows]
OPTIMIZED_ADAPTERS = [Darwin, Linux, BSD, Windows].freeze
POLLING_FALLBACK_MESSAGE = 'Listen will be polling for changes.'\
'Learn more at https://github.com/guard/listen#listen-adapters.'
'Learn more at https://github.com/guard/listen#listen-adapters.'.freeze

def self.select(options = {})
_log :debug, 'Adapter: considering polling ...'
return Polling if options[:force_polling]
_log :debug, 'Adapter: considering optimized backend...'
return _usable_adapter_class if _usable_adapter_class
_log :debug, 'Adapter: falling back to polling...'
_warn_polling_fallback(options)
Polling
rescue
_log :warn, format('Adapter: failed: %s:%s', $ERROR_POSITION.inspect,
$ERROR_POSITION * "\n")
raise
end
class << self
def select(options = {})
_log :debug, 'Adapter: considering polling ...'
return Polling if options[:force_polling]
_log :debug, 'Adapter: considering optimized backend...'
return _usable_adapter_class if _usable_adapter_class
_log :debug, 'Adapter: falling back to polling...'
_warn_polling_fallback(options)
Polling
rescue
_log :warn, format('Adapter: failed: %s:%s', $ERROR_POSITION.inspect,
$ERROR_POSITION * "\n")
raise
end

private
private

def self._usable_adapter_class
OPTIMIZED_ADAPTERS.detect(&:usable?)
end
def _usable_adapter_class
OPTIMIZED_ADAPTERS.detect(&:usable?)
end

def self._warn_polling_fallback(options)
msg = options.fetch(:polling_fallback_message, POLLING_FALLBACK_MESSAGE)
Kernel.warn "[Listen warning]:\n #{msg}" if msg
end
def _warn_polling_fallback(options)
msg = options.fetch(:polling_fallback_message, POLLING_FALLBACK_MESSAGE)
Kernel.warn "[Listen warning]:\n #{msg}" if msg
end

def self._log(type, message)
Listen::Logger.send(type, message)
def _log(type, message)
Listen::Logger.send(type, message)
end
end
end
end
10 changes: 7 additions & 3 deletions lib/listen/adapter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Base
attr_reader :options

# TODO: only used by tests
DEFAULTS = {}
DEFAULTS = {}.freeze

attr_reader :config

Expand Down Expand Up @@ -129,8 +129,12 @@ def _log_exception(msg, caller_stack)
_log(:error, formatted)
end

def self._log(*args, &block)
Listen::Logger.send(*args, &block)
class << self
private

def _log(*args, &block)
Listen::Logger.send(*args, &block)
end
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/listen/adapter/bsd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BSD < Base
:rename
# :link, :revoke
]
}
}.freeze

BUNDLER_DECLARE_GEM = <<-EOS.gsub(/^ {6}/, '')
Please add the following to your Gemfile to avoid polling for changes:
Expand All @@ -38,9 +38,9 @@ def self.usable?

private

def _configure(directory, &_callback)
def _configure(directory, &callback)
@worker ||= KQueue::Queue.new
@callback = _callback
@callback = callback
# use Record to make a snapshot of dir, so we
# can detect new files
_find(directory.to_s) { |path| _watch_file(path, @worker) }
Expand Down
2 changes: 1 addition & 1 deletion lib/listen/adapter/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Config
def initialize(directories, queue, silencer, adapter_options)
# Default to current directory if no directories are supplied
directories = [Dir.pwd] if directories.to_a.empty?

# TODO: fix (flatten, array, compact?)
@directories = directories.map do |directory|
Pathname.new(directory.to_s).realpath
Expand Down
16 changes: 10 additions & 6 deletions lib/listen/adapter/darwin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Darwin < Base
OS_REGEXP = /darwin(?<major_version>1\d+)/i

# The default delay between checking for changes.
DEFAULTS = { latency: 0.1 }
DEFAULTS = { latency: 0.1 }.freeze

INCOMPATIBLE_GEM_VERSION = <<-EOS.gsub(/^ {8}/, '')
rb-fsevent > 0.9.4 no longer supports OS X 10.6 through 10.8.
Expand All @@ -23,9 +23,12 @@ class Darwin < Base

def self.usable?
require 'rb-fsevent'
darwin_version = RbConfig::CONFIG['target_os'][OS_REGEXP, :major_version] or return false
return true if darwin_version.to_i >= 13 # darwin13 is OS X 10.9
return true if Gem::Version.new(FSEvent::VERSION) <= Gem::Version.new('0.9.4')
version = RbConfig::CONFIG['target_os'][OS_REGEXP, :major_version]
return false unless version
return true if version.to_i >= 13 # darwin13 is OS X 10.9

fsevent_version = Gem::Version.new(FSEvent::VERSION)
return true if fsevent_version <= Gem::Version.new('0.9.4')
Kernel.warn INCOMPATIBLE_GEM_VERSION
false
end
Expand Down Expand Up @@ -55,7 +58,7 @@ def _run
def _process_event(dir, event)
_log :debug, "fsevent: processing event: #{event.inspect}"
event.each do |path|
new_path = Pathname.new(path.sub(/\/$/, ''))
new_path = Pathname.new(path.sub(%r{\/$}, ''))
_log :debug, "fsevent: #{new_path}"
# TODO: does this preserve symlinks?
rel_path = new_path.relative_path_from(dir).to_s
Expand All @@ -67,7 +70,8 @@ def _run_worker(worker)
_log :debug, "fsevent: running worker: #{worker.inspect}"
worker.run
rescue
_log_exception 'fsevent: running worker failed: %s:%s called from: %s', caller
format_string = 'fsevent: running worker failed: %s:%s called from: %s'
_log_exception format_string, caller
end

def _run_workers_in_background(workers)
Expand Down
8 changes: 3 additions & 5 deletions lib/listen/adapter/linux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class Linux < Base
:close_write
],
wait_for_delay: 0.1
}
}.freeze

private

WIKI_URL = 'https://github.com/guard/listen'\
'/wiki/Increasing-the-amount-of-inotify-watchers'
'/wiki/Increasing-the-amount-of-inotify-watchers'.freeze

INOTIFY_LIMIT_MESSAGE = <<-EOS.gsub(/^\s*/, '')
FATAL: Listen error: unable to monitor directories for changes.
Expand Down Expand Up @@ -50,10 +50,8 @@ def _process_event(dir, event)
if /1|true/ =~ ENV['LISTEN_GEM_SIMULATE_FSEVENT']
if (event.flags & [:moved_to, :moved_from]) || _dir_event?(event)
rel_path = path.dirname.relative_path_from(dir).to_s
_queue_change(:dir, dir, rel_path, {})
else
_queue_change(:dir, dir, rel_path, {})
end
_queue_change(:dir, dir, rel_path, {})
return
end

Expand Down
2 changes: 1 addition & 1 deletion lib/listen/adapter/polling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Adapter
class Polling < Base
OS_REGEXP = // # match every OS

DEFAULTS = { latency: 1.0, wait_for_delay: 0.05 }
DEFAULTS = { latency: 1.0, wait_for_delay: 0.05 }.freeze

private

Expand Down
11 changes: 5 additions & 6 deletions lib/listen/adapter/windows.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ def self.usable?

private

def _configure(dir, &callback)
def _configure(dir)
require 'wdm'
_log :debug, 'wdm - starting...'
@worker ||= WDM::Monitor.new
@worker.watch_recursively(dir.to_s, :files) do |change|
callback.call([:file, change])
yield([:file, change])
end

@worker.watch_recursively(dir.to_s, :directories) do |change|
callback.call([:dir, change])
yield([:dir, change])
end

events = [:attributes, :last_write]
@worker.watch_recursively(dir.to_s, *events) do |change|
callback.call([:attr, change])
yield([:attr, change])
end
end

Expand Down Expand Up @@ -70,7 +70,6 @@ def _process_event(dir, event)
_queue_change(:dir, dir, Pathname(rel_path).dirname.to_s, {})
elsif change.type == :added
_queue_change(:dir, dir, rel_path, {})
else
# do nothing - changed directory means either:
# - removed subdirs (handled above)
# - added subdirs (handled above)
Expand All @@ -81,7 +80,7 @@ def _process_event(dir, event)
end
rescue
details = event.inspect
_log :error, format('wdm - callback (%): %s:%s', details, $ERROR_INFO,
_log :error, format('wdm - callback (%s): %s:%s', details, $ERROR_INFO,
$ERROR_POSITION * "\n")
raise
end
Expand Down
15 changes: 5 additions & 10 deletions lib/listen/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# from exploding with huge test setup blocks
module Listen
class Backend
extend Forwardable

def initialize(directories, queue, silencer, config)
adapter_select_opts = config.adapter_select_options

Expand All @@ -22,17 +24,10 @@ def initialize(directories, queue, silencer, config)
@adapter = adapter_class.new(aconfig)
end

def start
adapter.start
end
delegate start: :adapter
delegate stop: :adapter

def stop
adapter.stop
end

def min_delay_between_events
@min_delay_between_events
end
attr_reader :min_delay_between_events

private

Expand Down
18 changes: 8 additions & 10 deletions lib/listen/change.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def invalidate(type, rel_path, options)
cookie = options[:cookie]

if !cookie && config.silenced?(rel_path, type)
Listen::Logger.debug { "(silenced): #{rel_path.inspect}" }
Listen::Logger.debug { "(silenced): #{rel_path.inspect}" }
return
end

Expand All @@ -49,16 +49,14 @@ def invalidate(type, rel_path, options)
if change
options = cookie ? { cookie: cookie } : {}
config.queue(type, change, watched_dir, rel_path, options)
elsif type == :dir
# NOTE: POSSIBLE RECURSION
# TODO: fix - use a queue instead
Directory.scan(self, rel_path, options)
else
if type == :dir
# NOTE: POSSIBLE RECURSION
# TODO: fix - use a queue instead
Directory.scan(self, rel_path, options)
else
change = File.change(record, rel_path)
return if !change || options[:silence]
config.queue(:file, change, watched_dir, rel_path)
end
change = File.change(record, rel_path)
return if !change || options[:silence]
config.queue(:file, change, watched_dir, rel_path)
end
rescue RuntimeError => ex
msg = format(
Expand Down
8 changes: 2 additions & 6 deletions lib/listen/event/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ def timestamp
Time.now.to_f
end

def event_queue
@event_queue
end
attr_reader :event_queue

def callable?
@block
Expand All @@ -39,9 +37,7 @@ def optimize_changes(changes)
@queue_optimizer.smoosh_changes(changes)
end

def min_delay_between_events
@min_delay_between_events
end
attr_reader :min_delay_between_events

def stopped?
listener.state == :stopped
Expand Down
Loading

0 comments on commit b3899f7

Please sign in to comment.