2010-10-17 19:42:40 +00:00
|
|
|
require 'rbconfig'
|
2010-10-03 21:00:33 +00:00
|
|
|
|
|
|
|
module Guard
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-17 19:42:40 +00:00
|
|
|
autoload :Darwin, 'guard/listeners/darwin'
|
|
|
|
autoload :Linux, 'guard/listeners/linux'
|
|
|
|
autoload :Polling, 'guard/listeners/polling'
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
class Listener
|
2011-01-19 22:05:45 +00:00
|
|
|
attr_reader :last_event
|
|
|
|
|
2010-11-30 20:15:03 +00:00
|
|
|
def self.select_and_init
|
2010-10-17 19:42:40 +00:00
|
|
|
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
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-17 19:42:40 +00:00
|
|
|
def initialize
|
|
|
|
update_last_event
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
|
|
|
def update_last_event
|
|
|
|
@last_event = Time.now
|
2010-11-30 20:15:03 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
|
|
|
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
|
2011-01-19 22:05:45 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def potentially_modified_files(dirs, options = {})
|
2010-10-17 19:42:40 +00:00
|
|
|
match = options[:all] ? "**/*" : "*"
|
|
|
|
Dir.glob(dirs.map { |dir| "#{dir}#{match}" })
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
|
|
|
def recent_file?(file)
|
2010-10-03 21:00:33 +00:00
|
|
|
File.mtime(file) >= last_event
|
2010-10-07 18:53:29 +00:00
|
|
|
rescue
|
|
|
|
false
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-17 19:42:40 +00:00
|
|
|
def self.mac?
|
|
|
|
Config::CONFIG['target_os'] =~ /darwin/i
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-17 19:42:40 +00:00
|
|
|
def self.linux?
|
|
|
|
Config::CONFIG['target_os'] =~ /linux/i
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# require 'rbconfig'
|
|
|
|
#
|
|
|
|
# module Guard
|
|
|
|
#
|
|
|
|
# autoload :Darwin, 'guard/listeners/darwin'
|
|
|
|
# autoload :Linux, 'guard/listeners/linux'
|
|
|
|
# autoload :Polling, 'guard/listeners/polling'
|
|
|
|
#
|
|
|
|
# class Listener
|
|
|
|
# attr_accessor :last_event, :changed_files
|
|
|
|
#
|
|
|
|
# def self.select_and_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
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def initialize
|
|
|
|
# @changed_files = []
|
|
|
|
# update_last_event
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def get_and_clear_changed_files
|
|
|
|
# files = changed_files.dup
|
|
|
|
# changed_files.clear
|
|
|
|
# files.uniq
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# private
|
|
|
|
#
|
|
|
|
# def find_changed_files(dirs, options = {})
|
|
|
|
# files = potentially_changed_files(dirs, options).select { |path| File.file?(path) && changed_file?(path) }
|
|
|
|
# files.map! { |file| file.gsub("#{Dir.pwd}/", '') }
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def potentially_changed_files(dirs, options = {})
|
|
|
|
# match = options[:all] ? "**/*" : "*"
|
|
|
|
# Dir.glob(dirs.map { |dir| "#{dir}#{match}" })
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def changed_file?(file)
|
|
|
|
# File.mtime(file) >= last_event
|
|
|
|
# rescue
|
|
|
|
# false
|
|
|
|
# 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
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# end
|
2011-04-29 22:40:12 +00:00
|
|
|
# end
|