goldstar-metric_fu 1.5.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. data/HISTORY +208 -0
  2. data/MIT-LICENSE +22 -0
  3. data/README +28 -0
  4. data/Rakefile +16 -0
  5. data/TODO +9 -0
  6. data/lib/base/base_template.rb +151 -0
  7. data/lib/base/configuration.rb +190 -0
  8. data/lib/base/generator.rb +162 -0
  9. data/lib/base/graph.rb +39 -0
  10. data/lib/base/md5_tracker.rb +52 -0
  11. data/lib/base/report.rb +100 -0
  12. data/lib/generators/churn.rb +28 -0
  13. data/lib/generators/flay.rb +31 -0
  14. data/lib/generators/flog.rb +98 -0
  15. data/lib/generators/rails_best_practices.rb +25 -0
  16. data/lib/generators/rcov.rb +85 -0
  17. data/lib/generators/reek.rb +60 -0
  18. data/lib/generators/roodi.rb +27 -0
  19. data/lib/generators/saikuro.rb +224 -0
  20. data/lib/generators/stats.rb +59 -0
  21. data/lib/graphs/engines/bluff.rb +113 -0
  22. data/lib/graphs/engines/gchart.rb +157 -0
  23. data/lib/graphs/flay_grapher.rb +18 -0
  24. data/lib/graphs/flog_grapher.rb +57 -0
  25. data/lib/graphs/grapher.rb +11 -0
  26. data/lib/graphs/rails_best_practices_grapher.rb +19 -0
  27. data/lib/graphs/rcov_grapher.rb +18 -0
  28. data/lib/graphs/reek_grapher.rb +30 -0
  29. data/lib/graphs/roodi_grapher.rb +18 -0
  30. data/lib/graphs/stats_grapher.rb +20 -0
  31. data/lib/metric_fu.rb +37 -0
  32. data/lib/templates/awesome/awesome_template.rb +37 -0
  33. data/lib/templates/awesome/churn.html.erb +58 -0
  34. data/lib/templates/awesome/css/buttons.css +82 -0
  35. data/lib/templates/awesome/css/default.css +91 -0
  36. data/lib/templates/awesome/css/integrity.css +335 -0
  37. data/lib/templates/awesome/css/reset.css +7 -0
  38. data/lib/templates/awesome/flay.html.erb +34 -0
  39. data/lib/templates/awesome/flog.html.erb +55 -0
  40. data/lib/templates/awesome/index.html.erb +31 -0
  41. data/lib/templates/awesome/layout.html.erb +30 -0
  42. data/lib/templates/awesome/rails_best_practices.html.erb +27 -0
  43. data/lib/templates/awesome/rcov.html.erb +42 -0
  44. data/lib/templates/awesome/reek.html.erb +40 -0
  45. data/lib/templates/awesome/roodi.html.erb +27 -0
  46. data/lib/templates/awesome/saikuro.html.erb +71 -0
  47. data/lib/templates/awesome/stats.html.erb +51 -0
  48. data/lib/templates/javascripts/bluff-min.js +1 -0
  49. data/lib/templates/javascripts/excanvas.js +35 -0
  50. data/lib/templates/javascripts/js-class.js +1 -0
  51. data/lib/templates/standard/churn.html.erb +31 -0
  52. data/lib/templates/standard/default.css +64 -0
  53. data/lib/templates/standard/flay.html.erb +34 -0
  54. data/lib/templates/standard/flog.html.erb +57 -0
  55. data/lib/templates/standard/index.html.erb +41 -0
  56. data/lib/templates/standard/rails_best_practices.html.erb +29 -0
  57. data/lib/templates/standard/rcov.html.erb +43 -0
  58. data/lib/templates/standard/reek.html.erb +42 -0
  59. data/lib/templates/standard/roodi.html.erb +29 -0
  60. data/lib/templates/standard/saikuro.html.erb +84 -0
  61. data/lib/templates/standard/standard_template.rb +26 -0
  62. data/lib/templates/standard/stats.html.erb +55 -0
  63. data/spec/base/base_template_spec.rb +161 -0
  64. data/spec/base/configuration_spec.rb +276 -0
  65. data/spec/base/generator_spec.rb +223 -0
  66. data/spec/base/graph_spec.rb +24 -0
  67. data/spec/base/md5_tracker_spec.rb +57 -0
  68. data/spec/base/report_spec.rb +139 -0
  69. data/spec/generators/churn_spec.rb +41 -0
  70. data/spec/generators/flay_spec.rb +105 -0
  71. data/spec/generators/flog_spec.rb +70 -0
  72. data/spec/generators/reek_spec.rb +134 -0
  73. data/spec/generators/saikuro_spec.rb +58 -0
  74. data/spec/generators/stats_spec.rb +74 -0
  75. data/spec/graphs/engines/bluff_spec.rb +19 -0
  76. data/spec/graphs/engines/gchart_spec.rb +156 -0
  77. data/spec/graphs/flay_grapher_spec.rb +56 -0
  78. data/spec/graphs/flog_grapher_spec.rb +108 -0
  79. data/spec/graphs/rcov_grapher_spec.rb +56 -0
  80. data/spec/graphs/reek_grapher_spec.rb +65 -0
  81. data/spec/graphs/roodi_grapher_spec.rb +56 -0
  82. data/spec/resources/saikuro/app/controllers/sessions_controller.rb_cyclo.html +10 -0
  83. data/spec/resources/saikuro/app/controllers/users_controller.rb_cyclo.html +16 -0
  84. data/spec/resources/saikuro/index_cyclo.html +155 -0
  85. data/spec/resources/saikuro_sfiles/thing.rb_cyclo.html +11 -0
  86. data/spec/resources/yml/20090630.yml +7922 -0
  87. data/spec/spec.opts +6 -0
  88. data/spec/spec_helper.rb +7 -0
  89. data/tasks/metric_fu.rake +22 -0
  90. metadata +396 -0
