Sha256: 185deefa02ac0f690463bed800d7b228e6c9311548f4362f63ef96f4c9f9deb8

Contents?: true

Size: 1.98 KB

Versions: 153

Compression:

Stored size: 1.98 KB

Contents

module Guard

  # Listener implementation for Linux `inotify`.
  #
  class Linux < Listener

    # Initialize the Listener.
    #
    def initialize(*)
      super
      @inotify = INotify::Notifier.new
      @files   = []
      @latency = 0.5
    end

    # Start the listener.
    #
    def start
      @stop = false
      super
      watch_change unless watch_change?
    end

    # Stop the listener.
    #
    def stop
      super
      @stop = true
    end

    # Check if the listener is usable on the current OS.
    #
    # @return [Boolean] whether usable or not
    #
    def self.usable?(no_vendor = false)
      return false unless RbConfig::CONFIG['target_os'] =~ /linux/i

      $LOAD_PATH << File.expand_path('../../../vendor/linux/lib', __FILE__) unless no_vendor
      require 'rb-inotify'
      true
    rescue LoadError
      false
    end

    private

    # Get the listener worker.
    #
    def worker
      @inotify
    end

    # Watch the given directory for file changes.
    #
    # @param [String] directory the directory to watch
    #
    def watch(directory)
      worker.watch(directory, :recursive, :attrib, :create, :move_self, :close_write) do |event|
        unless event.name == "" # Event on root directory
          @files << event.absolute_name
        end
      end
    rescue Interrupt
    end

    # Test if inotify is watching for changes.
    #
    # @return [Boolean] whether inotify is active or not
    #
    def watch_change?
      !!(defined? @watch_change and @watch_change)
    end

    # Watch for file system changes.
    #
    def watch_change
      @watch_change = true
      until @stop
        if RbConfig::CONFIG['build'] =~ /java/ || IO.select([worker.to_io], [], [], @latency)
          break if @stop

          sleep(@latency)
          worker.process

          files = modified_files(@files.shift(@files.size).map { |f| File.dirname(f) }.uniq)
          @callback.call(files) unless files.empty?
        end
      end
      @watch_change = false
    end

  end
end

Version data entries

153 entries across 61 versions & 3 rubygems

Version Path
classiccms-0.6.9 vendor/bundle/gems/guard-1.0.1/lib/guard/listeners/linux.rb
classiccms-0.6.8 vendor/bundle/gems/guard-1.0.3/lib/guard/listeners/linux.rb
classiccms-0.6.8 vendor/bundle/gems/guard-1.0.2/lib/guard/listeners/linux.rb
classiccms-0.6.8 vendor/bundle/gems/guard-1.0.1/lib/guard/listeners/linux.rb
classiccms-0.6.7 vendor/bundle/gems/guard-1.0.1/lib/guard/listeners/linux.rb
classiccms-0.6.7 vendor/bundle/gems/guard-1.0.2/lib/guard/listeners/linux.rb
classiccms-0.6.7 vendor/bundle/gems/guard-1.0.3/lib/guard/listeners/linux.rb
classiccms-0.6.6 vendor/bundle/gems/guard-1.0.3/lib/guard/listeners/linux.rb
classiccms-0.6.6 vendor/bundle/gems/guard-1.0.2/lib/guard/listeners/linux.rb
classiccms-0.6.6 vendor/bundle/gems/guard-1.0.1/lib/guard/listeners/linux.rb
classiccms-0.6.5 vendor/bundle/gems/guard-1.0.1/lib/guard/listeners/linux.rb
classiccms-0.6.5 vendor/bundle/gems/guard-1.0.2/lib/guard/listeners/linux.rb
classiccms-0.6.5 vendor/bundle/gems/guard-1.0.3/lib/guard/listeners/linux.rb
classiccms-0.6.4 vendor/bundle/gems/guard-1.0.2/lib/guard/listeners/linux.rb
classiccms-0.6.4 vendor/bundle/gems/guard-1.0.3/lib/guard/listeners/linux.rb
classiccms-0.6.4 vendor/bundle/gems/guard-1.0.1/lib/guard/listeners/linux.rb
classiccms-0.6.3 vendor/bundle/gems/guard-1.0.2/lib/guard/listeners/linux.rb
classiccms-0.6.3 vendor/bundle/gems/guard-1.0.1/lib/guard/listeners/linux.rb
classiccms-0.6.3 vendor/bundle/gems/guard-1.0.3/lib/guard/listeners/linux.rb
classiccms-0.6.2 vendor/bundle/gems/guard-1.0.2/lib/guard/listeners/linux.rb