Sha256: 7a8dc66f75bc82302b5587ed3788746b011c7ece2b473b2346866dc86acddbbe
Contents?: true
Size: 1.33 KB
Versions: 3
Compression:
Stored size: 1.33 KB
Contents
require 'sys/uname' module Guard class Listener attr_reader :last_event, :callback, :pipe def initialize update_last_event end def on_change(&block) @callback = block end def start @pipe = case Sys::Uname.sysname when 'Darwin' IO.popen("#{bin_path}/fsevent_watch .") when 'Linux' IO.popen("#{bin_path}/inotify_watch .") end watch_change end def stop Process.kill("HUP", pipe.pid) if pipe end private def watch_change while !pipe.eof? if line = pipe.readline modified_dirs = line.split(" ") files = modified_files(modified_dirs) update_last_event callback.call(files) end end end def modified_files(dirs) files = potentially_modified_files(dirs).select { |file| recent_file?(file) } files.map! { |file| file.gsub("#{Dir.pwd}/", '') } end def potentially_modified_files(dirs) Dir.glob(dirs.map { |dir| "#{dir}*" }) end def recent_file?(file) File.mtime(file) >= last_event rescue false end def update_last_event @last_event = Time.now end def bin_path File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'bin')) end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
guard-0.1.1 | lib/guard/listener.rb |
guard-0.1.0 | lib/guard/listener.rb |
guard-0.1.0.beta.2 | lib/guard/listener.rb |