File: adapter.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 (43 lines) | stat: -rw-r--r-- 1,372 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
# frozen_string_literal: true

require 'listen/adapter/base'
require 'listen/adapter/bsd'
require 'listen/adapter/darwin'
require 'listen/adapter/linux'
require 'listen/adapter/polling'
require 'listen/adapter/windows'

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

    class << self
      def select(options = {})
        Listen.logger.debug 'Adapter: considering polling ...'
        return Polling if options[:force_polling]
        Listen.logger.debug 'Adapter: considering optimized backend...'
        return _usable_adapter_class if _usable_adapter_class
        Listen.logger.debug 'Adapter: falling back to polling...'
        _warn_polling_fallback(options)
        Polling
      rescue
        Listen.logger.warn format('Adapter: failed: %s:%s', $ERROR_POSITION.inspect,
                                  $ERROR_POSITION * "\n")
        raise
      end

      private

      def _usable_adapter_class
        OPTIMIZED_ADAPTERS.find(&:usable?)
      end

      def _warn_polling_fallback(options)
        msg = options.fetch(:polling_fallback_message, POLLING_FALLBACK_MESSAGE)
        Listen.adapter_warn("[Listen warning]:\n  #{msg}") if msg
      end
    end
  end
end