Sha256: ccafce8dee90264bdd93f8e11a03cc49bfc05abf6facfe8d1ed6675dbd2c354f

Contents?: true

Size: 1.57 KB

Versions: 4

Compression:

Stored size: 1.57 KB

Contents

module Guard
  class Linux < Listener
    attr_reader :inotify, :files, :latency, :callback

    def initialize
      super

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

    def start
      @stop = false
      watch_change unless watch_change?
    end

    def stop
      @stop = true
      sleep latency
    end

    def on_change(&callback)
      @callback = callback
      inotify.watch(Dir.pwd, :recursive, :modify, :create, :delete, :move) do |event|
        unless event.name == "" # Event on root directory
          @files << event.absolute_name
        end
      end
    rescue Interrupt
    end

    def self.usable?
      require 'rb-inotify'
      if !defined?(INotify::VERSION) || Gem::Version.new(INotify::VERSION.join('.')) < Gem::Version.new('0.5.1')
        UI.info "Please update rb-inotify (>= 0.5.1)"
        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 watch_change
      @watch_change = true
      while !@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!
            files.map! { |file| file.gsub("#{Dir.pwd}/", '') }
            callback.call(files)
            files.clear
          end
        end
      end
      @watch_change = false
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
guard-0.3.4 lib/guard/listeners/linux.rb
guard-0.3.3 lib/guard/listeners/linux.rb
guard-0.3.2 lib/guard/listeners/linux.rb
guard-0.3.1 lib/guard/listeners/linux.rb