rferraz-metric_fu 2.1.1

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 +448 -0
@@ -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
data/lib/base/graph.rb ADDED
@@ -0,0 +1,44 @@
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_parts = year_month_day_from_filename(metric_file)
27
+ metrics = YAML::load(File.open(metric_file))
28
+
29
+ self.clazz.each do |grapher|
30
+ grapher.get_metrics(metrics, "#{date_parts[:m]}/#{date_parts[:d]}")
31
+ end
32
+ end
33
+ self.clazz.each do |grapher|
34
+ grapher.graph!
35
+ end
36
+ end
37
+
38
+ private
39
+ def year_month_day_from_filename(path_to_file_with_date)
40
+ date = path_to_file_with_date.match(/\/(\d+).yml$/)[1]
41
+ {:y => date[0..3].to_i, :m => date[4..5].to_i, :d => date[6..7].to_i}
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,74 @@
1
+ require 'ruby_parser'
2
+ module MetricFu
3
+ class LineNumbers
4
+
5
+ def initialize(contents)
6
+ rp = RubyParser.new
7
+ @locations = {}
8
+ file_sexp = rp.parse(contents)
9
+ case file_sexp[0]
10
+ when :class
11
+ process_class(file_sexp)
12
+ when :module
13
+ process_module(file_sexp)
14
+ when :block
15
+ file_sexp.each_of_type(:class) { |sexp| process_class(sexp) }
16
+ else
17
+ end
18
+ rescue Exception
19
+ #catch errors for files ruby_parser fails on
20
+ @locations
21
+ end
22
+
23
+ def in_method? line_number
24
+ !!@locations.detect do |method_name, line_number_range|
25
+ line_number_range.include?(line_number)
26
+ end
27
+ end
28
+
29
+ def method_at_line line_number
30
+ found_method_and_range = @locations.detect do |method_name, line_number_range|
31
+ line_number_range.include?(line_number)
32
+ end
33
+ return nil unless found_method_and_range
34
+ found_method_and_range.first
35
+ end
36
+
37
+ def start_line_for_method(method)
38
+ return nil unless @locations.has_key?(method)
39
+ @locations[method].first
40
+ end
41
+
42
+ private
43
+
44
+ def process_module(sexp)
45
+ module_name = sexp[1]
46
+ sexp.each_of_type(:class) do |sexp|
47
+ process_class(sexp, module_name)
48
+ hide_methods_from_next_round(sexp)
49
+ end
50
+ process_class(sexp)
51
+ end
52
+
53
+ def process_class(sexp, module_name=nil)
54
+ class_name = sexp[1]
55
+ process_class_self_blocks(sexp, class_name)
56
+ module_name_string = module_name ? "#{module_name}::" : nil
57
+ sexp.each_of_type(:defn) { |s| @locations["#{module_name_string}#{class_name}##{s[1]}"] = (s.line)..(s.last.line) }
58
+ sexp.each_of_type(:defs) { |s| @locations["#{module_name_string}#{class_name}::#{s[2]}"] = (s.line)..(s.last.line) }
59
+ end
60
+
61
+ def process_class_self_blocks(sexp, class_name)
62
+ sexp.each_of_type(:sclass) do |sexp_in_class_self_block|
63
+ sexp_in_class_self_block.each_of_type(:defn) { |s| @locations["#{class_name}::#{s[1]}"] = (s.line)..(s.last.line) }
64
+ hide_methods_from_next_round(sexp_in_class_self_block)
65
+ end
66
+ end
67
+
68
+ def hide_methods_from_next_round(sexp)
69
+ sexp.find_and_replace_all(:defn, :ignore_me)
70
+ sexp.find_and_replace_all(:defs, :ignore_me)
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,85 @@
1
+ module MetricFu
2
+ class Location
3
+ include Comparable
4
+
5
+ attr_accessor :class_name, :method_name, :file_path, :simple_method_name, :hash
6
+
7
+ def self.get(file_path, class_name, method_name)
8
+ # This could be more 'confident' using Maybe, but we want it to be as fast as possible
9
+ file_path_copy = file_path == nil ? nil : file_path.clone
10
+ class_name_copy = class_name == nil ? nil : class_name.clone
11
+ method_name_copy = method_name == nil ? nil : method_name.clone
12
+ key = [file_path_copy, class_name_copy, method_name_copy]
13
+ @@locations ||= {}
14
+ if @@locations.has_key?(key)
15
+ @@locations[key]
16
+ else
17
+ location = self.new(file_path_copy, class_name_copy, method_name_copy)
18
+ @@locations[key] = location
19
+ location.freeze # we cache a lot of method call results, so we want location to be immutable
20
+ location
21
+ end
22
+ end
23
+
24
+ def initialize(file_path, class_name, method_name)
25
+ @file_path = file_path
26
+ @class_name = class_name
27
+ @method_name = method_name
28
+ @simple_method_name = @method_name.sub(@class_name,'') unless @method_name == nil
29
+ @hash = [@file_path, @class_name, @method_name].hash
30
+ end
31
+
32
+ # TODO - we need this method (and hash accessor above) as a temporary hack where we're using Location as a hash key
33
+ def eql?(other)
34
+ [self.file_path.to_s, self.class_name.to_s, self.method_name.to_s] == [other.file_path.to_s, other.class_name.to_s, other.method_name.to_s]
35
+ end
36
+ # END we need these methods as a temporary hack where we're using Location as a hash key
37
+
38
+ def self.for(class_or_method_name)
39
+ class_or_method_name = strip_modules(class_or_method_name)
40
+ if(class_or_method_name)
41
+ begin
42
+ match = class_or_method_name.match(/(.*)((\.|\#|\:\:[a-z])(.+))/)
43
+ rescue => error
44
+ #new error during port to metric_fu occasionally a unintialized
45
+ #MatchData object shows up here. Not expected.
46
+ match = nil
47
+ end
48
+
49
+ # reek reports the method with :: not # on modules like
50
+ # module ApplicationHelper \n def signed_in?, convert it so it records correctly
51
+ # but classes have to start with a capital letter... HACK for REEK bug, reported underlying issue to REEK
52
+ if(match)
53
+ class_name = strip_modules(match[1])
54
+ method_name = class_or_method_name.gsub(/\:\:/,"#")
55
+ else
56
+ class_name = strip_modules(class_or_method_name)
57
+ method_name = nil
58
+ end
59
+ else
60
+ class_name = nil
61
+ method_name = nil
62
+ end
63
+ self.get(nil, class_name, method_name)
64
+ end
65
+
66
+ def <=>(other)
67
+ [self.file_path.to_s, self.class_name.to_s, self.method_name.to_s] <=> [other.file_path.to_s, other.class_name.to_s, other.method_name.to_s]
68
+ end
69
+
70
+ private
71
+
72
+ def self.strip_modules(class_or_method_name)
73
+ # reek reports the method with :: not # on modules like
74
+ # module ApplicationHelper \n def signed_in?, convert it so it records correctly
75
+ # but classes have to start with a capital letter... HACK for REEK bug, reported underlying issue to REEK
76
+ if(class_or_method_name=~/\:\:[A-Z]/)
77
+ class_or_method_name.split("::").last
78
+ else
79
+ class_or_method_name
80
+ end
81
+
82
+ end
83
+
84
+ end
85
+ end