File: darwin.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 (77 lines) | stat: -rw-r--r-- 2,325 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
# frozen_string_literal: true

require 'listen/thread'

module Listen
  module Adapter
    # Adapter implementation for Mac OS X `FSEvents`.
    #
    class Darwin < Base
      OS_REGEXP = /darwin(?<major_version>(1|2)\d+)/i.freeze

      # The default delay between checking for changes.
      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.

        Please add the following to your Gemfile to avoid polling for changes:
          require 'rbconfig'
          if RbConfig::CONFIG['target_os'] =~ /darwin(1[0-3])/i
            gem 'rb-fsevent', '<= 0.9.4'
          end
      EOS

      def self.usable?
        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

        require 'rb-fsevent'
        fsevent_version = Gem::Version.new(FSEvent::VERSION)
        return true if fsevent_version <= Gem::Version.new('0.9.4')
        Listen.adapter_warn(INCOMPATIBLE_GEM_VERSION)
        false
      end

      private

      def _configure(dir, &callback)
        @callbacks[dir] = callback
      end

      def _run
        require 'rb-fsevent'
        worker = FSEvent.new
        dirs_to_watch = @callbacks.keys.map(&:to_s)
        Listen.logger.info { "fsevent: watching: #{dirs_to_watch.inspect}" }
        worker.watch(dirs_to_watch, { latency: options.latency }, &method(:_process_changes))
        @worker_thread = Listen::Thread.new("worker_thread") { worker.run }
      end

      def _process_changes(dirs)
        dirs.each do |dir|
          dir = Pathname.new(dir.sub(%r{/$}, ''))

          @callbacks.each do |watched_dir, callback|
            if watched_dir.eql?(dir) || Listen::Directory.ascendant_of?(watched_dir, dir)
              callback.call(dir)
            end
          end
        end
      end

      def _process_event(dir, path)
        Listen.logger.debug { "fsevent: processing path: #{path.inspect}" }
        # TODO: does this preserve symlinks?
        rel_path = path.relative_path_from(dir).to_s
        _queue_change(:dir, dir, rel_path, recursive: true)
      end

      def _stop
        @worker_thread&.kill
        super
      end
    end
  end
end