Class: MetricFu::Flog

Inherits:
Generator show all
Defined in:
lib/generators/flog.rb

Defined Under Namespace

Classes: Operator, Page, ScannedMethod

Constant Summary collapse

SCORE_FORMAT =
"%0.2f"
METHOD_LINE_REGEX =
/(\d+\.\d+):\s+([A-Za-z:]+#.*)/
OPERATOR_LINE_REGEX =
/\s*(\d+\.\d+):\s(.*)$/

Instance Attribute Summary collapse

Attributes inherited from Generator

#report, #template

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generator

class_name, #create_data_dir_if_missing, #create_metric_dir_if_missing, #create_output_dir_if_missing, generate_report, #generate_report, #initialize, #metric_directory, metric_directory, #round_to_tenths, #to_graph

Constructor Details

This class inherits a constructor from MetricFu::Generator

Instance Attribute Details

#pagesObject (readonly)

Returns the value of attribute pages.



4
5
6
# File 'lib/generators/flog.rb', line 4

def pages
  @pages
end

Class Method Details

.verify_dependencies!Object



6
7
8
9
# File 'lib/generators/flog.rb', line 6

def self.verify_dependencies!
  `flog --help`
  raise 'sudo gem install flog # if you want the flog tasks' unless $?.success?
end

Instance Method Details

#analyzeObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/generators/flog.rb', line 51

def analyze
  @pages = []
  flog_results.each do |path|
    page = parse(open(path, "r") { |f| f.read })
    if page
      page.path = path.sub(metric_directory, "").sub(".txt", ".rb") 
      @pages << page
    end
  end
end

#emitObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/generators/flog.rb', line 15

def emit
  metric_dir = MetricFu::Flog.metric_directory
  MetricFu.flog[:dirs_to_flog].each do |directory|
    Dir.glob("#{directory}/**/*.rb").each do |filename|
      output_dir = "#{metric_dir}/#{filename.split("/")[0..-2].join("/")}"
      mkdir_p(output_dir, :verbose => false) unless File.directory?(output_dir)
      if MetricFu::MD5Tracker.file_changed?(filename, metric_dir)
        `flog -ad #{filename} > #{metric_dir}/#{filename.split('.')[0]}.txt`
      end
    end
  end
rescue LoadError
  if RUBY_PLATFORM =~ /java/
    puts 'running in jruby - flog tasks not available'
  else
    puts 'sudo gem install flog # if you want the flog tasks'
  end
end

#parse(text) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/generators/flog.rb', line 34

def parse(text)
  summary, methods_summary = text.split "\n\n"
  return unless summary
  score, average = summary.split("\n").map {|line| line[OPERATOR_LINE_REGEX, 1]}
  return nil unless score && methods_summary
  page = Flog::Page.new(score, average)
  methods_summary.each_line do |method_line|
    if match = method_line.match(METHOD_LINE_REGEX)
     page.scanned_methods << ScannedMethod.new(match[2], match[1])
    elsif match = method_line.match(OPERATOR_LINE_REGEX)
      return if page.scanned_methods.empty?
      page.scanned_methods.last.operators << Operator.new(match[1], match[2])
    end
  end
  page
end

#to_hObject



62
63
64
65
66
67
68
69
# File 'lib/generators/flog.rb', line 62

def to_h
  number_of_methods = @pages.inject(0) {|count, page| count += page.scanned_methods.size}
  total_flog_score = @pages.inject(0) {|total, page| total += page.score}
  sorted_pages = @pages.sort_by {|page| page.highest_score }.reverse 
  {:flog => { :total => total_flog_score,
              :average => average_score(total_flog_score, number_of_methods),
              :pages => sorted_pages.map {|page| page.to_h}}}
end