Class: FileChangeWatcher

Inherits:
Object show all
Defined in:
lib/feldtruby/file/file_change_watcher.rb

Overview

Watch for file changes in given paths then call hooks with the updated files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(specifiedDirectories = ["."], sleepTime = 5*60, excludeFilesRegexps = [], &runWhenFilesUpdated) ⇒ FileChangeWatcher

Returns a new instance of FileChangeWatcher.



16
17
18
19
20
21
22
23
24
# File 'lib/feldtruby/file/file_change_watcher.rb', line 16

def initialize(specifiedDirectories = ["."], sleepTime = 5*60, excludeFilesRegexps = [], &runWhenFilesUpdated)
	specified_directories  = specifiedDirectories.reject { |path| path.starts_with?("-") }
	self.find_directories  = specified_directories.empty? ? ['.'] : specified_directories
	self.exclusions = excludeFilesRegexps
	self.sleep_time = sleepTime
	self.last_mtime = nil # Ensure we run first time when started
	@hooks = Hash.new { |h,k| h[k] = [] }
	add_hook :updated, &runWhenFilesUpdated if runWhenFilesUpdated
end

Instance Attribute Details

#exclusionsObject

Returns the value of attribute exclusions.



14
15
16
# File 'lib/feldtruby/file/file_change_watcher.rb', line 14

def exclusions
  @exclusions
end

#find_directoriesObject

Returns the value of attribute find_directories.



14
15
16
# File 'lib/feldtruby/file/file_change_watcher.rb', line 14

def find_directories
  @find_directories
end

#last_mtimeObject

Returns the value of attribute last_mtime.



14
15
16
# File 'lib/feldtruby/file/file_change_watcher.rb', line 14

def last_mtime
  @last_mtime
end

#sleep_timeObject

Returns the value of attribute sleep_time.



14
15
16
# File 'lib/feldtruby/file/file_change_watcher.rb', line 14

def sleep_time
  @sleep_time
end

Instance Method Details

#add_hook(name, &block) ⇒ Object

Add the supplied block to the available hooks, with the given name.



64
65
66
67
# File 'lib/feldtruby/file/file_change_watcher.rb', line 64

def add_hook name, &block
	# New hooks added in front
	@hooks[name] = [block] + @hooks[name]
end

#exclude_matched_files(files, exclusionRegexps) ⇒ Object



45
46
47
# File 'lib/feldtruby/file/file_change_watcher.rb', line 45

def exclude_matched_files(files, exclusionRegexps)
	files.reject {|f| exclusionRegexps.any? {|exre| exre.match(f)}}
end

#find_filesObject

Find the files to process, ignoring temporary files, source configuration management files, etc., and return a Hash mapping filename to modification time.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/feldtruby/file/file_change_watcher.rb', line 29

def find_files
	result = {}
	targets = self.find_directories
	targets.each do |target|
		Find.find target do |f|
			next if test ?d, f
			next if f =~ /(swp|~|rej|orig)$/ # temporary/patch files
			next if f =~ /^\.\/tmp/          # temporary dir, used by isolate
			next if f =~ /\/\.?#/            # Emacs autosave/cvs merge files
			filename = f.sub(/^\.\//, '')
			result[filename] = File.stat(filename).mtime rescue next
		end
	end
	result
end

#find_updated_files(files = find_files) ⇒ Object

Find files that has changed since last time. Call updated hook if any found.



51
52
53
54
55
56
57
58
59
60
# File 'lib/feldtruby/file/file_change_watcher.rb', line 51

def find_updated_files files = find_files
	hook :checkingChanges, files
	updated = self.last_mtime.nil? ? files : files.select { |filename, mtime| self.last_mtime < mtime }
	updated = exclude_matched_files(updated, self.exclusions)

	unless updated.empty? then
		self.last_mtime = Time.now
		hook :updated, updated
	end
end

#hook(name, *args) ⇒ Object

Call the event hook named name, passing in optional args depending on the hook itself.

Returns false if no hook handled the event.



74
75
76
# File 'lib/feldtruby/file/file_change_watcher.rb', line 74

def hook name, *args
	@hooks[name].any? { |plugin| plugin[self, *args] }
end

#wait_for_changesObject



78
79
80
81
# File 'lib/feldtruby/file/file_change_watcher.rb', line 78

def wait_for_changes
	hook :waiting
	Kernel.sleep self.sleep_time until find_updated_files
end