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 =
/\s*(\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, #remove_excluded_files, #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.



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

def pages
  @pages
end

Class Method Details

.verify_dependencies!Object



8
9
10
11
# File 'lib/generators/flog.rb', line 8

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

Instance Method Details

#analyzeObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/generators/flog.rb', line 59

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") 
      #don't include old cached flog results for files that no longer exist.
	  if is_file_current?(page.path.to_s)
        @pages << page
      end
    end
  end
end

#emitObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/generators/flog.rb', line 17

def emit
  metric_dir = MetricFu::Flog.metric_directory
  MetricFu.flog[:dirs_to_flog].each do |directory|
    directory = "." if directory=='./'
    files = Dir.glob("#{directory}/**/*.rb")
    files = remove_excluded_files(files)
    files.each do |filename|
      output_dir = "#{metric_dir}/#{filename.split("/")[0..-2].join("/")}"
      mkdir_p(output_dir, :verbose => false) unless File.directory?(output_dir)
      pathname         = Pathname.new(filename)
      if MetricFu::MD5Tracker.file_changed?(filename, metric_dir) 
		base_name = pathname.basename.to_s.gsub(/\..*$/,'.txt')
            editted_filename = File.join(pathname.dirname.to_s, base_name)
     `flog -ad #{filename} > #{metric_dir}/#{editted_filename}`
      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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/generators/flog.rb', line 42

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



73
74
75
76
77
78
79
80
# File 'lib/generators/flog.rb', line 73

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