danmayer-metric_fu 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (120) hide show
  1. data/HISTORY +237 -0
  2. data/MIT-LICENSE +22 -0
  3. data/README +29 -0
  4. data/Rakefile +18 -0
  5. data/TODO +6 -0
  6. data/lib/base/base_template.rb +172 -0
  7. data/lib/base/churn_analyzer.rb +38 -0
  8. data/lib/base/code_issue.rb +97 -0
  9. data/lib/base/configuration.rb +199 -0
  10. data/lib/base/flay_analyzer.rb +50 -0
  11. data/lib/base/flog_analyzer.rb +43 -0
  12. data/lib/base/generator.rb +166 -0
  13. data/lib/base/graph.rb +44 -0
  14. data/lib/base/line_numbers.rb +74 -0
  15. data/lib/base/location.rb +85 -0
  16. data/lib/base/md5_tracker.rb +52 -0
  17. data/lib/base/metric_analyzer.rb +404 -0
  18. data/lib/base/ranking.rb +34 -0
  19. data/lib/base/rcov_analyzer.rb +43 -0
  20. data/lib/base/reek_analyzer.rb +163 -0
  21. data/lib/base/report.rb +108 -0
  22. data/lib/base/roodi_analyzer.rb +37 -0
  23. data/lib/base/saikuro_analyzer.rb +48 -0
  24. data/lib/base/scoring_strategies.rb +29 -0
  25. data/lib/base/stats_analyzer.rb +37 -0
  26. data/lib/base/table.rb +102 -0
  27. data/lib/generators/churn.rb +28 -0
  28. data/lib/generators/flay.rb +31 -0
  29. data/lib/generators/flog.rb +111 -0
  30. data/lib/generators/hotspots.rb +52 -0
  31. data/lib/generators/rails_best_practices.rb +53 -0
  32. data/lib/generators/rcov.rb +122 -0
  33. data/lib/generators/reek.rb +81 -0
  34. data/lib/generators/roodi.rb +35 -0
  35. data/lib/generators/saikuro.rb +256 -0
  36. data/lib/generators/stats.rb +58 -0
  37. data/lib/graphs/engines/bluff.rb +113 -0
  38. data/lib/graphs/engines/gchart.rb +157 -0
  39. data/lib/graphs/flay_grapher.rb +18 -0
  40. data/lib/graphs/flog_grapher.rb +57 -0
  41. data/lib/graphs/grapher.rb +11 -0
  42. data/lib/graphs/rails_best_practices_grapher.rb +19 -0
  43. data/lib/graphs/rcov_grapher.rb +18 -0
  44. data/lib/graphs/reek_grapher.rb +30 -0
  45. data/lib/graphs/roodi_grapher.rb +18 -0
  46. data/lib/graphs/stats_grapher.rb +20 -0
  47. data/lib/metric_fu.rb +40 -0
  48. data/lib/templates/awesome/awesome_template.rb +73 -0
  49. data/lib/templates/awesome/churn.html.erb +58 -0
  50. data/lib/templates/awesome/css/buttons.css +82 -0
  51. data/lib/templates/awesome/css/default.css +91 -0
  52. data/lib/templates/awesome/css/integrity.css +334 -0
  53. data/lib/templates/awesome/css/reset.css +7 -0
  54. data/lib/templates/awesome/css/syntax.css +19 -0
  55. data/lib/templates/awesome/flay.html.erb +34 -0
  56. data/lib/templates/awesome/flog.html.erb +55 -0
  57. data/lib/templates/awesome/hotspots.html.erb +62 -0
  58. data/lib/templates/awesome/index.html.erb +34 -0
  59. data/lib/templates/awesome/layout.html.erb +30 -0
  60. data/lib/templates/awesome/rails_best_practices.html.erb +27 -0
  61. data/lib/templates/awesome/rcov.html.erb +42 -0
  62. data/lib/templates/awesome/reek.html.erb +40 -0
  63. data/lib/templates/awesome/roodi.html.erb +27 -0
  64. data/lib/templates/awesome/saikuro.html.erb +71 -0
  65. data/lib/templates/awesome/stats.html.erb +51 -0
  66. data/lib/templates/javascripts/bluff-min.js +1 -0
  67. data/lib/templates/javascripts/excanvas.js +35 -0
  68. data/lib/templates/javascripts/js-class.js +1 -0
  69. data/lib/templates/standard/churn.html.erb +31 -0
  70. data/lib/templates/standard/default.css +64 -0
  71. data/lib/templates/standard/flay.html.erb +34 -0
  72. data/lib/templates/standard/flog.html.erb +57 -0
  73. data/lib/templates/standard/hotspots.html.erb +54 -0
  74. data/lib/templates/standard/index.html.erb +41 -0
  75. data/lib/templates/standard/rails_best_practices.html.erb +29 -0
  76. data/lib/templates/standard/rcov.html.erb +43 -0
  77. data/lib/templates/standard/reek.html.erb +42 -0
  78. data/lib/templates/standard/roodi.html.erb +29 -0
  79. data/lib/templates/standard/saikuro.html.erb +84 -0
  80. data/lib/templates/standard/standard_template.rb +26 -0
  81. data/lib/templates/standard/stats.html.erb +55 -0
  82. data/spec/base/base_template_spec.rb +177 -0
  83. data/spec/base/configuration_spec.rb +276 -0
  84. data/spec/base/generator_spec.rb +223 -0
  85. data/spec/base/graph_spec.rb +61 -0
  86. data/spec/base/line_numbers_spec.rb +62 -0
  87. data/spec/base/md5_tracker_spec.rb +57 -0
  88. data/spec/base/report_spec.rb +146 -0
  89. data/spec/generators/churn_spec.rb +41 -0
  90. data/spec/generators/flay_spec.rb +105 -0
  91. data/spec/generators/flog_spec.rb +70 -0
  92. data/spec/generators/rails_best_practices_spec.rb +52 -0
  93. data/spec/generators/rcov_spec.rb +180 -0
  94. data/spec/generators/reek_spec.rb +134 -0
  95. data/spec/generators/roodi_spec.rb +24 -0
  96. data/spec/generators/saikuro_spec.rb +74 -0
  97. data/spec/generators/stats_spec.rb +74 -0
  98. data/spec/graphs/engines/bluff_spec.rb +19 -0
  99. data/spec/graphs/engines/gchart_spec.rb +156 -0
  100. data/spec/graphs/flay_grapher_spec.rb +56 -0
  101. data/spec/graphs/flog_grapher_spec.rb +108 -0
  102. data/spec/graphs/rails_best_practices_grapher_spec.rb +61 -0
  103. data/spec/graphs/rcov_grapher_spec.rb +56 -0
  104. data/spec/graphs/reek_grapher_spec.rb +65 -0
  105. data/spec/graphs/roodi_grapher_spec.rb +56 -0
  106. data/spec/graphs/stats_grapher_spec.rb +68 -0
  107. data/spec/resources/line_numbers/foo.rb +33 -0
  108. data/spec/resources/line_numbers/module.rb +11 -0
  109. data/spec/resources/line_numbers/module_surrounds_class.rb +15 -0
  110. data/spec/resources/line_numbers/two_classes.rb +11 -0
  111. data/spec/resources/saikuro/app/controllers/sessions_controller.rb_cyclo.html +10 -0
  112. data/spec/resources/saikuro/app/controllers/users_controller.rb_cyclo.html +16 -0
  113. data/spec/resources/saikuro/index_cyclo.html +155 -0
  114. data/spec/resources/saikuro_sfiles/thing.rb_cyclo.html +11 -0
  115. data/spec/resources/yml/20090630.yml +7922 -0
  116. data/spec/resources/yml/metric_missing.yml +1 -0
  117. data/spec/spec.opts +6 -0
  118. data/spec/spec_helper.rb +7 -0
  119. data/tasks/metric_fu.rake +22 -0
  120. metadata +462 -0
