Class: Watch

Inherits:
Object
  • Object
show all
Defined in:
lib/aml/watch.rb

Instance Method Summary collapse

Constructor Details

#initialize(filenames, arguments, instance, build) ⇒ Watch

Returns a new instance of Watch.



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/aml/watch.rb', line 2

def initialize(filenames, arguments, instance, build)
	@arguments = arguments
	@instance = instance
	filenames = [filenames] if(filenames.kind_of?String)
	@last_mtimes = {}
	filenames.each do |filename|
		@last_mtimes[filename] = File.stat(filename).mtime if File.exists?(filename)
	end
	@filenames = filenames
	watch
end

Instance Method Details

#file_updated?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/aml/watch.rb', line 23

def file_updated?
	@filenames.each do |filename|
		if File.exists?(filename)
			mtime = File.stat(filename).mtime
			updated = @last_mtimes[filename] < mtime
			@last_mtimes[filename] = mtime
			if(updated)
				exec("aml #{@arguments} --aml-watch-instance #{@instance}")
				Kernel.exit!
				return true
			end
		end
	end
	return false
end

#watch(sleep = 1) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/aml/watch.rb', line 13

def watch(sleep=1)
	loop do
		begin
			Kernel.sleep sleep until file_updated?
			rescue SystemExit,Interrupt
			puts ""
			Kernel.exit!
		end
	end
end