Class: Autometric

Inherits:
Auto show all
Defined in:
lib/autometric.rb

Constant Summary collapse

VERSION =
'0.0.1'
RUNNERS =
{
'c' => Autocoverage,
'y' => Autocyclo,
't' => Autotoken,
'f' => Autoflog
}

Constants inherited from Auto

Auto::SEP, Auto::WINDOZE

Instance Attribute Summary collapse

Attributes inherited from Auto

#file_masks, #modified, #output, #sleep, #wants_to_quit, #working_dirs

Instance Method Summary collapse

Methods inherited from Auto

add_discovery, add_hook, autodiscover, #find_files, #hook, #last_modified_time, #method_missing, #old_method_missing, run, #run_metric, #wait_for_changes

Constructor Details

#initialize(argv = []) ⇒ Autometric

Returns a new instance of Autometric.



24
25
26
27
# File 'lib/autometric.rb', line 24

def initialize(argv = [])
  @argv = argv
  @wants_to_quit = @interrupted = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Auto

Instance Attribute Details

#argvObject

Returns the value of attribute argv.



22
23
24
# File 'lib/autometric.rb', line 22

def argv
  @argv
end

Instance Method Details

#add_sigint_handlerObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/autometric.rb', line 60

def add_sigint_handler
  trap 'INT' do
    if @interrupted then
      @wants_to_quit = true
    else
      unless hook :interrupt then
        puts "Interrupt a second time to quit"
        @interrupted = true
        Kernel::sleep(1.5)
      end
      raise Interrupt, nil # let the run loop catch it
    end
  end
end

#find_target(klass) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/autometric.rb', line 85

def find_target(klass)
  style = klass.autodiscover
  target = klass
  unless style.empty? then
    mod = "autometric/#{style.join("_")}"
    puts "loading #{mod}"

    begin
      require mod
    rescue LoadError
      abort "#{klass.name} style #{mod} doesn't seem to exist.  Aborting."
    end
    target = klass.const_get(style.map{|s| s.capitalize}.join)
  end
  target
end

#resetObject



102
103
104
105
106
107
108
109
110
111
# File 'lib/autometric.rb', line 102

def reset
  @interrupted = @wants_to_quit = false
  @thread_map.each do |target, thread|
    thread.kill if thread.status
    @thread_map[target] = Thread.start(target) do |target|
      target.run
      target.hook :thread_terminated
    end
  end
end

#runObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/autometric.rb', line 29

def run()
  targets = runners.collect { |klass| find_target klass }
  targets = targets.find_all {|target| target.meets_requirements? }
  
  return if targets.empty?
  
  @thread_map = targets.to_h do |target|
    Thread.start(target) do |target|
      target.run
      target.hook :thread_terminated
    end
  end
  
  hook :run
  add_sigint_handler
  
  loop do # ^c handler
    begin
      Kernel::sleep
    rescue Interrupt
      if @wants_to_quit
        break
      else
        reset
      end
    end
  end
  
  hook :quit
end

#runnersObject



75
76
77
78
79
80
81
82
83
# File 'lib/autometric.rb', line 75

def runners
  return RUNNERS.values if @argv.any? {|arg| arg =~ /a/ } || @argv.empty?
  
  klasses = @argv.collect do |arg|
    RUNNERS.collect { |flag, klass| klass if arg =~ Regexp.compile(flag) }.compact
  end
  
  klasses.flatten
end