master/lib/guard/listener.rb

58 lines
1.3 KiB
Ruby
Raw Normal View History

require 'rbconfig'
2010-10-03 21:00:33 +00:00
module Guard
autoload :Darwin, 'guard/listeners/darwin'
autoload :Linux, 'guard/listeners/linux'
autoload :Polling, 'guard/listeners/polling'
2010-10-03 21:00:33 +00:00
class Listener
attr_reader :last_event
def self.init
if mac? && Darwin.usable?
Darwin.new
elsif linux? && Linux.usable?
Linux.new
else
UI.info "Using polling (Please help us to support your system better than that.)"
Polling.new
2010-10-03 21:00:33 +00:00
end
end
def initialize
update_last_event
2010-10-03 21:00:33 +00:00
end
private
def modified_files(dirs, options = {})
files = potentially_modified_files(dirs, options).select { |path| File.file?(path) && recent_file?(path) }
2010-10-03 21:00:33 +00:00
files.map! { |file| file.gsub("#{Dir.pwd}/", '') }
end
def potentially_modified_files(dirs, options = {})
match = options[:all] ? "**/*" : "*"
Dir.glob(dirs.map { |dir| "#{dir}#{match}" })
2010-10-03 21:00:33 +00:00
end
def recent_file?(file)
File.mtime(file) >= last_event
rescue
false
2010-10-03 21:00:33 +00:00
end
def update_last_event
@last_event = Time.now
end
def self.mac?
Config::CONFIG['target_os'] =~ /darwin/i
end
def self.linux?
Config::CONFIG['target_os'] =~ /linux/i
2010-10-03 21:00:33 +00:00
end
end
end