Skip to content

Commit

Permalink
refactor out SimulatedDarwin class
Browse files Browse the repository at this point in the history
  • Loading branch information
e2 committed May 30, 2015
1 parent d501bd9 commit a9e8bb9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/listen/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
require 'listen/adapter/linux'
require 'listen/adapter/polling'
require 'listen/adapter/windows'
require 'listen/adapter/simulated_darwin'

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

Expand Down
9 changes: 7 additions & 2 deletions lib/listen/adapter/linux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,16 @@ def _process_event(dir, event)
_log :debug, "inotify: #{rel_path} (#{event.flags.inspect})"

if /1|true/ =~ ENV['LISTEN_GEM_SIMULATE_FSEVENT']
@darwin ||= Class.new(Darwin) do
def _configure(dir, &callback)
end
end.new({mq: @mq})

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, {})
@darwin.send(:_process_event, dir, [(dir + rel_path).to_s + "/"])
else
_queue_change(:dir, dir, rel_path, {})
@darwin.send(:_process_event, dir, [(dir + rel_path).to_s + "/"])
end
return
end
Expand Down
33 changes: 33 additions & 0 deletions lib/listen/adapter/simulated_darwin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Listen
module Adapter
class SimulatedDarwin < Linux
def self.usable?
os = RbConfig::CONFIG['target_os']
return false unless const_get('OS_REGEXP') =~ os
/1|true/ =~ ENV['LISTEN_GEM_SIMULATE_FSEVENT']
end

def _process_event(dir, event)
# NOTE: avoid using event.absolute_name since new API
# will need to have a custom recursion implemented
# to properly match events to configured directories
path = Pathname.new(event.watcher.path) + event.name
rel_path = path.relative_path_from(dir).to_s

_log :debug, "fake_fsevent: #{rel_path} (#{event.flags.inspect})"

@darwin ||= Class.new(Darwin) do
def _configure(dir, &callback)
end
end.new({mq: @mq})

if (event.flags & [:moved_to, :moved_from]) || _dir_event?(event)
rel_path = path.dirname.relative_path_from(dir).to_s
@darwin.send(:_process_event, dir, [(dir + rel_path).to_s + "/"])
else
@darwin.send(:_process_event, dir, [(dir + rel_path).to_s + "/"])
end
end
end
end
end

0 comments on commit a9e8bb9

Please sign in to comment.