jscruggs-metric_fu 0.9.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +10 -0
- data/README +1 -154
- data/Rakefile +37 -2
- data/TODO +2 -1
- data/lib/base/base_template.rb +134 -0
- data/lib/base/configuration.rb +187 -0
- data/lib/base/generator.rb +144 -0
- data/lib/{metric_fu → base}/md5_tracker.rb +0 -0
- data/lib/base/report.rb +100 -0
- data/lib/{metric_fu → generators}/churn.rb +20 -22
- data/lib/generators/flay.rb +29 -0
- data/lib/generators/flog.rb +130 -0
- data/lib/generators/rcov.rb +74 -0
- data/lib/generators/reek.rb +31 -0
- data/lib/generators/roodi.rb +28 -0
- data/lib/generators/saikuro.rb +201 -0
- data/lib/generators/stats.rb +43 -0
- data/lib/metric_fu.rb +20 -3
- data/lib/templates/{churn.html.erb → standard/churn.html.erb} +12 -4
- data/lib/templates/{default.css → standard/default.css} +20 -1
- data/lib/templates/{flay.html.erb → standard/flay.html.erb} +12 -9
- data/lib/templates/standard/flog.html.erb +52 -0
- data/lib/templates/standard/index.html.erb +38 -0
- data/lib/templates/standard/rcov.html.erb +42 -0
- data/lib/templates/standard/reek.html.erb +41 -0
- data/lib/templates/{roodi.html.erb → standard/roodi.html.erb} +10 -8
- data/lib/templates/standard/saikuro.html.erb +83 -0
- data/lib/templates/standard/standard_template.rb +26 -0
- data/lib/templates/standard/stats.html.erb +54 -0
- data/spec/base/base_template_spec.rb +140 -0
- data/spec/base/configuration_spec.rb +303 -0
- data/spec/base/generator_spec.rb +159 -0
- data/spec/{md5_tracker_spec.rb → base/md5_tracker_spec.rb} +1 -1
- data/spec/base/report_spec.rb +139 -0
- data/spec/generators/churn_spec.rb +152 -0
- data/spec/generators/flay_spec.rb +101 -0
- data/spec/generators/flog_spec.rb +189 -0
- data/spec/generators/reek_spec.rb +47 -0
- data/spec/generators/saikuro_spec.rb +35 -0
- data/spec/generators/stats_spec.rb +74 -0
- data/spec/spec_helper.rb +24 -7
- data/tasks/metric_fu.rake +14 -0
- data/{lib/tasks → tasks}/railroad.rake +0 -0
- data/{lib/metric_fu → vendor}/saikuro/saikuro.rb +0 -0
- metadata +58 -47
- data/lib/metric_fu/base.rb +0 -160
- data/lib/metric_fu/flay.rb +0 -17
- data/lib/metric_fu/flog.rb +0 -129
- data/lib/metric_fu/reek.rb +0 -17
- data/lib/metric_fu/roodi.rb +0 -17
- data/lib/tasks/churn.rake +0 -9
- data/lib/tasks/coverage.rake +0 -54
- data/lib/tasks/flay.rake +0 -6
- data/lib/tasks/flog.rake +0 -69
- data/lib/tasks/metric_fu.rake +0 -24
- data/lib/tasks/metric_fu.rb +0 -6
- data/lib/tasks/reek.rake +0 -6
- data/lib/tasks/roodi.rake +0 -7
- data/lib/tasks/saikuro.rake +0 -35
- data/lib/tasks/stats.rake +0 -14
- data/lib/templates/flog.html.erb +0 -38
- data/lib/templates/flog_page.html.erb +0 -25
- data/lib/templates/reek.html.erb +0 -30
- data/spec/base_spec.rb +0 -57
- data/spec/churn_spec.rb +0 -117
- data/spec/config_spec.rb +0 -110
- data/spec/flay_spec.rb +0 -19
- data/spec/flog_spec.rb +0 -208
- data/spec/reek_spec.rb +0 -26
data/HISTORY
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
=== MetricFu 1.0.0 / 2009-4-30
|
2
|
+
|
3
|
+
* Merged in Grant McInnes' work on creating yaml output for all metrics to aid harvesting by other tools
|
4
|
+
* Supporting Flog 2.1.0
|
5
|
+
* Supporting Reek 1.0.0
|
6
|
+
* Removed dependency on Rails Env for 3.months.ago (for churn report), now using chronic gem ("3 months ago").
|
7
|
+
* Almost all code is out of Rakefiles now and so is more easily testable
|
8
|
+
* Metrics inherit from a refactored Generator now. New metrics generators just have to implement "emit", "analyze", "to_h" and inherit from Generator. They also must have a template. See the flay generator and template for a simple implementation.
|
9
|
+
* You now define the metrics you wish to run in the configuration and then run "metrics:all". No other metrics task is exposed by default.
|
10
|
+
|
1
11
|
=== MetricFu 0.9.0 / 2009-1-25
|
2
12
|
|
3
13
|
* Adding line numbers to the views so that people viewing it on cc.rb can figure out where the problems are
|
data/README
CHANGED
@@ -1,154 +1 @@
|
|
1
|
-
|
2
|
-
http://github.com/jscruggs/metric_fu
|
3
|
-
|
4
|
-
Metric_fu began its life as a plugin for Rails that generated code metrics reports. As of version 0.7.0, metric_fu is a gem owing to the excellent work done by Sean Soper.
|
5
|
-
|
6
|
-
Metric_fu is a set of rake tasks that make it easy to generate metrics reports. It uses Saikuro, Flog, Rcov, and Rails' built-in stats task to create a series of reports. It's designed to integrate easily with CruiseControl.rb by placing files in the Custom Build Artifacts folder.
|
7
|
-
|
8
|
-
****Installation****
|
9
|
-
sudo gem install jscruggs-metric_fu -s http://gems.github.com
|
10
|
-
|
11
|
-
Then in your Rakefile:
|
12
|
-
require 'metric_fu'
|
13
|
-
|
14
|
-
If you like to vendor gems, you can unpack metric_fu into vendor/gems and require it like so:
|
15
|
-
require 'vendor/gems/jscruggs-metric_fu-0.9.0/lib/metric_fu'
|
16
|
-
|
17
|
-
then you don't have to install it on every box you run it on.
|
18
|
-
|
19
|
-
Important note:
|
20
|
-
You must have Rcov and Flog installed to get coverage and flog reports. Metric_fu requires both of these gems so they will be installed when you install the metric_fu gem.
|
21
|
-
|
22
|
-
|
23
|
-
****Usage****
|
24
|
-
|
25
|
-
Out of the box metrics provides these tasks:
|
26
|
-
rake metrics:all # Generate coverage, cyclomatic complexity, flog, flay, railroad and churn reports
|
27
|
-
rake metrics:churn # Which files change the most
|
28
|
-
rake metrics:coverage # Generate and open coverage report
|
29
|
-
rake metrics:coverage:clean # Delete aggregate coverage data.
|
30
|
-
rake metrics:coverage:clobber_do # Remove rcov products for do
|
31
|
-
rake metrics:coverage:do # RCov task to generate report
|
32
|
-
rake metrics:flay # Generate code duplication report with flay
|
33
|
-
rake metrics:flog:all # Generate and open flog report
|
34
|
-
rake metrics:flog:clean # Delete aggregate flog data.
|
35
|
-
rake metrics:flog:controllers # Flog code in app/controllers
|
36
|
-
rake metrics:flog:custom # Generate a flog report from specified directories
|
37
|
-
rake metrics:flog:helpers # Flog code in app/helpers
|
38
|
-
rake metrics:flog:lib # Flog code in lib
|
39
|
-
rake metrics:flog:models # Flog code in app/models
|
40
|
-
rake metrics:reek # A code smell report using Reek
|
41
|
-
rake metrics:saikuro # A cyclomatic complexity report using Saikuro
|
42
|
-
|
43
|
-
Rails projects also have the following tasks:
|
44
|
-
|
45
|
-
rake metrics:stats # A stats report
|
46
|
-
|
47
|
-
See below for more detail on the individual tasks. It's recommended to use CruiseControl.rb to set up a metrics build. See the CruiseControl.rb online docs for more info on how to set up cc.rb and, once you've got that figured out, change the cruise_config.rb file inside your project to have these lines:
|
48
|
-
|
49
|
-
project.rake_task = 'metrics:all_with_migrate'
|
50
|
-
project.scheduler.polling_interval = 24.hours
|
51
|
-
|
52
|
-
Which will check for updates every 24 hours and run all the metrics rake tasks (migrating your test db first). The output will be visible from an individual build's detail page.
|
53
|
-
|
54
|
-
|
55
|
-
****Notes on configuration****
|
56
|
-
|
57
|
-
Metric_fu can be customized to your liking by adding the following to your Rakefile
|
58
|
-
|
59
|
-
MetricFu::Configuration.run do |config|
|
60
|
-
#define which metrics you want to use
|
61
|
-
config.metrics = [:coverage, :flog]
|
62
|
-
config.churn = { :start_date => lambda{ 3.months.ago } }
|
63
|
-
config.coverage = { :test_files => ['test/**/test_*.rb'] }
|
64
|
-
config.flog = { :dirs_to_flog => ['cms/app', 'cms/lib'] }
|
65
|
-
config.flay = { :dirs_to_flay => ['cms/app', 'cms/lib'] }
|
66
|
-
config.saikuro = { "--warn_cyclo" => "3", "--error_cyclo" => "4" }
|
67
|
-
end
|
68
|
-
|
69
|
-
|
70
|
-
****Notes on metrics:coverage****
|
71
|
-
|
72
|
-
When creating a coverage report, metric_fu runs all the tests in the test folder and specs in spec folder using Rcov.
|
73
|
-
You can configure the coverage test files pattern:
|
74
|
-
config.coverage[:test_files] = ['test/**/test_*.rb']
|
75
|
-
|
76
|
-
The default value is ['test/**/*_test.rb', 'spec/**/*_spec.rb']
|
77
|
-
|
78
|
-
You may also configure Rcov options:
|
79
|
-
config.coverage = { :test_files => ['test/**/*_test.rb'],
|
80
|
-
:rcov_opts => ["--exclude /gems/,/Library/"] }
|
81
|
-
|
82
|
-
The default value is { :test_files => ['test/**/*_test.rb', 'spec/**/*_spec.rb'],
|
83
|
-
:rcov_opts => ["--sort coverage", "--html", "--rails", "--exclude /gems/,/Library/,spec"] }
|
84
|
-
|
85
|
-
|
86
|
-
****Notes on metrics:saikuro****
|
87
|
-
|
88
|
-
Saikuro is bundled with metric_fu so you don't have to install it. Look at the SAIKURO_README (or the internet) for more documentation on Saikuro. If you wish to change the options Saikuro is run with, then set this constant in your configuration:
|
89
|
-
|
90
|
-
config.saikuro = { "--warn_cyclo" => "3", "--error_cyclo" => "4" }
|
91
|
-
|
92
|
-
config.saikuro is a hash that gets merged with the default options hash. The above example will set the warn_cyclo to 3 and the error_cyclo to 4 (which is way too low -- it's just an example) instructing Saikuro to flag methods with a higher cyclomatic complexity in it's report.
|
93
|
-
|
94
|
-
If you want to have Saikuro look at multiple folders you can put something like this in your configuration:
|
95
|
-
|
96
|
-
config.saikuro = {"--input_directory" => '"cms/app | cms/lib"'}
|
97
|
-
|
98
|
-
|
99
|
-
****Notes on metrics:flay****
|
100
|
-
|
101
|
-
Flay analyzes ruby code for structural similarities.
|
102
|
-
You can configure which directories need to be flayed.
|
103
|
-
The defaults are 'lib' for non Rails projects and ['app', 'lib'] for Rails projects.
|
104
|
-
|
105
|
-
config.flay[:dirs_to_flay] = ['cms/app', 'cms/lib']
|
106
|
-
|
107
|
-
|
108
|
-
****Notes on metrics:flog****
|
109
|
-
|
110
|
-
Flog is another way of measuring complexity (or tortured code as the Flog authors like to put it). You should check out the awesome, and a little scary, Flog website for more info.
|
111
|
-
'rake metrics:flog:custom' allows you to specify a custom set of directories to Flog (in your configuration).
|
112
|
-
The defaults are 'lib' for non Rails projects and ['app', 'lib'] for Rails projects.
|
113
|
-
|
114
|
-
config.flog[:dirs_to_flog] = ['cms/app', 'cms/lib']
|
115
|
-
|
116
|
-
|
117
|
-
****Notes on metrics:reek****
|
118
|
-
|
119
|
-
Reek detects common code smells in ruby code.
|
120
|
-
You can configure which directories need to be checked.
|
121
|
-
The defaults are 'lib' for non Rails projects and ['app', 'lib'] for Rails projects.
|
122
|
-
|
123
|
-
config.reek[:dirs_to_reek] = ['cms/app', 'cms/lib']
|
124
|
-
|
125
|
-
|
126
|
-
****Notes on metrics:roodi****
|
127
|
-
|
128
|
-
Roodi parses your Ruby code and warns you about design issues you have based on the checks that is has configured.
|
129
|
-
You can configure which directories need to be checked.
|
130
|
-
The defaults are 'lib' for non Rails projects and ['app', 'lib'] for Rails projects.
|
131
|
-
|
132
|
-
config.roodi[:dirs_to_roodi] = ['cms/app', 'cms/lib']
|
133
|
-
|
134
|
-
****Notes on metrics:stats****
|
135
|
-
|
136
|
-
This is just 'rake stats' for Rails put into a file. On my projects I like to be able to look at CruiseControl and get stats about the app at different points in time.
|
137
|
-
|
138
|
-
|
139
|
-
****Notes on metrics:churn****
|
140
|
-
|
141
|
-
Files that change a lot in your project may be bad a sign. This task uses "svn log" to identify those files and put them in a report. The default is to start counting changes from the beginning of your project, which might be too far back so you can change like so:
|
142
|
-
|
143
|
-
config.churn = { :start_date => lambda{ 3.months.ago } }
|
144
|
-
|
145
|
-
The Proc is there because '3.months.ago' only works when after the Rails Environment is loaded (and Rails extends Fixnum) which I didn't want to do every time you run a rake task.
|
146
|
-
|
147
|
-
You can also change the minimum churn count like so:
|
148
|
-
|
149
|
-
config.churn = { :minimum_churn_count => 3 }
|
150
|
-
|
151
|
-
|
152
|
-
****Thanks****
|
153
|
-
|
154
|
-
I'd like to thank the authors of Saikuro, Flog, Rcov, CruiseControl.rb, Flay, Reek, Roodi and Rails for creating such excellent open source products. Also Andre Arko, Petrik de Heus, Sean Soper, Erik St Martin, Andy Gregorowicz, Bastien, Michael Schubert, Kurtis Seebaldt, Toby Tripp, Paul Gross, and Chirdeep Shetty for their help and advice.
|
1
|
+
See http://metric-fu.rubyforge.org/ for documentation, or the HISTORY file for a change log.
|
data/Rakefile
CHANGED
@@ -1,11 +1,46 @@
|
|
1
1
|
require 'rake'
|
2
2
|
require 'rake/rdoctask'
|
3
3
|
require 'spec/rake/spectask'
|
4
|
-
require
|
4
|
+
require 'lib/metric_fu'
|
5
5
|
|
6
6
|
desc "Run all specs in spec directory"
|
7
7
|
Spec::Rake::SpecTask.new(:spec) do |t|
|
8
8
|
t.spec_files = FileList['spec/**/*_spec.rb']
|
9
|
+
t.rcov = true
|
10
|
+
t.rcov_opts = ['--exclude', 'spec,config,Library,usr/lib/ruby']
|
11
|
+
t.rcov_dir = File.join(File.dirname(__FILE__), "tmp")
|
9
12
|
end
|
10
13
|
|
11
|
-
|
14
|
+
MetricFu::Configuration.run do |config|
|
15
|
+
end
|
16
|
+
|
17
|
+
namespace :metrics do
|
18
|
+
desc "Generate all reports"
|
19
|
+
task :all do
|
20
|
+
MetricFu.metrics.each {|metric| MetricFu.report.add(metric) }
|
21
|
+
MetricFu.report.save_output(MetricFu.report.to_yaml,
|
22
|
+
MetricFu.base_directory,
|
23
|
+
'report.yml')
|
24
|
+
MetricFu.report.save_templatized_report
|
25
|
+
if MetricFu.report.open_in_browser?
|
26
|
+
MetricFu.report.show_in_browser(MetricFu.output_directory)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
MetricFu.metrics.each do |metric|
|
31
|
+
desc "Generate report for #{metric}"
|
32
|
+
task metric do
|
33
|
+
|
34
|
+
MetricFu.report.add(metric)
|
35
|
+
MetricFu.report.save_output(MetricFu.report.to_yaml,
|
36
|
+
MetricFu.base_directory,
|
37
|
+
'report.yml')
|
38
|
+
MetricFu.report.save_templatized_report
|
39
|
+
if MetricFu.report.open_in_browser?
|
40
|
+
MetricFu.report.show_in_browser(MetricFu.output_directory)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
task :default => [:"metrics:all"]
|
data/TODO
CHANGED
@@ -10,4 +10,5 @@
|
|
10
10
|
* Generate metrics:* rake tasks for each of CodeMetric's descendants
|
11
11
|
* Update flog specs so that they actually run flog
|
12
12
|
* Add flay specs that run flay
|
13
|
-
* Convert readme to markdown and rename to README.mkdn so github will render it
|
13
|
+
* Convert readme to markdown and rename to README.mkdn so github will render it
|
14
|
+
* Saikuro.rb falls over if tries to parse an empty file. Fair enough. We shouldn't feed it empty files
|
@@ -0,0 +1,134 @@
|
|
1
|
+
module MetricFu
|
2
|
+
|
3
|
+
# The Template class is intended as an abstract class for concrete
|
4
|
+
# template classes to subclass. It provides a variety of utility
|
5
|
+
# methods to make templating a bit easier. However, classes do not
|
6
|
+
# have to inherit from here in order to provide a template. The only
|
7
|
+
# requirement for a template class is that it provides a #write method
|
8
|
+
# to actually write out the template. See StandardTemplate for an
|
9
|
+
# example.
|
10
|
+
class Template
|
11
|
+
attr_accessor :report
|
12
|
+
|
13
|
+
private
|
14
|
+
# Creates a new erb evaluated result from the passed in section.
|
15
|
+
#
|
16
|
+
# @param section String
|
17
|
+
# The section name of
|
18
|
+
#
|
19
|
+
# @return String
|
20
|
+
# The erb evaluated string
|
21
|
+
def erbify(section)
|
22
|
+
require 'erb'
|
23
|
+
erb_doc = File.read(template(section))
|
24
|
+
ERB.new(erb_doc).result(binding)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Determines whether a template file exists for a given section
|
28
|
+
# of the full template.
|
29
|
+
#
|
30
|
+
# @param section String
|
31
|
+
# The section of the template to check against
|
32
|
+
#
|
33
|
+
# @return Boolean
|
34
|
+
# Does a template file exist for this section or not?
|
35
|
+
def template_exists?(section)
|
36
|
+
File.exist?(template(section))
|
37
|
+
end
|
38
|
+
|
39
|
+
# Copies an instance variable mimicing the name of the section
|
40
|
+
# we are trying to render, with a value equal to the passed in
|
41
|
+
# constant. Allows the concrete template classes to refer to
|
42
|
+
# that instance variable from their ERB rendering
|
43
|
+
#
|
44
|
+
# @param section String
|
45
|
+
# The name of the instance variable to create
|
46
|
+
#
|
47
|
+
# @param contents Object
|
48
|
+
# The value to set as the value of the created instance
|
49
|
+
# variable
|
50
|
+
def create_instance_var(section, contents)
|
51
|
+
instance_variable_set("@#{section}", contents)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Generates the filename of the template file to load and
|
55
|
+
# evaluate. In this case, the path to the template directory +
|
56
|
+
# the section name + .html.erb
|
57
|
+
#
|
58
|
+
# @param section String
|
59
|
+
# A section of the template to render
|
60
|
+
#
|
61
|
+
# @return String
|
62
|
+
# A file path
|
63
|
+
def template(section)
|
64
|
+
File.join(this_directory, section.to_s + ".html.erb")
|
65
|
+
end
|
66
|
+
|
67
|
+
# Returns the filename that the template will render into for
|
68
|
+
# a given section. In this case, the section name + '.html'
|
69
|
+
#
|
70
|
+
# @param section String
|
71
|
+
# A section of the template to render
|
72
|
+
#
|
73
|
+
# @return String
|
74
|
+
# The output filename
|
75
|
+
def output_filename(section)
|
76
|
+
section.to_s + ".html"
|
77
|
+
end
|
78
|
+
|
79
|
+
# Returns the contents of a given css file in order to
|
80
|
+
# render it inline into a template.
|
81
|
+
#
|
82
|
+
# @param css String
|
83
|
+
# The name of a css file to open
|
84
|
+
#
|
85
|
+
# @return String
|
86
|
+
# The contents of the css file
|
87
|
+
def inline_css(css)
|
88
|
+
open(File.join(this_directory, css)) { |f| f.read }
|
89
|
+
end
|
90
|
+
|
91
|
+
# Provides a link to open a file through the textmate protocol
|
92
|
+
# on Darwin, or otherwise, a simple file link.
|
93
|
+
#
|
94
|
+
# @param name String
|
95
|
+
#
|
96
|
+
# @param line Integer
|
97
|
+
# The line number to link to, if textmate is available. Defaults
|
98
|
+
# to nil
|
99
|
+
#
|
100
|
+
# @return String
|
101
|
+
# An anchor link to a textmate reference or a file reference
|
102
|
+
def link_to_filename(name, line = nil)
|
103
|
+
filename = File.expand_path(name)
|
104
|
+
if MetricFu.configuration.platform.include?('darwin')
|
105
|
+
"<a href='txmt://open/?url=file://" \
|
106
|
+
+"#{filename}&line=#{line}'>#{name}:#{line}</a>"
|
107
|
+
else
|
108
|
+
"<a href='file://#{filename}'>#{name}:#{line}</a>"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
# Provides a brain dead way to cycle between two values during
|
114
|
+
# an iteration of some sort. Pass in the first_value, the second_value,
|
115
|
+
# and the cardinality of the iteration.
|
116
|
+
#
|
117
|
+
# @param first_value Object
|
118
|
+
#
|
119
|
+
# @param second_value Object
|
120
|
+
#
|
121
|
+
# @param iteration Integer
|
122
|
+
# The number of times through the iteration.
|
123
|
+
#
|
124
|
+
# @return Object
|
125
|
+
# The first_value if iteration is even. The second_value if
|
126
|
+
# iteration is odd.
|
127
|
+
def cycle(first_value, second_value, iteration)
|
128
|
+
return first_value if iteration % 2 == 0
|
129
|
+
return second_value
|
130
|
+
end
|
131
|
+
|
132
|
+
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,187 @@
|
|
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, :saikuro, :rcov]
|
10
|
+
|
11
|
+
|
12
|
+
# The @@configuration class variable holds a global type configuration
|
13
|
+
# object for any parts of the system to use.
|
14
|
+
def self.configuration
|
15
|
+
@@configuration ||= Configuration.new
|
16
|
+
end
|
17
|
+
|
18
|
+
# = Configuration
|
19
|
+
#
|
20
|
+
# The Configuration class, as it sounds, provides methods for
|
21
|
+
# configuring the behaviour of MetricFu.
|
22
|
+
#
|
23
|
+
# == Customization for Rails
|
24
|
+
#
|
25
|
+
# The Configuration class checks for the presence of a
|
26
|
+
# 'config/environment.rb' file. If the file is present, it assumes
|
27
|
+
# it is running in a Rails project. If it is, it will:
|
28
|
+
#
|
29
|
+
# * Add 'app' to the @code_dirs directory to include the
|
30
|
+
# code in the app directory in the processing
|
31
|
+
# * Add :stats to the list of metrics to run to get the Rails stats
|
32
|
+
# task
|
33
|
+
#
|
34
|
+
# == Customization for CruiseControl.rb
|
35
|
+
#
|
36
|
+
# The Configuration class checks for the presence of a
|
37
|
+
# 'CC_BUILD_ARTIFACTS' environment variable. If it's found
|
38
|
+
# it will change the default output directory from the default
|
39
|
+
# "tmp/metric_fu to the directory represented by 'CC_BUILD_ARTIFACTS'
|
40
|
+
#
|
41
|
+
# == Deprications
|
42
|
+
#
|
43
|
+
# The Configuration class checks for several deprecated constants
|
44
|
+
# that were previously used to configure MetricFu. These include
|
45
|
+
# CHURN_OPTIONS, DIRECTORIES_TO_FLOG, SAIKURO_OPTIONS,
|
46
|
+
# and MetricFu::SAIKURO_OPTIONS.
|
47
|
+
#
|
48
|
+
# These have been replaced by config.churn, config.flog and
|
49
|
+
# config.saikuro respectively.
|
50
|
+
class Configuration
|
51
|
+
|
52
|
+
def initialize #:nodoc:#
|
53
|
+
warn_about_deprecated_config_options
|
54
|
+
reset
|
55
|
+
add_attr_accessors_to_self
|
56
|
+
add_class_methods_to_metric_fu
|
57
|
+
end
|
58
|
+
|
59
|
+
# Searches through the instance variables of the class and
|
60
|
+
# creates a class method on the MetricFu module to read the value
|
61
|
+
# of the instance variable from the Configuration class.
|
62
|
+
def add_class_methods_to_metric_fu
|
63
|
+
instance_variables.each do |name|
|
64
|
+
method_name = name[1..-1].to_sym
|
65
|
+
method = <<-EOF
|
66
|
+
def self.#{method_name}
|
67
|
+
configuration.send(:#{method_name})
|
68
|
+
end
|
69
|
+
EOF
|
70
|
+
MetricFu.module_eval(method)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Searches through the instance variables of the class and creates
|
75
|
+
# an attribute accessor on this instance of the Configuration
|
76
|
+
# class for each instance variable.
|
77
|
+
def add_attr_accessors_to_self
|
78
|
+
instance_variables.each do |name|
|
79
|
+
method_name = name[1..-1].to_sym
|
80
|
+
MetricFu::Configuration.send(:attr_accessor, method_name)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Check if certain constants that are deprecated have been
|
85
|
+
# assigned. If so, warn the user about them, and the
|
86
|
+
# fact that they will have no effect.
|
87
|
+
def warn_about_deprecated_config_options
|
88
|
+
if defined?(::MetricFu::CHURN_OPTIONS)
|
89
|
+
raise("Use config.churn instead of MetricFu::CHURN_OPTIONS")
|
90
|
+
end
|
91
|
+
if defined?(::MetricFu::DIRECTORIES_TO_FLOG)
|
92
|
+
raise("Use config.flog[:dirs_to_flog] "+
|
93
|
+
"instead of MetricFu::DIRECTORIES_TO_FLOG")
|
94
|
+
end
|
95
|
+
if defined?(::MetricFu::SAIKURO_OPTIONS)
|
96
|
+
raise("Use config.saikuro instead of MetricFu::SAIKURO_OPTIONS")
|
97
|
+
end
|
98
|
+
if defined?(SAIKURO_OPTIONS)
|
99
|
+
raise("Use config.saikuro instead of SAIKURO_OPTIONS")
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# This allows us to have a nice syntax like:
|
104
|
+
#
|
105
|
+
# MetricFu.run do |config|
|
106
|
+
# config.base_directory = 'tmp/metric_fu'
|
107
|
+
# end
|
108
|
+
#
|
109
|
+
# See the README for more information on configuration options.
|
110
|
+
def self.run
|
111
|
+
yield MetricFu.configuration
|
112
|
+
end
|
113
|
+
|
114
|
+
# This does the real work of the Configuration class, by setting
|
115
|
+
# up a bunch of instance variables to represent the configuration
|
116
|
+
# of the MetricFu app.
|
117
|
+
def reset
|
118
|
+
@base_directory = ENV['CC_BUILD_ARTIFACTS'] || 'tmp/metric_fu'
|
119
|
+
@scratch_directory = File.join(@base_directory, 'scratch')
|
120
|
+
@output_directory = File.join(@base_directory, 'output')
|
121
|
+
@metric_fu_root_directory = File.join(File.dirname(__FILE__),
|
122
|
+
'..', '..')
|
123
|
+
@template_directory = File.join(@metric_fu_root_directory,
|
124
|
+
'lib', 'templates')
|
125
|
+
@template_class = StandardTemplate
|
126
|
+
set_metrics
|
127
|
+
set_code_dirs
|
128
|
+
@flay = { :dirs_to_flay => @code_dirs }
|
129
|
+
@flog = { :dirs_to_flog => @code_dirs }
|
130
|
+
@reek = { :dirs_to_reek => @code_dirs }
|
131
|
+
@roodi = { :dirs_to_roodi => @code_dirs }
|
132
|
+
@saikuro = { :output_directory => @scratch_directory + '/saikuro',
|
133
|
+
:input_directory => @code_dirs,
|
134
|
+
:cyclo => "",
|
135
|
+
:filter_cyclo => "0",
|
136
|
+
:warn_cyclo => "5",
|
137
|
+
:error_cyclo => "7",
|
138
|
+
:formater => "text"}
|
139
|
+
@churn = {}
|
140
|
+
@stats = {}
|
141
|
+
@rcov = { :test_files => ['test/**/*_test.rb',
|
142
|
+
'spec/**/*_spec.rb'],
|
143
|
+
:rcov_opts => ["--sort coverage",
|
144
|
+
"--no-html",
|
145
|
+
"--text-coverage",
|
146
|
+
"--no-color",
|
147
|
+
"--profile",
|
148
|
+
"--rails",
|
149
|
+
"--exclude /gems/,/Library/,spec"]}
|
150
|
+
end
|
151
|
+
|
152
|
+
# Perform a simple check to try and guess if we're running
|
153
|
+
# against a rails app.
|
154
|
+
#
|
155
|
+
# @todo This should probably be made a bit more robust.
|
156
|
+
def rails?
|
157
|
+
@rails = File.exist?("config/environment.rb")
|
158
|
+
end
|
159
|
+
|
160
|
+
# Add the :stats task to the AVAILABLE_METRICS constant if we're
|
161
|
+
# running within rails.
|
162
|
+
def set_metrics
|
163
|
+
if rails?
|
164
|
+
@metrics = MetricFu::AVAILABLE_METRICS + [:stats]
|
165
|
+
else
|
166
|
+
@metrics = MetricFu::AVAILABLE_METRICS
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
# Add the 'app' directory if we're running within rails.
|
171
|
+
def set_code_dirs
|
172
|
+
if rails?
|
173
|
+
@code_dirs = ['app', 'lib']
|
174
|
+
else
|
175
|
+
@code_dirs = ['lib']
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def platform #:nodoc:
|
180
|
+
return PLATFORM
|
181
|
+
end
|
182
|
+
|
183
|
+
def is_cruise_control_rb?
|
184
|
+
!!ENV['CC_BUILD_ARTIFACTS']
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
@@ -0,0 +1,144 @@
|
|
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
|
+
end
|
39
|
+
|
40
|
+
# Creates a new generator and returns the output of the
|
41
|
+
# #generate_report method. This is the typical way to
|
42
|
+
# generate a new MetricFu report. For more information see
|
43
|
+
# the #generate_report instance method.
|
44
|
+
#
|
45
|
+
# @params options Hash
|
46
|
+
# A currently unused hash to configure the Generator
|
47
|
+
#
|
48
|
+
# @see generate_report
|
49
|
+
def self.generate_report(options={})
|
50
|
+
generator = self.new(options)
|
51
|
+
generator.generate_report
|
52
|
+
end
|
53
|
+
|
54
|
+
# Provides the unqualified class name of an implemented concrete
|
55
|
+
# class, as a string. For example:
|
56
|
+
#
|
57
|
+
# class Flay < Generator; end
|
58
|
+
# klass = Flay.new
|
59
|
+
# klass.class_name
|
60
|
+
# > "flay"
|
61
|
+
#
|
62
|
+
# @return String
|
63
|
+
# The unqualified class name of this concrete class, returned
|
64
|
+
# as a string.
|
65
|
+
def self.class_name
|
66
|
+
self.to_s.split('::').last.downcase
|
67
|
+
end
|
68
|
+
|
69
|
+
# Returns the directory where the Generator will write any output
|
70
|
+
def self.metric_directory
|
71
|
+
File.join(MetricFu.scratch_directory, class_name)
|
72
|
+
end
|
73
|
+
|
74
|
+
def create_metric_dir_if_missing #:nodoc:
|
75
|
+
unless File.directory?(metric_directory)
|
76
|
+
FileUtils.mkdir_p(metric_directory, :verbose => false)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def create_output_dir_if_missing #:nodoc:
|
81
|
+
unless File.directory?(MetricFu.output_directory)
|
82
|
+
FileUtils.mkdir_p(MetricFu.output_directory, :verbose => false)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# @return String
|
87
|
+
# The path of the metric directory this class is using.
|
88
|
+
def metric_directory
|
89
|
+
self.class.metric_directory
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
# Defines some hook methods for the concrete classes to hook into.
|
94
|
+
%w[emit analyze].each do |meth|
|
95
|
+
define_method("before_#{meth}".to_sym) {}
|
96
|
+
define_method("after_#{meth}".to_sym) {}
|
97
|
+
end
|
98
|
+
define_method("before_to_h".to_sym) {}
|
99
|
+
|
100
|
+
# Provides a template method to drive the production of a metric
|
101
|
+
# from a concrete implementation of this class. Each concrete
|
102
|
+
# class must implement the three methods that this template method
|
103
|
+
# calls: #emit, #analyze and #to_h. For more details, see the
|
104
|
+
# class documentation.
|
105
|
+
#
|
106
|
+
# This template method also calls before_emit, after_emit... etc.
|
107
|
+
# methods to allow extra hooks into the processing methods, and help
|
108
|
+
# to keep the logic of your Generators clean.
|
109
|
+
def generate_report
|
110
|
+
%w[emit analyze].each do |meth|
|
111
|
+
send("before_#{meth}".to_sym)
|
112
|
+
send("#{meth}".to_sym)
|
113
|
+
send("after_#{meth}".to_sym)
|
114
|
+
end
|
115
|
+
before_to_h()
|
116
|
+
to_h()
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
def emit #:nodoc:
|
121
|
+
raise <<-EOF
|
122
|
+
This method must be implemented by a concrete class descending
|
123
|
+
from Generator. See generator class documentation for more
|
124
|
+
information.
|
125
|
+
EOF
|
126
|
+
end
|
127
|
+
|
128
|
+
def analyze #:nodoc:
|
129
|
+
raise <<-EOF
|
130
|
+
This method must be implemented by a concrete class descending
|
131
|
+
from Generator. See generator class documentation for more
|
132
|
+
information.
|
133
|
+
EOF
|
134
|
+
end
|
135
|
+
|
136
|
+
def to_h #:nodoc:
|
137
|
+
raise <<-EOF
|
138
|
+
This method must be implemented by a concrete class descending
|
139
|
+
from Generator. See generator class documentation for more
|
140
|
+
information.
|
141
|
+
EOF
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
File without changes
|