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

Instance Attribute Summary

Attributes inherited from Base

#listener

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#_directories, #_log, #_notify_change, #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

#_attr_callbackObject (private)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/listen/adapter/windows.rb', line 57

def _attr_callback
  lambda do |change|
    begin
      path = _path(change.path)
      return if path.directory?

      _log :debug, "wdm - ATTR callback: #{change.inspect}"
      options = { change: _change(change.type) }
      _notify_change(:file, _path(change.path), options)
    rescue
      _log :error, "wdm - callback failed: #{$!}:#{$@.join("\n")}"
      raise
    end
  end
end

#_change(type) ⇒ Object (private)



101
102
103
104
105
106
107
108
# File 'lib/listen/adapter/windows.rb', line 101

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

#_configureObject (private)



28
29
30
31
32
33
34
35
36
37
# File 'lib/listen/adapter/windows.rb', line 28

def _configure
  _log :debug, 'wdm - starting...'
  @worker = WDM::Monitor.new
  _directories.each do |path|
    @worker.watch_recursively(path.to_s, :files, &_file_callback)
    @worker.watch_recursively(path.to_s, :directories, &_dir_callback)
    @worker.watch_recursively(path.to_s, :attributes, :last_write,
                              &_attr_callback)
  end
end

#_dir_callbackObject (private)



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/listen/adapter/windows.rb', line 73

def _dir_callback
  lambda do |change|
    begin
      path = _path(change.path)
      _log :debug, "wdm - DIR callback: #{change.inspect}"
      if change.type == :removed
        _notify_change(:dir, path.dirname)
      elsif change.type == :added
        _notify_change(:dir, 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
    rescue
      _log :error, "wdm - callback failed: #{$!}:#{$@.join("\n")}"
      raise
    end
  end
end

#_file_callbackObject (private)



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/listen/adapter/windows.rb', line 43

def _file_callback
  lambda do |change|
    begin
      path = _path(change.path)
      _log :debug, "wdm - FILE callback: #{change.inspect}"
      options = { change: _change(change.type) }
      _notify_change(:file, path, options)
    rescue
      _log :error, "wdm - callback failed: #{$!}:#{$@.join("\n")}"
      raise
    end
  end
end

#_path(path) ⇒ Object (private)



97
98
99
# File 'lib/listen/adapter/windows.rb', line 97

def _path(path)
  Pathname.new(path)
end

#_runObject (private)



39
40
41
# File 'lib/listen/adapter/windows.rb', line 39

def _run
  @worker.run!
end