@@ -0,0 +1,38 @@
1
+ class ChurnAnalyzer
2
+ include ScoringStrategies
3
+
4
+ COLUMNS = %w{times_changed}
5
+
6
+ def columns
7
+ COLUMNS
8
+ end
9
+
10
+ def name
11
+ :churn
12
+ end
13
+
14
+ def map(row)
15
+ ScoringStrategies.present(row)
16
+ end
17
+
18
+ def reduce(scores)
19
+ ScoringStrategies.sum(scores)
20
+ end
21
+
22
+ def score(metric_ranking, item)
23
+ flat_churn_score = 0.50
24
+ metric_ranking.scored?(item) ? flat_churn_score : 0
25
+ end
26
+
27
+ def generate_records(data, table)
28
+ return if data==nil
29
+ Array(data[:changes]).each do |change|
30
+ table << {
31
+ "metric" => :churn,
32
+ "times_changed" => change[:times_changed],
33
+ "file_path" => change[:file_path]
34
+ }
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,97 @@
1
+ require 'delegate'
2
+ require MetricFu::LIB_ROOT + '/base/metric_analyzer'
3
+ require MetricFu::LIB_ROOT + '/base/flog_analyzer'
4
+ require MetricFu::LIB_ROOT + '/base/saikuro_analyzer'
5
+ require MetricFu::LIB_ROOT + '/base/churn_analyzer'
6
+ require MetricFu::LIB_ROOT + '/base/reek_analyzer'
7
+ require MetricFu::LIB_ROOT + '/base/flay_analyzer'
8
+
9
+ module CarefulArray
10
+
11
+ def carefully_remove(elements)
12
+ missing_elements = elements - self
13
+ raise "Cannot delete missing elements: #{missing_elements.inspect}" unless missing_elements.empty?
14
+ (self - elements).extend(CarefulArray)
15
+ end
16
+
17
+ end
18
+
19
+ class CodeIssue < DelegateClass(Record) #DelegateClass(Ruport::Data::Record)
20
+ include Comparable
21
+
22
+ # TODO: Yuck! 'stat_value' is a column for StatAnalyzer
23
+ EXCLUDED_COLUMNS = FlogAnalyzer::COLUMNS + SaikuroAnalyzer::COLUMNS + ['stat_value'] + ChurnAnalyzer::COLUMNS + ReekAnalyzer.new.columns.extend(CarefulArray).carefully_remove(['reek__type_name', 'reek__comparable_message']) + FlayAnalyzer.new.columns.extend(CarefulArray).carefully_remove(['flay_matching_reason'])
24
+
25
+ def <=>(other)
26
+ spaceship_for_columns(self.attributes, other)
27
+ end
28
+
29
+ def ===(other)
30
+ self.hash_for(included_columns_hash, included_columns) == other.hash_for(included_columns_hash, included_columns)
31
+ end
32
+
33
+ def spaceship_for_columns(columns, other)
34
+ columns.each do |column|
35
+ equality = self[column].to_s <=> other[column].to_s
36
+ return equality if equality!=0
37
+ end
38
+ return 0
39
+ end
40
+
41
+ def hash_for(column_hash, columns)
42
+ @hashes ||= {}
43
+ # fetch would be cleaner, but slower
44
+ if @hashes.has_key?(column_hash)
45
+ @hashes[column_hash]
46
+ else
47
+ values = columns.map {|column| self[column]}
48
+ hash_for_columns = values.join('').hash
49
+ @hashes[column_hash]=hash_for_columns
50
+ hash_for_columns
51
+ end
52
+ end
53
+
54
+ def included_columns_hash
55
+ @included_columns_hash ||= included_columns.hash
56
+ end
57
+
58
+ def included_columns
59
+ @included_columns ||= self.attributes.extend(CarefulArray).carefully_remove(EXCLUDED_COLUMNS)
60
+ end
61
+
62
+ def find_counterpart_index_in(collection)
63
+ # each_with_index is cleaner, but it is slower and we
64
+ # spend a lot of time in this loop
65
+ index = 0
66
+ collection.each do |issue|
67
+ return index if self === issue
68
+ index += 1
69
+ end
70
+ return nil
71
+ end
72
+
73
+ def modifies?(other)
74
+ case self.metric
75
+ when :reek
76
+ #return false unless ["Large Class", "Long Method", "Long Parameter List"].include?(self.reek__type_name)
77
+ return false if self.reek__type_name != other.reek__type_name
78
+ self.reek__value != other.reek__value
79
+ when :flog
80
+ self.score != other.score
81
+ when :saikuro
82
+ self.complexity != other.complexity
83
+ when :stats
84
+ self.stat_value != other.stat_value
85
+ when :churn
86
+ self.times_changed != other.times_changed
87
+ when :flay
88
+ #self.flay_reason != other.flay_reason
89
+ # do nothing for now
90
+ when :roodi, :stats
91
+ # do nothing
92
+ else
93
+ raise ArgumentError, "Invalid metric type #{self.metric}"
94
+ end
95
+ end
96
+
97
+ end
@@ -0,0 +1,199 @@
1
+ module MetricFu
2
+
3
+ # A list of metrics which are available in the MetricFu system.
4
+ #
5
+ # These are metrics which have been developed for the system. Of
6
+ # course, in order to use these metrics, their respective gems must
7
+ # be installed on the system.
8
+ AVAILABLE_METRICS = [:churn, :flog, :flay, :reek,
9
+ :roodi, :rcov,
10
+ :hotspots]
11
+
12
+ AVAILABLE_METRICS << :saikuro unless RUBY_VERSION == '1.9.2'
13
+
14
+ AVAILABLE_GRAPHS = [:flog, :flay, :reek, :roodi, :rcov, :rails_best_practices]
15
+ AVAILABLE_GRAPH_ENGINES = [:gchart, :bluff]
16
+
17
+ # The @@configuration class variable holds a global type configuration
18
+ # object for any parts of the system to use.
19
+ def self.configuration
20
+ @@configuration ||= Configuration.new
21
+ end
22
+
23
+ # = Configuration
24
+ #
25
+ # The Configuration class, as it sounds, provides methods for
26
+ # configuring the behaviour of MetricFu.
27
+ #
28
+ # == Customization for Rails
29
+ #
30
+ # The Configuration class checks for the presence of a
31
+ # 'config/environment.rb' file. If the file is present, it assumes
32
+ # it is running in a Rails project. If it is, it will:
33
+ #
34
+ # * Add 'app' to the @code_dirs directory to include the
35
+ # code in the app directory in the processing
36
+ # * Add :stats to the list of metrics to run to get the Rails stats
37
+ # task
38
+ #
39
+ # == Customization for CruiseControl.rb
40
+ #
41
+ # The Configuration class checks for the presence of a
42
+ # 'CC_BUILD_ARTIFACTS' environment variable. If it's found
43
+ # it will change the default output directory from the default
44
+ # "tmp/metric_fu to the directory represented by 'CC_BUILD_ARTIFACTS'
45
+ #
46
+ # == Deprications
47
+ #
48
+ # The Configuration class checks for several deprecated constants
49
+ # that were previously used to configure MetricFu. These include
50
+ # CHURN_OPTIONS, DIRECTORIES_TO_FLOG, SAIKURO_OPTIONS,
51
+ # and MetricFu::SAIKURO_OPTIONS.
52
+ #
53
+ # These have been replaced by config.churn, config.flog and
54
+ # config.saikuro respectively.
55
+ class Configuration
56
+
57
+ def initialize #:nodoc:#
58
+ reset
59
+ add_attr_accessors_to_self
60
+ add_class_methods_to_metric_fu
61
+ end
62
+
63
+ # Searches through the instance variables of the class and
64
+ # creates a class method on the MetricFu module to read the value
65
+ # of the instance variable from the Configuration class.
66
+ def add_class_methods_to_metric_fu
67
+ instance_variables.each do |name|
68
+ method_name = name[1..-1].to_sym
69
+ method = <<-EOF
70
+ def self.#{method_name}
71
+ configuration.send(:#{method_name})
72
+ end
73
+ EOF
74
+ MetricFu.module_eval(method)
75
+ end
76
+ end
77
+
78
+ # Searches through the instance variables of the class and creates
79
+ # an attribute accessor on this instance of the Configuration
80
+ # class for each instance variable.
81
+ def add_attr_accessors_to_self
82
+ instance_variables.each do |name|
83
+ method_name = name[1..-1].to_sym
84
+ MetricFu::Configuration.send(:attr_accessor, method_name)
85
+ end
86
+ end
87
+
88
+ # This allows us to have a nice syntax like:
89
+ #
90
+ # MetricFu.run do |config|
91
+ # config.base_directory = 'tmp/metric_fu'
92
+ # end
93
+ #
94
+ # See the README for more information on configuration options.
95
+ def self.run
96
+ yield MetricFu.configuration
97
+ end
98
+
99
+ # This does the real work of the Configuration class, by setting
100
+ # up a bunch of instance variables to represent the configuration
101
+ # of the MetricFu app.
102
+ def reset
103
+ @base_directory = ENV['CC_BUILD_ARTIFACTS'] || 'tmp/metric_fu'
104
+ @scratch_directory = File.join(@base_directory, 'scratch')
105
+ @output_directory = File.join(@base_directory, 'output')
106
+ @data_directory = File.join('tmp/metric_fu', '_data')
107
+ @metric_fu_root_directory = File.join(File.dirname(__FILE__),
108
+ '..', '..')
109
+ @template_directory = File.join(@metric_fu_root_directory,
110
+ 'lib', 'templates')
111
+ @template_class = AwesomeTemplate
112
+ set_metrics
113
+ set_graphs
114
+ set_code_dirs
115
+ @flay = { :dirs_to_flay => @code_dirs,
116
+ :minimum_score => 100,
117
+ :filetypes => ['rb'] }
118
+ @flog = { :dirs_to_flog => @code_dirs }
119
+ @reek = { :dirs_to_reek => @code_dirs,
120
+ :config_file_pattern => nil}
121
+ @roodi = { :dirs_to_roodi => @code_dirs,
122
+ :roodi_config => nil}
123
+ @saikuro = { :output_directory => @scratch_directory + '/saikuro',
124
+ :input_directory => @code_dirs,
125
+ :cyclo => "",
126
+ :filter_cyclo => "0",
127
+ :warn_cyclo => "5",
128
+ :error_cyclo => "7",
129
+ :formater => "text"}
130
+ @churn = {}
131
+ @stats = {}
132
+ @rcov = { :environment => 'test',
133
+ :test_files => ['test/**/*_test.rb',
134
+ 'spec/**/*_spec.rb'],
135
+ :rcov_opts => ["--sort coverage",
136
+ "--no-html",
137
+ "--text-coverage",
138
+ "--no-color",
139
+ "--profile",
140
+ "--rails",
141
+ "--exclude /gems/,/Library/,/usr/,spec"],
142
+ :external => nil
143
+ }
144
+ @rails_best_practices = {}
145
+ @hotspots = {}
146
+ @file_globs_to_ignore = []
147
+
148
+ @verbose = false
149
+
150
+ @graph_engine = :bluff # can be :bluff or :gchart
151
+
152
+ @darwin_txmt_protocol_no_thanks = false
153
+ @syntax_highlighting = true #Can be set to false to avoid UTF-8 issues with Ruby 1.9.2 and Syntax 1.0
154
+ end
155
+
156
+ # Perform a simple check to try and guess if we're running
157
+ # against a rails app.
158
+ #
159
+ # @todo This should probably be made a bit more robust.
160
+ def rails?
161
+ @rails = File.exist?("config/environment.rb")
162
+ end
163
+
164
+ # Add the :stats task to the AVAILABLE_METRICS constant if we're
165
+ # running within rails.
166
+ def set_metrics
167
+ if rails?
168
+ @metrics = MetricFu::AVAILABLE_METRICS + [:stats, :rails_best_practices]
169
+ else
170
+ @metrics = MetricFu::AVAILABLE_METRICS
171
+ end
172
+ end
173
+
174
+ def set_graphs
175
+ if rails?
176
+ @graphs = MetricFu::AVAILABLE_GRAPHS + [:stats]
177
+ else
178
+ @graphs = MetricFu::AVAILABLE_GRAPHS
179
+ end
180
+ end
181
+
182
+ # Add the 'app' directory if we're running within rails.
183
+ def set_code_dirs
184
+ if rails?
185
+ @code_dirs = ['app', 'lib']
186
+ else
187
+ @code_dirs = ['lib']
188
+ end
189
+ end
190
+
191
+ def platform #:nodoc:
192
+ return RUBY_PLATFORM
193
+ end
194
+
195
+ def is_cruise_control_rb?
196
+ !!ENV['CC_BUILD_ARTIFACTS']
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,50 @@
1
+ class FlayAnalyzer
2
+ include ScoringStrategies
3
+
4
+ COLUMNS = %w{flay_reason flay_matching_reason}
5
+
6
+ def columns
7
+ COLUMNS
8
+ end
9
+
10
+ def name
11
+ :flay
12
+ end
13
+
14
+ def map(row)
15
+ ScoringStrategies.present(row)
16
+ end
17
+
18
+ def reduce(scores)
19
+ ScoringStrategies.sum(scores)
20
+ end
21
+
22
+ def score(metric_ranking, item)
23
+ ScoringStrategies.percentile(metric_ranking, item)
24
+ end
25
+
26
+ def generate_records(data, table)
27
+ return if data==nil
28
+ Array(data[:matches]).each do |match|
29
+ problems = match[:reason]
30
+ matching_reason = problems.gsub(/^[0-9]+\) /,'').gsub(/\:[0-9]+/,'')
31
+ files = []
32
+ locations = []
33
+ match[:matches].each do |file_match|
34
+ file_path = file_match[:name].sub(%r{^/},'')
35
+ locations << "#{file_path}:#{file_match[:line]}"
36
+ files << file_path
37
+ end
38
+ files = files.uniq
39
+ files.each do |file|
40
+ table << {
41
+ "metric" => self.name,
42
+ "file_path" => file,
43
+ "flay_reason" => problems+" files: #{locations.join(', ')}",
44
+ "flay_matching_reason" => matching_reason
45
+ }
46
+ end
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,43 @@
1
+ class FlogAnalyzer
2
+ include ScoringStrategies
3
+
4
+ COLUMNS = %w{score}
5
+
6
+ def columns
7
+ COLUMNS
8
+ end
9
+
10
+ def name
11
+ :flog
12
+ end
13
+
14
+ def map(row)
15
+ row.score
16
+ end
17
+
18
+ def reduce(scores)
19
+ ScoringStrategies.average(scores)
20
+ end
21
+
22
+ def score(metric_ranking, item)
23
+ ScoringStrategies.identity(metric_ranking, item)
24
+ end
25
+
26
+ def generate_records(data, table)
27
+ return if data==nil
28
+ Array(data[:method_containers]).each do |method_container|
29
+ Array(method_container[:methods]).each do |entry|
30
+ file_path = entry[1][:path].sub(%r{^/},'') if entry[1][:path]
31
+ location = MetricFu::Location.for(entry.first)
32
+ table << {
33
+ "metric" => name,
34
+ "score" => entry[1][:score],
35
+ "file_path" => file_path,
36
+ "class_name" => location.class_name,
37
+ "method_name" => location.method_name
38
+ }
39
+ end
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,166 @@
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
+ if MetricFu.configuration.verbose
125
+ puts "Executing #{self.class.to_s.gsub(/.*::/, '')}"
126
+ end
127
+
128
+ %w[emit analyze].each do |meth|
129
+ send("before_#{meth}".to_sym)
130
+ send("#{meth}".to_sym)
131
+ send("after_#{meth}".to_sym)
132
+ end
133
+ before_to_h()
134
+ to_h()
135
+ end
136
+
137
+ def round_to_tenths(decimal)
138
+ decimal = 0.0 if decimal.to_s.eql?('NaN')
139
+ (decimal * 10).round / 10.0
140
+ end
141
+
142
+ def emit #:nodoc:
143
+ raise <<-EOF
144
+ This method must be implemented by a concrete class descending
145
+ from Generator. See generator class documentation for more
146
+ information.
147
+ EOF
148
+ end
149
+
150
+ def analyze #:nodoc:
151
+ raise <<-EOF
152
+ This method must be implemented by a concrete class descending
153
+ from Generator. See generator class documentation for more
154
+ information.
155
+ EOF
156
+ end
157
+
158
+ def to_graph #:nodoc:
159
+ raise <<-EOF
160
+ This method must be implemented by a concrete class descending
161
+ from Generator. See generator class documentation for more
162
+ information.
163
+ EOF
164
+ end
165
+ end
166
+ end