Class: Listen::Adapter::Windows

Inherits:
Base
  • Object
show all
Defined in:
lib/listen/adapter/windows.rb

Overview

Adapter implementation for Windows wdm.

Constant Summary collapse

OS_REGEXP =
/mswin|mingw|cygwin/i
BUNDLER_DECLARE_GEM =
<<-EOS.gsub(/^ {6}/, '')
  Please add the following to your Gemfile to avoid polling for changes:
    require 'rbconfig'
    if RbConfig::CONFIG['target_os'] =~ /mswin|mingw|cygwin/i
      gem 'wdm', '>= 0.1.0'
    end
EOS

Constants inherited from Base

Base::DEFAULTS

Instance Attribute Summary

Attributes inherited from Base

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#_log, _log, #_queue_change, #configure, #initialize, local_fs?, #start

Constructor Details

This class inherits a constructor from Listen::Adapter::Base

Class Method Details

.usable?Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
# File 'lib/listen/adapter/windows.rb', line 16

def self.usable?
  return false unless super
  require 'wdm'
  true
rescue LoadError
  _log :debug, "wdm - load failed: #{$!}:#{$@.join("\n")}"
  Kernel.warn BUNDLER_DECLARE_GEM
  false
end

Instance Method Details

#_change(type) ⇒ Object (private)



89
90
91
92
93
94
95
96
# File 'lib/listen/adapter/windows.rb', line 89

def _change(type)
  { modified: [:modified, :attrib], # TODO: is attrib really passed?
    added:    [:added, :renamed_new_file],
    removed:  [:removed, :renamed_old_file] }.each do |change, types|
    return change if types.include?(type)
  end
  nil
end

#_configure(dir, &callback) ⇒ Object (private)



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/listen/adapter/windows.rb', line 28

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

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

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

#_process_event(dir, event) ⇒ Object (private)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/listen/adapter/windows.rb', line 50

def _process_event(dir, event)
  _log :debug, "wdm - callback: #{event.inspect}"

  type, change = event

  full_path = Pathname(change.path)

  rel_path = full_path.relative_path_from(dir).to_s

  options = { change: _change(change.type) }

  case type
  when :file
    _queue_change(:file, dir, rel_path, options)
  when :attr
    unless full_path.directory?
      _queue_change(:file, dir, rel_path, options)
    end
  when :dir
    if change.type == :removed
      # TODO: check if watched dir?
      _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)
      #   - removed files (handled by _file_callback)
      #   - added files (handled by _file_callback)
      # so what's left?
    end
  end
rescue
  details = event.inspect
  _log :error, "wdm - callback (#{details}): #{$!}:#{$@.join("\n")}"
  raise
end

#_runObject (private)



46
47
48
# File 'lib/listen/adapter/windows.rb', line 46

def _run
  @worker.run!
end