Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 79 lines (66 sloc) 1.615 kb
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
module Guard
  class Linux < Listener
    attr_reader :inotify, :files, :latency

    def initialize(*)
      super

      @inotify = INotify::Notifier.new
      @files = []
      @latency = 0.5
    end

    def start
      @stop = false
      super
      watch_change unless watch_change?
    end

    def stop
      super
      @stop = true
      sleep latency
    end

    def self.usable?
      require 'rb-inotify'
      if !defined?(INotify::VERSION) || (defined?(Gem::Version) &&
          Gem::Version.new(INotify::VERSION.join('.')) < Gem::Version.new('0.8.5'))
        UI.info "Please update rb-inotify (>= 0.8.5)"
        false
      else
        true
      end
    rescue LoadError
      UI.info "Please install rb-inotify gem for Linux inotify support"
      false
    end

    def watch_change?
      !!@watch_change
    end

  private

    def worker
      @inotify
    end

    def watch(directory)
      worker.watch(directory, :recursive, :modify, :create) do |event|
        unless event.name == "" # Event on root directory
          @files << event.absolute_name
        end
      end
    rescue Interrupt
    end

    def watch_change
      @watch_change = true
      until @stop
        if Config::CONFIG['build'] =~ /java/ || IO.select([inotify.to_io], [], [], latency)
          break if @stop

          sleep latency
          inotify.process
          update_last_event

          unless files.empty?
            files.uniq!
            callback.call( relativate_paths(files) )
            files.clear
          end
        end
      end
      @watch_change = false
    end

  end
end
Something went wrong with that request. Please try again.