Class: Bolt::Listeners::Generic

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/listeners/generic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGeneric

constructor



13
14
15
16
# File 'lib/bolt/listeners/generic.rb', line 13

def initialize
  self.interval = 1 # decrease the CPU load by increasing the interval
  self.busy     = false
end

Instance Attribute Details

#busyObject

Returns the value of attribute busy.



10
11
12
# File 'lib/bolt/listeners/generic.rb', line 10

def busy
  @busy
end

#filesObject

Returns the value of attribute files.



10
11
12
# File 'lib/bolt/listeners/generic.rb', line 10

def files
  @files
end

#intervalObject

Returns the value of attribute interval.



10
11
12
# File 'lib/bolt/listeners/generic.rb', line 10

def interval
  @interval
end

#mappingsObject

Returns the value of attribute mappings.



10
11
12
# File 'lib/bolt/listeners/generic.rb', line 10

def mappings
  @mappings
end

#notifierObject

Returns the value of attribute notifier.



10
11
12
# File 'lib/bolt/listeners/generic.rb', line 10

def notifier
  @notifier
end

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/bolt/listeners/generic.rb', line 10

def parent
  @parent
end

Instance Method Details

#check_filesObject

check files to find these that have changed



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bolt/listeners/generic.rb', line 34

def check_files
  return if busy # if working on something already, skip the iteration
  updated = []
  files.each do |filename, mtime| 
    begin
      current_mtime = File.stat(filename).mtime
    rescue Errno::ENOENT
      # file was not found and was probably deleted
      # remove the file from the file list 
      files.delete(filename)
      puts "=> ERROR: #{filename} not found, ignoring" if Bolt.verbose?
      next
    end
    if current_mtime != mtime  
      updated << filename
      # update the mtime in file registry so we it's only send once
      files[filename] = current_mtime
      $stdout.puts ">> Spotted change in #{filename}" if Bolt.verbose?
    end
  end
  parent.handle(updated) if updated != []
  false
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. source: ZenTest/autotest.rb



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bolt/listeners/generic.rb', line 63

def find_files
  result = {}
  targets = ['.'] # start simple
  targets.each do |target|
    order = []
    Find.find(target) do |f|
      in_mappings = f =~ self.mappings
      next if in_mappings.nil?
      next if test ?d, f
      next if f =~ /(swp|~|rej|orig)$/ # temporary/patch files
      next if f =~ /(\.svn|\.git)$/ # subversion/git
      next if f =~ /\/\.?#/ # Emacs autosave/cvs merge files

      filename = f.sub(/^\.\//, '')

      result[filename] = File.stat(filename).mtime rescue next
    end
  end
  
  self.files = result
end

#startObject

find files and start the listener



19
20
21
22
23
24
25
# File 'lib/bolt/listeners/generic.rb', line 19

def start
  puts "** #{self.class} is scanning for files... " if Bolt.verbose?
  # build a file collection
  find_files
  puts "** #{self.class} watching #{files.size} files... "
  wait  
end

#waitObject

wait for a specified interval and check files for changes source: ZenTest/autotest.rb



29
30
31
# File 'lib/bolt/listeners/generic.rb', line 29

def wait
  Kernel.sleep self.interval until check_files
end