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'
|
2011-04-30 10:38:57 +00:00
|
|
|
autoload :Windows, 'guard/listeners/windows'
|
2010-10-17 19:42:40 +00:00
|
|
|
autoload :Polling, 'guard/listeners/polling'
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
class Listener
|
2011-05-13 18:54:39 +00:00
|
|
|
attr_reader :last_event, :sha1_checksums_hash, :directory
|
2011-01-19 22:05:45 +00:00
|
|
|
|
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
|
2011-04-30 10:38:57 +00:00
|
|
|
elsif windows? && Windows.usable?
|
|
|
|
Windows.new
|
2010-10-17 19:42:40 +00:00
|
|
|
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
|
|
|
|
2011-05-13 18:54:39 +00:00
|
|
|
def initialize(directory=Dir.pwd)
|
2011-05-13 21:06:45 +00:00
|
|
|
@directory = directory.to_s
|
2011-05-09 07:39:11 +00:00
|
|
|
@sha1_checksums_hash = {}
|
2010-10-17 19:42:40 +00:00
|
|
|
update_last_event
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-05-13 21:06:45 +00:00
|
|
|
def start
|
|
|
|
watch directory
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_change(&callback)
|
|
|
|
@callback = callback
|
|
|
|
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 = {})
|
2011-05-09 07:39:11 +00:00
|
|
|
files = potentially_modified_files(dirs, options).select { |path| File.file?(path) && file_modified?(path) && file_content_modified?(path) }
|
2011-05-13 18:54:39 +00:00
|
|
|
files.map! { |file| file.gsub("#{directory}/", '') }
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-05-13 21:06:45 +00:00
|
|
|
def worker
|
|
|
|
raise NotImplementedError, "should respond to #watch"
|
|
|
|
end
|
|
|
|
|
|
|
|
# register a directory to watch. must be implemented by the subclasses
|
|
|
|
def watch(directory)
|
|
|
|
raise NotImplementedError, "do whatever you want here, given the directory as only argument"
|
|
|
|
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
|
|
|
|
2011-05-09 07:39:11 +00:00
|
|
|
def file_modified?(path)
|
|
|
|
# Depending on the filesystem, mtime is probably only precise to the second, so round
|
|
|
|
# both values down to the second for the comparison.
|
|
|
|
File.mtime(path).to_i >= last_event.to_i
|
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
|
|
|
|
2011-05-09 07:39:11 +00:00
|
|
|
def file_content_modified?(path)
|
|
|
|
sha1_checksum = Digest::SHA1.file(path).to_s
|
|
|
|
if sha1_checksums_hash[path] != sha1_checksum
|
|
|
|
@sha1_checksums_hash[path] = sha1_checksum
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2011-04-30 10:38:57 +00:00
|
|
|
def self.windows?
|
|
|
|
Config::CONFIG['target_os'] =~ /mswin|mingw/i
|
|
|
|
end
|
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
end
|