Class: MetricFu::Saikuro

Inherits:
Generator show all
Includes:
Rake::DSL
Defined in:
lib/generators/saikuro.rb

Defined Under Namespace

Classes: ParsingElement, SFile

Instance Attribute Summary

Attributes inherited from Generator

#report, #template

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 Method Details

#analyzeObject



28
29
30
31
32
# File 'lib/generators/saikuro.rb', line 28

def analyze
  @files = sort_files(assemble_files)
  @classes = sort_classes(assemble_classes(@files))
  @meths = sort_methods(assemble_methods(@files))
end

#emitObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/generators/saikuro.rb', line 6

def emit
  options_string = MetricFu.saikuro.inject("") do |options, option|
    option[0] == :input_directory ? options : options + "--#{option.join(' ')} "
  end

  MetricFu.saikuro[:input_directory].each do |input_dir|
    options_string += "--input_directory #{input_dir} "
  end

  saikuro_bin= $:.map{|d| d+'/../bin/saikuro'}.select{|f| File.exists? f}.first || 'saikuro'
  sh %{#{saikuro_bin} #{options_string}} do |ok, response|
    unless ok
      puts "Saikuro failed with exit status: #{response.exitstatus}"
    end
  end
end

#format_directoriesObject



23
24
25
26
# File 'lib/generators/saikuro.rb', line 23

def format_directories
  dirs = MetricFu.saikuro[:input_directory].join(" | ")
  "\"#{dirs}\""
end

#per_file_info(out) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/generators/saikuro.rb', line 52

def per_file_info(out)
  @saikuro_data[:files].each do |file_data|
    next if File.extname(file_data[:filename]) == '.erb' || !File.exists?(file_data[:filename])
    begin
      line_numbers = MetricFu::LineNumbers.new(File.open(file_data[:filename], 'r').read)
    rescue StandardError => e
      raise e unless e.message =~ /you shouldn't be able to get here/
      puts "ruby_parser blew up while trying to parse #{file_path}. You won't have method level Saikuro information for this file."
      next
    end

    out[file_data[:filename]] ||= {}
    file_data[:classes].each do |class_data|
      class_data[:methods].each do |method_data|
        line = line_numbers.start_line_for_method(method_data[:name])
        out[file_data[:filename]][line.to_s] ||= []
        out[file_data[:filename]][line.to_s] << {:type => :saikuro,
                                                  :description => "Complexity #{method_data[:complexity]}"}
      end
    end
  end
end

#to_hObject



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

def to_h
  files = @files.map do |file|
    my_file = file.to_h

    f = file.filepath
    f.gsub!(%r{^#{metric_directory}/}, '')
    f << "/#{file.filename}"

    my_file[:filename] = f
    my_file
  end
  @saikuro_data = {:files => files,
                   :classes => @classes.map {|c| c.to_h},
                   :methods => @meths.map {|m| m.to_h}
                  }
  {:saikuro => @saikuro_data}
end