@@ -0,0 +1,162 @@
1
+ module MetricFu
2
+
3
+ # = Generator
4
+ #
5
+ # The Generator class is an abstract class that provides the
6
+ # skeleton for producing different types of metrics.
7
+ #
8
+ # It drives the production of the metrics through a template
9
+ # method - #generate_report(options={}). This method calls
10
+ # #emit, #analyze and #to_h in order to produce the metrics.
11
+ #
12
+ # To implement a concrete class to generate a metric, therefore,
13
+ # the class must implement those three methods.
14
+ #
15
+ # * #emit should take care of running the metric tool and
16
+ # gathering its output.
17
+ # * #analyze should take care of manipulating the output from
18
+ # #emit and making it possible to store it in a programmatic way.
19
+ # * #to_h should provide a hash representation of the output from
20
+ # #analyze ready to be serialized into yaml at some point.
21
+ #
22
+ # == Pre-conditions
23
+ #
24
+ # Based on the class name of the concrete class implementing a
25
+ # Generator, the Generator class will create a 'metric_directory'
26
+ # named after the class under the MetricFu.scratch_directory, where
27
+ # any output from the #emit method should go.
28
+ #
29
+ # It will also create the MetricFu.output_directory if neccessary, and
30
+ # in general setup the directory structure that the MetricFu system
31
+ # expects.
32
+ class Generator
33
+ attr_reader :report, :template
34
+
35
+ def initialize(options={})
36
+ create_metric_dir_if_missing
37
+ create_output_dir_if_missing
38
+ create_data_dir_if_missing
39
+ end
40
+
41
+ # Creates a new generator and returns the output of the
42
+ # #generate_report method. This is the typical way to
43
+ # generate a new MetricFu report. For more information see
44
+ # the #generate_report instance method.
45
+ #
46
+ # @params options Hash
47
+ # A currently unused hash to configure the Generator
48
+ #
49
+ # @see generate_report
50
+ def self.generate_report(options={})
51
+ generator = self.new(options)
52
+ generator.generate_report
53
+ end
54
+
55
+ # Provides the unqualified class name of an implemented concrete
56
+ # class, as a string. For example:
57
+ #
58
+ # class Flay < Generator; end
59
+ # klass = Flay.new
60
+ # klass.class_name
61
+ # > "flay"
62
+ #
63
+ # @return String
64
+ # The unqualified class name of this concrete class, returned
65
+ # as a string.
66
+ def self.class_name
67
+ self.to_s.split('::').last.downcase
68
+ end
69
+
70
+ # Returns the directory where the Generator will write any output
71
+ def self.metric_directory
72
+ File.join(MetricFu.scratch_directory, class_name)
73
+ end
74
+
75
+ def create_metric_dir_if_missing #:nodoc:
76
+ unless File.directory?(metric_directory)
77
+ FileUtils.mkdir_p(metric_directory, :verbose => false)
78
+ end
79
+ end
80
+
81
+ def create_output_dir_if_missing #:nodoc:
82
+ unless File.directory?(MetricFu.output_directory)
83
+ FileUtils.mkdir_p(MetricFu.output_directory, :verbose => false)
84
+ end
85
+ end
86
+
87
+ def create_data_dir_if_missing #:nodoc:
88
+ unless File.directory?(MetricFu.data_directory)
89
+ FileUtils.mkdir_p(MetricFu.data_directory, :verbose => false)
90
+ end
91
+ end
92
+
93
+ # @return String
94
+ # The path of the metric directory this class is using.
95
+ def metric_directory
96
+ self.class.metric_directory
97
+ end
98
+
99
+ def remove_excluded_files(paths, globs_to_remove = MetricFu.file_globs_to_ignore)
100
+ files_to_remove = []
101
+ globs_to_remove.each do |glob|
102
+ files_to_remove.concat(Dir[glob])
103
+ end
104
+ paths - files_to_remove
105
+ end
106
+
107
+ # Defines some hook methods for the concrete classes to hook into.
108
+ %w[emit analyze].each do |meth|
109
+ define_method("before_#{meth}".to_sym) {}
110
+ define_method("after_#{meth}".to_sym) {}
111
+ end
112
+ define_method("before_to_h".to_sym) {}
113
+
114
+ # Provides a template method to drive the production of a metric
115
+ # from a concrete implementation of this class. Each concrete
116
+ # class must implement the three methods that this template method
117
+ # calls: #emit, #analyze and #to_h. For more details, see the
118
+ # class documentation.
119
+ #
120
+ # This template method also calls before_emit, after_emit... etc.
121
+ # methods to allow extra hooks into the processing methods, and help
122
+ # to keep the logic of your Generators clean.
123
+ def generate_report
124
+ %w[emit analyze].each do |meth|
125
+ send("before_#{meth}".to_sym)
126
+ send("#{meth}".to_sym)
127
+ send("after_#{meth}".to_sym)
128
+ end
129
+ before_to_h()
130
+ to_h()
131
+ end
132
+
133
+ def round_to_tenths(decimal)
134
+ decimal = 0.0 if decimal.to_s.eql?('NaN')
135
+ (decimal * 10).round / 10.0
136
+ end
137
+
138
+ def emit #:nodoc:
139
+ raise <<-EOF
140
+ This method must be implemented by a concrete class descending
141
+ from Generator. See generator class documentation for more
142
+ information.
143
+ EOF
144
+ end
145
+
146
+ def analyze #:nodoc:
147
+ raise <<-EOF
148
+ This method must be implemented by a concrete class descending
149
+ from Generator. See generator class documentation for more
150
+ information.
151
+ EOF
152
+ end
153
+
154
+ def to_graph #:nodoc:
155
+ raise <<-EOF
156
+ This method must be implemented by a concrete class descending
157
+ from Generator. See generator class documentation for more
158
+ information.
159
+ EOF
160
+ end
161
+ end
162
+ end
data/lib/base/graph.rb ADDED
@@ -0,0 +1,39 @@
1
+ module MetricFu
2
+
3
+ def self.graph
4
+ @graph ||= Graph.new
5
+ end
6
+
7
+ class Graph
8
+
9
+ attr_accessor :clazz
10
+
11
+ def initialize
12
+ self.clazz = []
13
+ end
14
+
15
+ def add(graph_type, graph_engine)
16
+ grapher_name = graph_type.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } + graph_engine.to_s.capitalize + "Grapher"
17
+ self.clazz.push MetricFu.const_get(grapher_name).new
18
+ end
19
+
20
+
21
+ def generate
22
+ return if self.clazz.empty?
23
+ puts "Generating graphs"
24
+ Dir[File.join(MetricFu.data_directory, '*.yml')].sort.each do |metric_file|
25
+ puts "Generating graphs for #{metric_file}"
26
+ date = metric_file.split('/')[3].split('.')[0]
27
+ y, m, d = date[0..3].to_i, date[4..5].to_i, date[6..7].to_i
28
+ metrics = YAML::load(File.open(metric_file))
29
+
30
+ self.clazz.each do |grapher|
31
+ grapher.get_metrics(metrics, "#{m}/#{d}")
32
+ end
33
+ end
34
+ self.clazz.each do |grapher|
35
+ grapher.graph!
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,52 @@
1
+ require 'digest/md5'
2
+ require 'fileutils'
3
+
4
+ module MetricFu
5
+ class MD5Tracker
6
+
7
+ @@unchanged_md5s = []
8
+
9
+ class << self
10
+ def md5_dir(path_to_file, base_dir)
11
+ File.join(base_dir,
12
+ path_to_file.split('/')[0..-2].join('/'))
13
+ end
14
+
15
+ def md5_file(path_to_file, base_dir)
16
+ File.join(md5_dir(path_to_file, base_dir),
17
+ path_to_file.split('/').last.sub(/\.[a-z]+/, '.md5'))
18
+ end
19
+
20
+ def track(path_to_file, base_dir)
21
+ md5 = Digest::MD5.hexdigest(File.read(path_to_file))
22
+ FileUtils.mkdir_p(md5_dir(path_to_file, base_dir), :verbose => false)
23
+ f = File.new(md5_file(path_to_file, base_dir), "w")
24
+ f.puts(md5)
25
+ f.close
26
+ md5
27
+ end
28
+
29
+ def file_changed?(path_to_file, base_dir)
30
+ orig_md5_file = md5_file(path_to_file, base_dir)
31
+ return !!track(path_to_file, base_dir) unless File.exist?(orig_md5_file)
32
+
33
+ current_md5 = ""
34
+ file = File.open(orig_md5_file, 'r')
35
+ file.each_line { |line| current_md5 << line }
36
+ file.close
37
+ current_md5.chomp!
38
+
39
+ new_md5 = Digest::MD5.hexdigest(File.read(path_to_file))
40
+ new_md5.chomp!
41
+
42
+ @@unchanged_md5s << path_to_file if new_md5 == current_md5
43
+
44
+ return new_md5 != current_md5
45
+ end
46
+
47
+ def file_already_counted?(path_to_file)
48
+ return @@unchanged_md5s.include?(path_to_file)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,100 @@
1
+ module MetricFu
2
+
3
+ # MetricFu.report memoizes access to a Report object, that will be
4
+ # used throughout the lifecycle of the MetricFu app.
5
+ def self.report
6
+ @report ||= Report.new
7
+ end
8
+
9
+ # = Report
10
+ #
11
+ # The Report class is responsible two things:
12
+ #
13
+ # It adds information to the yaml report, produced by the system
14
+ # as a whole, for each of the generators used in this test run.
15
+ #
16
+ # It also handles passing the information from each generator used
17
+ # in this test run out to the template class set in
18
+ # MetricFu::Configuration.
19
+ class Report
20
+
21
+ # Renders the result of the report_hash into a yaml serialization
22
+ # ready for writing out to a file.
23
+ #
24
+ # @return YAML
25
+ # A YAML object containing the results of the report generation
26
+ # process
27
+ def to_yaml
28
+ report_hash.to_yaml
29
+ end
30
+
31
+
32
+ def report_hash #:nodoc:
33
+ @report_hash ||= {}
34
+ end
35
+
36
+ # Instantiates a new template class based on the configuration set
37
+ # in MetricFu::Configuration, or through the MetricFu.config block
38
+ # in your rake file (defaults to the included AwesomeTemplate),
39
+ # assigns the report_hash to the report_hash in the template, and
40
+ # tells the template to to write itself out.
41
+ def save_templatized_report
42
+ @template = MetricFu.template_class.new
43
+ @template.report = report_hash
44
+ @template.write
45
+ end
46
+
47
+ # Adds a hash from a passed report, produced by one of the Generator
48
+ # classes to the aggregate report_hash managed by this hash.
49
+ #
50
+ # @param report_type Hash
51
+ # The hash to add to the aggregate report_hash
52
+ def add(report_type)
53
+ clazz = MetricFu.const_get(report_type.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase })
54
+ report_hash.merge!(clazz.generate_report)
55
+ end
56
+
57
+ # Saves the passed in content to the passed in directory. If
58
+ # a filename is passed in it will be used as the name of the
59
+ # file, otherwise it will default to 'index.html'
60
+ #
61
+ # @param content String
62
+ # A string containing the content (usually html) to be written
63
+ # to the file.
64
+ #
65
+ # @param dir String
66
+ # A dir containing the path to the directory to write the file in.
67
+ #
68
+ # @param file String
69
+ # A filename to save the path as. Defaults to 'index.html'.
70
+ #
71
+ def save_output(content, dir, file='index.html')
72
+ open("#{dir}/#{file}", "w") do |f|
73
+ f.puts content
74
+ end
75
+ end
76
+
77
+ # Checks to discover whether we should try and open the results
78
+ # of the report in the browser on this system. We only try and open
79
+ # in the browser if we're on OS X and we're not running in a
80
+ # CruiseControl.rb environment. See MetricFu.configuration for more
81
+ # details about how we make those guesses.
82
+ #
83
+ # @return Boolean
84
+ # Should we open in the browser or not?
85
+ def open_in_browser?
86
+ MetricFu.configuration.platform.include?('darwin') &&
87
+ ! MetricFu.configuration.is_cruise_control_rb?
88
+ end
89
+
90
+ # Shows 'index.html' from the passed directory in the browser
91
+ # if we're able to open the browser on this platform.
92
+ #
93
+ # @param dir String
94
+ # The directory path where the 'index.html' we want to open is
95
+ # stored
96
+ def show_in_browser(dir)
97
+ system("open #{dir}/index.html") if open_in_browser?
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,28 @@
1
+ module MetricFu
2
+
3
+ class Churn < Generator
4
+
5
+ def initialize(options={})
6
+ super
7
+ end
8
+
9
+ def emit
10
+ @output = `churn --yaml`
11
+ yaml_start = @output.index("---")
12
+ @output = @output[yaml_start...@output.length] if yaml_start
13
+ end
14
+
15
+ def analyze
16
+ if @output.match(/fatal: Not a git repository/)
17
+ @churn = [:churn => {}]
18
+ else
19
+ @churn = YAML::load(@output)
20
+ end
21
+ end
22
+
23
+ def to_h
24
+ {:churn => @churn[:churn]}
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,31 @@
1
+ require 'flay'
2
+
3
+ module MetricFu
4
+
5
+ class Flay < Generator
6
+
7
+ def emit
8
+ mimimum_score_parameter = MetricFu.flay[:minimum_score] ? "--mass #{MetricFu.flay[:minimum_score]} " : ""
9
+
10
+ @output = `flay #{mimimum_score_parameter} #{MetricFu.flay[:dirs_to_flay].join(" ")}`
11
+ end
12
+
13
+ def analyze
14
+ @matches = @output.chomp.split("\n\n").map{|m| m.split("\n ") }
15
+ end
16
+
17
+ def to_h
18
+ target = []
19
+ total_score = @matches.shift.first.split('=').last.strip
20
+ @matches.each do |problem|
21
+ reason = problem.shift.strip
22
+ lines_info = problem.map do |full_line|
23
+ name, line = full_line.split(":")
24
+ {:name => name.strip, :line => line.strip}
25
+ end
26
+ target << [:reason => reason, :matches => lines_info]
27
+ end
28
+ {:flay => {:total_score => total_score, :matches => target.flatten}}
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,98 @@
1
+ require 'pathname'
2
+ require 'optparse'
3
+ require 'flog'
4
+
5
+ module MetricFu
6
+
7
+ class Flog < Generator
8
+
9
+ def emit
10
+ files = []
11
+ MetricFu.flog[:dirs_to_flog].each do |directory|
12
+ directory = "." if directory=='./'
13
+ dir_files = Dir.glob("#{directory}/**/*.rb")
14
+ dir_files = remove_excluded_files(dir_files)
15
+ files += dir_files
16
+ end
17
+ options = ::Flog.parse_options ["--all", "--details"]
18
+
19
+ @flogger = ::Flog.new options
20
+ @flogger.flog files
21
+
22
+ rescue LoadError
23
+ if RUBY_PLATFORM =~ /java/
24
+ puts 'running in jruby - flog tasks not available'
25
+ end
26
+ end
27
+
28
+ def analyze
29
+ @method_containers = {}
30
+ @flogger.calls.each do |full_method_name, operators|
31
+ container_name = full_method_name.split('#').first
32
+ path = @flogger.method_locations[full_method_name]
33
+ if @method_containers[container_name]
34
+ @method_containers[container_name].add_method(full_method_name, operators, @flogger.totals[full_method_name], path)
35
+ @method_containers[container_name].add_path(path)
36
+ else
37
+ mc = MethodContainer.new(container_name, path)
38
+ mc.add_method(full_method_name, operators, @flogger.totals[full_method_name], path)
39
+ @method_containers[container_name] = mc
40
+ end
41
+ end
42
+ end
43
+
44
+ def to_h
45
+ sorted_containers = @method_containers.values.sort_by {|c| c.highest_score}.reverse
46
+ {:flog => { :total => @flogger.total,
47
+ :average => @flogger.average,
48
+ :method_containers => sorted_containers.map {|method_container| method_container.to_h}}}
49
+ end
50
+
51
+ end
52
+
53
+ class MethodContainer
54
+ attr_reader :methods
55
+
56
+ def initialize(name, path)
57
+ @name = name
58
+ add_path path
59
+ @methods = {}
60
+ end
61
+
62
+ def add_path path
63
+ return unless path
64
+ @path ||= path.split(':').first
65
+ end
66
+
67
+ def add_method(full_method_name, operators, score, path)
68
+ @methods[full_method_name] = {:operators => operators, :score => score, :path => path}
69
+ end
70
+
71
+ def to_h
72
+ { :name => @name,
73
+ :path => @path || '',
74
+ :total_score => total_score,
75
+ :highest_score => highest_score,
76
+ :average_score => average_score,
77
+ :methods => @methods}
78
+ end
79
+
80
+ def highest_score
81
+ method_scores.max
82
+ end
83
+
84
+ private
85
+
86
+ def method_scores
87
+ @method_scores ||= @methods.values.map {|v| v[:score] }
88
+ end
89
+
90
+ def total_score
91
+ @total_score ||= method_scores.inject(0) {|sum, score| sum += score}
92
+ end
93
+
94
+ def average_score
95
+ total_score / method_scores.size.to_f
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,25 @@
1
+ module MetricFu
2
+ class RailsBestPractices < Generator
3
+
4
+ def emit
5
+ @output = `rails_best_practices .`
6
+ end
7
+
8
+ def analyze
9
+ @matches = @output.chomp.split("\n").map{|m| m.split(" - ") }
10
+ total = @matches.pop
11
+ 2.times { @matches.pop } # ignore wiki link
12
+ @matches.reject! {|array| array.empty? }
13
+ @matches.map! do |match|
14
+ file, line = match[0].split(':')
15
+ problem = match[1]
16
+ {:file => file, :line => line, :problem => problem}
17
+ end
18
+ @rails_best_practices_results = {:total => total, :problems => @matches}
19
+ end
20
+
21
+ def to_h
22
+ {:rails_best_practices => @rails_best_practices_results}
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,85 @@
1
+ require 'enumerator'
2
+
3
+ module MetricFu
4
+
5
+ class Rcov < Generator
6
+ NEW_FILE_MARKER = ("=" * 80) + "\n"
7
+
8
+ class Line
9
+ attr_accessor :content, :was_run
10
+
11
+ def initialize(content, was_run)
12
+ @content = content
13
+ @was_run = was_run
14
+ end
15
+
16
+ def to_h
17
+ {:content => @content, :was_run => @was_run}
18
+ end
19
+ end
20
+
21
+ def emit
22
+ unless MetricFu.rcov[:external]
23
+ FileUtils.rm_rf(MetricFu::Rcov.metric_directory, :verbose => false)
24
+ Dir.mkdir(MetricFu::Rcov.metric_directory)
25
+ test_files = FileList[*MetricFu.rcov[:test_files]].join(' ')
26
+ rcov_opts = MetricFu.rcov[:rcov_opts].join(' ')
27
+ output = ">> #{MetricFu::Rcov.metric_directory}/rcov.txt"
28
+ puts "** Running the specs/tests in the [#{MetricFu.rcov[:environment]}] environment"
29
+ `RAILS_ENV=#{MetricFu.rcov[:environment]} rcov #{test_files} #{rcov_opts} #{output}`
30
+ end
31
+ end
32
+
33
+
34
+ def analyze
35
+ output_file = MetricFu.rcov[:external] ? MetricFu.rcov[:external] : MetricFu::Rcov.metric_directory + '/rcov.txt'
36
+ output = File.open(output_file).read
37
+ output = output.split(NEW_FILE_MARKER)
38
+
39
+ output.shift # Throw away the first entry - it's the execution time etc.
40
+
41
+ files = assemble_files(output)
42
+
43
+ @global_total_lines = 0
44
+ @global_total_lines_run = 0
45
+
46
+ @rcov = add_coverage_percentage(files)
47
+ end
48
+
49
+ def to_h
50
+ global_percent_run = ((@global_total_lines_run.to_f / @global_total_lines.to_f) * 100)
51
+ {:rcov => @rcov.merge({:global_percent_run => round_to_tenths(global_percent_run) })}
52
+ end
53
+
54
+ private
55
+
56
+ def assemble_files(output)
57
+ files = {}
58
+ output.each_slice(2) {|out| files[out.first.strip] = out.last}
59
+ files.each_pair {|fname, content| files[fname] = content.split("\n") }
60
+ files.each_pair do |fname, content|
61
+ content.map! do |raw_line|
62
+ if raw_line.match(/^!!/)
63
+ line = Line.new(raw_line.gsub('!!', ' '), false).to_h
64
+ else
65
+ line = Line.new(raw_line, true).to_h
66
+ end
67
+ end
68
+ content.reject! {|line| line[:content].blank? }
69
+ files[fname] = {:lines => content}
70
+ end
71
+ files
72
+ end
73
+
74
+ def add_coverage_percentage(files)
75
+ files.each_pair do |fname, content|
76
+ lines = content[:lines]
77
+ @global_total_lines_run += lines_run = lines.find_all {|line| line[:was_run] == true }.length
78
+ @global_total_lines += total_lines = lines.length
79
+ percent_run = ((lines_run.to_f / total_lines.to_f) * 100).round
80
+ files[fname][:percent_run] = percent_run
81
+ end
82
+ end
83
+
84
+ end
85
+ end
@@ -0,0 +1,60 @@
1
+ module MetricFu
2
+
3
+ class Reek < Generator
4
+ REEK_REGEX = /^(\S+) (.*) \((.*)\)$/
5
+
6
+ def emit
7
+ files_to_reek = MetricFu.reek[:dirs_to_reek].map{|dir| Dir[File.join(dir, "**/*.rb")] }
8
+ files = remove_excluded_files(files_to_reek.flatten)
9
+ config_file_param = MetricFu.reek[:config_file_pattern] ? "--config #{MetricFu.reek[:config_file_pattern]}" : ''
10
+ @output = `reek #{config_file_param} #{files.join(" ")}`
11
+ @output = massage_for_reek_12 if reek_12?
12
+ end
13
+
14
+ def reek_12?
15
+ return false if @output.length == 0
16
+ (@output =~ /^"/) != 0
17
+ end
18
+
19
+ def massage_for_reek_12
20
+ section_break = ''
21
+ @output.split("\n").map do |line|
22
+ case line
23
+ when /^ /
24
+ "#{line.gsub(/^ /, '')}\n"
25
+ else
26
+ parts = line.split(" -- ")
27
+ if parts[1].nil?
28
+ "#{line}\n"
29
+ else
30
+ warnings = parts[1].gsub(/ \(.*\):/, ':')
31
+ result = "#{section_break}\"#{parts[0]}\" -- #{warnings}\n"
32
+ section_break = "\n"
33
+ result
34
+ end
35
+ end
36
+ end.join
37
+ end
38
+
39
+ def analyze
40
+ @matches = @output.chomp.split("\n\n").map{|m| m.split("\n") }
41
+ @matches = @matches.map do |match|
42
+ file_path = match.shift.split('--').first
43
+ file_path = file_path.gsub('"', ' ').strip
44
+ code_smells = match.map do |smell|
45
+ match_object = smell.match(REEK_REGEX)
46
+ next unless match_object
47
+ {:method => match_object[1].strip,
48
+ :message => match_object[2].strip,
49
+ :type => match_object[3].strip}
50
+ end.compact
51
+ {:file_path => file_path, :code_smells => code_smells}
52
+ end
53
+ end
54
+
55
+ def to_h
56
+ {:reek => {:matches => @matches}}
57
+ end
58
+
59
+ end
60
+ end