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

Instance Method Summary collapse

Methods inherited from Generator

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

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

Instance Method Details

#analyzeObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/generators/flog.rb', line 45

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/generators/flog.rb', line 10

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

#flog_resultsObject



65
66
67
# File 'lib/generators/flog.rb', line 65

def flog_results
  Dir.glob("#{metric_directory}/**/*.txt")
end

#parse(text) ⇒ Object



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

def parse(text)
  summary, methods_summary = text.split "\n\n"
  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



56
57
58
59
60
61
62
63
# File 'lib/generators/flog.rb', line 56

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.score }.reverse 
  {:flog => { :total => total_flog_score,
              :average => round_to_tenths(total_flog_score/number_of_methods),
              :pages => sorted_pages.map {|page| page.to_h}}}
end