2010-10-17 19:42:40 +00:00
|
|
|
require 'rbconfig'
|
2011-05-24 01:36:51 +00:00
|
|
|
require 'digest/sha1'
|
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
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# The Listener is the base class for all listener
|
|
|
|
# implementations.
|
|
|
|
#
|
2011-09-20 13:07:29 +00:00
|
|
|
# @abstract
|
|
|
|
#
|
2010-10-03 21:00:33 +00:00
|
|
|
class Listener
|
2011-08-30 19:13:51 +00:00
|
|
|
|
2011-09-20 13:07:29 +00:00
|
|
|
# Default paths that gets ignored by the listener
|
2011-09-20 11:58:25 +00:00
|
|
|
DEFAULT_IGNORE_PATHS = %w[. .. .bundle .git log tmp vendor]
|
|
|
|
|
2011-09-01 21:24:45 +00:00
|
|
|
attr_accessor :changed_files
|
2011-10-01 01:45:41 +00:00
|
|
|
attr_reader :directory, :ignore_paths
|
|
|
|
|
|
|
|
def paused?
|
|
|
|
@paused
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Select the appropriate listener implementation for the
|
|
|
|
# current OS and initializes it.
|
|
|
|
#
|
|
|
|
# @param [Array] args the arguments for the listener
|
|
|
|
# @return [Guard::Listener] the chosen listener
|
|
|
|
#
|
|
|
|
def self.select_and_init(*args)
|
2010-10-17 19:42:40 +00:00
|
|
|
if mac? && Darwin.usable?
|
2011-09-20 11:58:25 +00:00
|
|
|
Darwin.new(*args)
|
2010-10-17 19:42:40 +00:00
|
|
|
elsif linux? && Linux.usable?
|
2011-09-20 11:58:25 +00:00
|
|
|
Linux.new(*args)
|
2011-04-30 10:38:57 +00:00
|
|
|
elsif windows? && Windows.usable?
|
2011-09-20 11:58:25 +00:00
|
|
|
Windows.new(*args)
|
2010-10-17 19:42:40 +00:00
|
|
|
else
|
2011-09-20 11:58:25 +00:00
|
|
|
UI.info 'Using polling (Please help us to support your system better than that).'
|
|
|
|
Polling.new(*args)
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Initialize the listener.
|
|
|
|
#
|
|
|
|
# @param [String] directory the root directory to listen to
|
|
|
|
# @option options [Boolean] relativize_paths use only relative paths
|
|
|
|
# @option options [Array<String>] ignore_paths the paths to ignore by the listener
|
|
|
|
#
|
2011-09-01 19:28:03 +00:00
|
|
|
def initialize(directory = Dir.pwd, options = {})
|
2011-09-28 11:46:05 +00:00
|
|
|
@directory = directory.to_s
|
|
|
|
@sha1_checksums_hash = {}
|
|
|
|
@file_timestamp_hash = {}
|
|
|
|
@relativize_paths = options.fetch(:relativize_paths, true)
|
|
|
|
@changed_files = []
|
2011-10-01 01:45:41 +00:00
|
|
|
@paused = false
|
2011-09-28 11:46:05 +00:00
|
|
|
@ignore_paths = DEFAULT_IGNORE_PATHS
|
|
|
|
@ignore_paths |= options[:ignore_paths] if options[:ignore_paths]
|
2011-09-21 22:57:40 +00:00
|
|
|
@watch_all_modifications = options.fetch(:watch_all_modifications, false)
|
2011-09-01 21:24:45 +00:00
|
|
|
|
2010-10-17 19:42:40 +00:00
|
|
|
update_last_event
|
2011-08-30 19:13:51 +00:00
|
|
|
start_reactor
|
|
|
|
end
|
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Start the listener thread.
|
|
|
|
#
|
2011-08-30 19:13:51 +00:00
|
|
|
def start_reactor
|
2011-09-02 14:22:01 +00:00
|
|
|
return if ENV["GUARD_ENV"] == 'test'
|
2011-09-20 11:58:25 +00:00
|
|
|
|
2011-08-30 19:13:51 +00:00
|
|
|
Thread.new do
|
|
|
|
loop do
|
2011-10-01 01:45:41 +00:00
|
|
|
if @changed_files != [] && !@paused
|
2011-08-30 19:13:51 +00:00
|
|
|
changed_files = @changed_files.dup
|
|
|
|
clear_changed_files
|
|
|
|
::Guard.run_on_change(changed_files)
|
|
|
|
else
|
|
|
|
sleep 0.1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Start watching the root directory.
|
|
|
|
#
|
2011-05-13 21:06:45 +00:00
|
|
|
def start
|
2011-07-20 23:29:05 +00:00
|
|
|
watch(@directory)
|
2011-09-13 22:18:32 +00:00
|
|
|
timestamp_files
|
2011-05-13 21:06:45 +00:00
|
|
|
end
|
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Stop listening for events.
|
|
|
|
#
|
2011-05-13 21:06:45 +00:00
|
|
|
def stop
|
|
|
|
end
|
|
|
|
|
2011-10-01 01:45:41 +00:00
|
|
|
# Pause the listener to ignore change events.
|
2011-09-20 11:58:25 +00:00
|
|
|
#
|
2011-10-01 01:45:41 +00:00
|
|
|
def pause
|
|
|
|
@paused = true
|
2011-08-30 19:13:51 +00:00
|
|
|
end
|
|
|
|
|
2011-10-01 01:45:41 +00:00
|
|
|
# Unpause the listener to listen again to change events.
|
2011-09-20 11:58:25 +00:00
|
|
|
#
|
2011-10-01 01:45:41 +00:00
|
|
|
def run
|
|
|
|
@paused = false
|
2011-08-30 19:13:51 +00:00
|
|
|
end
|
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Clear the list of changed files.
|
|
|
|
#
|
2011-08-30 19:13:51 +00:00
|
|
|
def clear_changed_files
|
|
|
|
@changed_files.clear
|
|
|
|
end
|
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Store a listener callback.
|
|
|
|
#
|
|
|
|
# @param [Block] callback the callback to store
|
|
|
|
#
|
2011-05-13 21:06:45 +00:00
|
|
|
def on_change(&callback)
|
|
|
|
@callback = callback
|
|
|
|
end
|
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Updates the timestamp of the last event.
|
|
|
|
#
|
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
|
|
|
|
2011-09-20 13:07:29 +00:00
|
|
|
# Get the modified files.
|
|
|
|
#
|
2011-09-28 10:42:09 +00:00
|
|
|
# If the `:watch_all_modifications` option is true, then moved and
|
|
|
|
# deleted files are also reported, but prefixed by an exclamation point.
|
|
|
|
#
|
|
|
|
# @example Deleted or moved file
|
|
|
|
# !/home/user/dir/file.rb
|
2011-09-26 17:37:54 +00:00
|
|
|
#
|
2011-09-20 13:07:29 +00:00
|
|
|
# @param [Array<String>] dirs the watched directories
|
|
|
|
# @param [Hash] options the listener options
|
2011-09-28 13:47:53 +00:00
|
|
|
# @option options [Symbol] all whether to files in sub directories
|
2011-09-26 17:37:54 +00:00
|
|
|
# @return [Array<String>] paths of files that have been modified
|
2011-09-20 13:07:29 +00:00
|
|
|
#
|
|
|
|
def modified_files(dirs, options = {})
|
2011-09-03 12:43:25 +00:00
|
|
|
last_event = @last_event
|
2011-08-23 16:07:23 +00:00
|
|
|
files = []
|
2011-09-21 22:57:40 +00:00
|
|
|
if @watch_all_modifications
|
2011-08-26 17:05:16 +00:00
|
|
|
deleted_files = @file_timestamp_hash.collect do |path, ts|
|
2011-08-23 16:07:23 +00:00
|
|
|
unless File.exists?(path)
|
|
|
|
@sha1_checksums_hash.delete(path)
|
2011-08-26 17:05:16 +00:00
|
|
|
@file_timestamp_hash.delete(path)
|
2011-08-23 16:07:23 +00:00
|
|
|
"!#{path}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
files.concat(deleted_files.compact)
|
|
|
|
end
|
2011-07-20 23:25:06 +00:00
|
|
|
update_last_event
|
2011-09-13 22:50:24 +00:00
|
|
|
files.concat(potentially_modified_files(dirs, options).select { |path| file_modified?(path, last_event) })
|
2011-09-28 11:46:05 +00:00
|
|
|
|
2011-07-20 23:25:06 +00:00
|
|
|
relativize_paths(files)
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Register a directory to watch.
|
|
|
|
# Must be implemented by the subclasses.
|
|
|
|
#
|
|
|
|
# @param [String] directory the directory to watch
|
|
|
|
#
|
2011-05-13 21:06:45 +00:00
|
|
|
def watch(directory)
|
|
|
|
raise NotImplementedError, "do whatever you want here, given the directory as only argument"
|
|
|
|
end
|
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Get all files that are in the watched directory.
|
|
|
|
#
|
|
|
|
# @return [Array<String>] the list of files
|
|
|
|
#
|
2011-05-15 16:36:23 +00:00
|
|
|
def all_files
|
2011-07-20 23:40:40 +00:00
|
|
|
potentially_modified_files([@directory], :all => true)
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Scopes all given paths to the current directory.
|
|
|
|
#
|
|
|
|
# @param [Array<String>] paths the paths to change
|
|
|
|
# @return [Array<String>] all paths now relative to the current dir
|
|
|
|
#
|
2011-07-20 23:29:05 +00:00
|
|
|
def relativize_paths(paths)
|
|
|
|
return paths unless relativize_paths?
|
2011-05-28 15:15:09 +00:00
|
|
|
paths.map do |path|
|
2011-09-26 17:37:54 +00:00
|
|
|
path.gsub(%r{^(!)?#{ @directory }/},'\1')
|
2011-05-15 16:36:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Use paths relative to the current directory.
|
2011-09-20 11:58:25 +00:00
|
|
|
#
|
|
|
|
# @return [Boolean] whether to use relative or absolute paths
|
|
|
|
#
|
2011-07-20 23:29:05 +00:00
|
|
|
def relativize_paths?
|
|
|
|
!!@relativize_paths
|
2011-05-15 17:00:15 +00:00
|
|
|
end
|
|
|
|
|
2011-09-28 11:41:29 +00:00
|
|
|
# Populate initial timestamp file hash to watch for deleted or moved files.
|
|
|
|
#
|
2011-09-13 22:18:32 +00:00
|
|
|
def timestamp_files
|
2011-09-21 22:57:40 +00:00
|
|
|
all_files.each {|path| set_file_timestamp_hash(path, file_timestamp(path)) } if @watch_all_modifications
|
2011-09-13 22:18:32 +00:00
|
|
|
end
|
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Removes the ignored paths from the directory list.
|
2011-09-20 11:58:25 +00:00
|
|
|
#
|
|
|
|
# @param [Array<String>] dirs the directory to listen to
|
|
|
|
# @param [Array<String>] ignore_paths the paths to ignore
|
|
|
|
# @return children of the passed dirs that are not in the ignore_paths list
|
|
|
|
#
|
2011-09-01 11:30:25 +00:00
|
|
|
def exclude_ignored_paths(dirs, ignore_paths = self.ignore_paths)
|
|
|
|
Dir.glob(dirs.map { |d| "#{d.sub(%r{/+$}, '')}/*" }, File::FNM_DOTMATCH).reject do |path|
|
|
|
|
ignore_paths.include?(File.basename(path))
|
|
|
|
end
|
|
|
|
end
|
2011-05-15 17:00:15 +00:00
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
private
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 23:30:35 +00:00
|
|
|
# Gets a list of files that are in the modified directories.
|
2011-09-20 11:58:25 +00:00
|
|
|
#
|
|
|
|
# @param [Array<String>] dirs the list of directories
|
2011-09-28 13:47:53 +00:00
|
|
|
# @param [Hash] options the find file option
|
|
|
|
# @option options [Symbol] all whether to files in sub directories
|
2011-09-20 11:58:25 +00:00
|
|
|
#
|
|
|
|
def potentially_modified_files(dirs, options = {})
|
2011-09-01 11:30:25 +00:00
|
|
|
paths = exclude_ignored_paths(dirs)
|
2011-07-20 23:40:40 +00:00
|
|
|
|
|
|
|
if options[:all]
|
|
|
|
paths.inject([]) do |array, path|
|
|
|
|
if File.file?(path)
|
|
|
|
array << path
|
|
|
|
else
|
2011-09-20 11:58:25 +00:00
|
|
|
array += Dir.glob("#{ path }/**/*", File::FNM_DOTMATCH).select { |p| File.file?(p) }
|
2011-07-20 23:40:40 +00:00
|
|
|
end
|
|
|
|
array
|
|
|
|
end
|
|
|
|
else
|
|
|
|
paths.select { |path| File.file?(path) }
|
|
|
|
end
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Test if the file content has changed.
|
|
|
|
#
|
2011-09-03 19:33:06 +00:00
|
|
|
# Depending on the filesystem, mtime/ctime is probably only precise to the second, so round
|
2011-05-28 14:47:35 +00:00
|
|
|
# both values down to the second for the comparison.
|
2011-09-20 11:58:25 +00:00
|
|
|
#
|
2011-09-05 08:44:50 +00:00
|
|
|
# ctime is used only on == comparison to always catches Rails 3.1 Assets pipelined on Mac OSX
|
2011-09-20 11:58:25 +00:00
|
|
|
#
|
|
|
|
# @param [String] path the file path
|
|
|
|
# @param [Time] last_event the time of the last event
|
|
|
|
# @return [Boolean] Whether the file content has changed or not.
|
|
|
|
#
|
2011-09-02 14:22:01 +00:00
|
|
|
def file_modified?(path, last_event)
|
2011-09-05 06:22:08 +00:00
|
|
|
ctime = File.ctime(path).to_i
|
|
|
|
mtime = File.mtime(path).to_i
|
|
|
|
if [mtime, ctime].max == last_event.to_i
|
2011-05-28 14:47:35 +00:00
|
|
|
file_content_modified?(path, sha1_checksum(path))
|
2011-09-05 06:22:08 +00:00
|
|
|
elsif mtime > last_event.to_i
|
2011-05-28 14:47:35 +00:00
|
|
|
set_sha1_checksums_hash(path, sha1_checksum(path))
|
|
|
|
true
|
2011-09-21 22:57:40 +00:00
|
|
|
elsif @watch_all_modifications
|
2011-09-13 22:18:32 +00:00
|
|
|
ts = file_timestamp(path)
|
|
|
|
if ts != @file_timestamp_hash[path]
|
|
|
|
set_file_timestamp_hash(path, ts)
|
|
|
|
true
|
|
|
|
end
|
2011-09-03 12:16:32 +00:00
|
|
|
else
|
|
|
|
false
|
2011-05-28 14:47:35 +00:00
|
|
|
end
|
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-09-20 11:58:25 +00:00
|
|
|
# Tests if the file content has been modified by
|
|
|
|
# comparing the SHA1 checksum.
|
|
|
|
#
|
|
|
|
# @param [String] path the file path
|
|
|
|
# @param [String] sha1_checksum the checksum of the file
|
|
|
|
#
|
2011-05-28 14:47:35 +00:00
|
|
|
def file_content_modified?(path, sha1_checksum)
|
2011-07-20 23:29:05 +00:00
|
|
|
if @sha1_checksums_hash[path] != sha1_checksum
|
2011-05-28 14:47:35 +00:00
|
|
|
set_sha1_checksums_hash(path, sha1_checksum)
|
2011-05-09 07:39:11 +00:00
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-26 17:37:54 +00:00
|
|
|
# Set save a files current timestamp
|
|
|
|
#
|
|
|
|
# @param [String] path the file path
|
|
|
|
# @param [Int] file_timestamp the files modified timestamp
|
|
|
|
#
|
2011-08-26 17:05:16 +00:00
|
|
|
def set_file_timestamp_hash(path, file_timestamp)
|
|
|
|
@file_timestamp_hash[path] = file_timestamp
|
|
|
|
end
|
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Set the current checksum of a file.
|
|
|
|
#
|
|
|
|
# @param [String] path the file path
|
|
|
|
# @param [String] sha1_checksum the checksum of the file
|
|
|
|
#
|
2011-05-28 14:47:35 +00:00
|
|
|
def set_sha1_checksums_hash(path, sha1_checksum)
|
|
|
|
@sha1_checksums_hash[path] = sha1_checksum
|
|
|
|
end
|
|
|
|
|
2011-09-26 17:37:54 +00:00
|
|
|
# Gets a files modified timestamp
|
|
|
|
#
|
|
|
|
# @path [String] path the file path
|
|
|
|
# @return [Int] file modified timestamp
|
|
|
|
#
|
2011-08-26 17:05:16 +00:00
|
|
|
def file_timestamp(path)
|
|
|
|
File.mtime(path).to_i
|
|
|
|
end
|
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Calculates the SHA1 checksum of a file.
|
|
|
|
#
|
|
|
|
# @param [String] path the path to the file
|
|
|
|
# @return [String] the SHA1 checksum
|
|
|
|
#
|
2011-05-28 14:47:35 +00:00
|
|
|
def sha1_checksum(path)
|
2011-05-28 14:50:16 +00:00
|
|
|
Digest::SHA1.file(path).to_s
|
2011-05-28 14:47:35 +00:00
|
|
|
end
|
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Test if the OS is Mac OS X.
|
|
|
|
#
|
|
|
|
# @return [Boolean] Whether the OS is Mac OS X
|
|
|
|
#
|
2010-10-17 19:42:40 +00:00
|
|
|
def self.mac?
|
2011-06-16 11:14:51 +00:00
|
|
|
RbConfig::CONFIG['target_os'] =~ /darwin/i
|
2010-10-17 19:42:40 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Test if the OS is Linux.
|
|
|
|
#
|
|
|
|
# @return [Boolean] Whether the OS is Linux
|
|
|
|
#
|
2010-10-17 19:42:40 +00:00
|
|
|
def self.linux?
|
2011-06-16 11:14:51 +00:00
|
|
|
RbConfig::CONFIG['target_os'] =~ /linux/i
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
|
2011-09-20 11:58:25 +00:00
|
|
|
# Test if the OS is Windows.
|
|
|
|
#
|
|
|
|
# @return [Boolean] Whether the OS is Windows
|
|
|
|
#
|
2011-04-30 10:38:57 +00:00
|
|
|
def self.windows?
|
2011-06-16 11:14:51 +00:00
|
|
|
RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i
|
2011-04-30 10:38:57 +00:00
|
|
|
end
|
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2011-01-19 22:05:45 +00:00
|
|
|
end
|