File: windows.rb

package info (click to toggle)
ruby-listen 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544 kB
  • sloc: ruby: 5,033; makefile: 9
file content (96 lines) | stat: -rw-r--r-- 2,774 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true

module Listen
  module Adapter
    # Adapter implementation for Windows `wdm`.
    #
    class Windows < Base
      OS_REGEXP = /mswin|mingw|cygwin/i.freeze

      BUNDLER_DECLARE_GEM = <<-EOS.gsub(/^ {6}/, '')
        Please add the following to your Gemfile to avoid polling for changes:
          gem 'wdm', '>= 0.1.0' if Gem.win_platform?
      EOS

      def self.usable?
        return false unless super
        require 'wdm'
        true
      rescue LoadError
        Listen.logger.debug format('wdm - load failed: %s:%s', $ERROR_INFO,
                                   $ERROR_POSITION * "\n")

        Listen.adapter_warn(BUNDLER_DECLARE_GEM)
        false
      end

      private

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

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

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

      def _run
        @worker.run!
      end

      # rubocop:disable Metrics/MethodLength
      def _process_event(dir, event)
        Listen.logger.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
          case change.type
          when :removed
            # TODO: check if watched dir?
            _queue_change(:dir, dir, Pathname(rel_path).dirname.to_s, {})
          when :added
            _queue_change(:dir, dir, rel_path, {})
            # 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
      end
      # rubocop:enable Metrics/MethodLength

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