nielsm-metric_fu 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY ADDED
@@ -0,0 +1,98 @@
1
+ === MetricFu 0.9.0 / 2009-1-25
2
+
3
+ * Adding line numbers to the views so that people viewing it on cc.rb can figure out where the problems are
4
+ * Merging in changes from Jay Zeschin having to do with the railroad task -- I still have no idea how to use it (lemme know if you figure it out)
5
+ * Added totals to Flog results
6
+ * Moved rcov options to configuration
7
+
8
+ === MetricFu 0.8.9 / 2009-1-20
9
+
10
+ * Thanks to Andre Arko and Petrik de Heus for adding the following features:
11
+ * The source control type is auto-detected for Churn
12
+ * Moved all presentation to templates
13
+ * Wrote specs for all classes
14
+ * Added flay, Reek and Roodi metrics
15
+ * There's now a configuration class (see README for details)
16
+ * Unification of metrics reports
17
+ * Metrics can be generated using one command
18
+ * Adding new metrics reports has been standardized
19
+
20
+ === MetricFu 0.8.0 / 2008-10-06
21
+
22
+ * Source Control Churn now supports git (thanks to Erik St Martin)
23
+ * Flog Results are sorted by Highest Flog Score
24
+ * Fix for a bunch of 'already initialized constant' warnings that metric_fu caused
25
+ * Fixing bug so the flog reporter can handle methods with digits in the name (thanks to Andy Gregorowicz)
26
+ * Internal Rake task now allows metric_fu to flog/churn itself
27
+
28
+ === MetricFu 0.7.6 / 2008-09-15
29
+
30
+ * CHURN_OPTIONS has become MetricFu::CHURN_OPTIONS
31
+ * SAIKURO_OPTIONS has become MetricFu::SAIKURO_OPTIONS
32
+ * Rcov now looks at test and specs
33
+ * Exclude gems and Library ruby code from rcov
34
+ * Fixed bug with churn start_date functionality (bad path)
35
+
36
+ === MetricFu 0.7.5 / 2008-09-12
37
+
38
+ * Flog can now flog any set of directories you like (see README).
39
+ * Saikuro can now look at any set of directories you like (see README).
40
+
41
+ === MetricFu 0.7.1 / 2008-09-12
42
+
43
+ * Fixed filename bugs pointed out by Bastien
44
+
45
+ === MetricFu 0.7.0 / 2008-09-11
46
+
47
+ * Merged in Sean Soper's changes to metric_fu.
48
+ * Metric_fu is now a gem.
49
+ * Flogging now uses a MD5 hash to figure out if it should re-flog a file (if it's changed)
50
+ * Flogging also has a cool new output screen(s)
51
+ * Thanks Sean!
52
+
53
+ === Metricks 0.4.2 / 2008-07-01
54
+
55
+ * Changed rcov output directory so that it is no longer 'coverage/unit' but just 'coverage' for better integration with CC.rb
56
+
57
+ === Metricks 0.4.1 / 2008-06-13
58
+
59
+ * Rcov tests now extend beyond one level depth directory by using RcovTask instead of the shell
60
+
61
+ === Metricks 0.4.0 / 2008-06-13
62
+
63
+ * Implementing functionality for use as a gem
64
+ * Added Rakefile to facilitate testing
65
+
66
+ === Metricks 0.3.0 / 2008-06-11
67
+
68
+ * Generated reports now open on darwin automatically
69
+ * Generated reports reside under tmp/metricks unless otherwise specified by ENV['CC_BUILD_ARTIFACTS']
70
+ * MD5Tracker works with Flog reports for speed optimization
71
+
72
+ === Metricks 0.2.0 / 2008-06-11
73
+
74
+ * Integrated use of base directory constant
75
+ * Have all reports automatically open in a browser if platform is darwin
76
+ * Namespaced under Metricks
77
+ * Dropped use of shell md5 command in favor of Ruby's Digest::MD5 libraries
78
+
79
+ === Metricks 0.1.0 / 2008-06-10
80
+
81
+ * Initial integration of metric_fu and my enhancements to flog
82
+ * Metrics are generated but are all over the place
83
+
84
+ === MetricFu 0.6.0 / 2008-05-11
85
+
86
+ * Add source control churn report
87
+
88
+ === MetricFu 0.5.1 / 2008-04-25
89
+
90
+ * Fixed bug with Saikuro report generation
91
+
92
+ === MetricFu 0.5.0 / 2008-04-25
93
+
94
+ * create MetricFu as a Rails Plugin
95
+ * Add Flog Report
96
+ * Add Coverage Report
97
+ * Add Saikuro Report
98
+ * Add Stats Report
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2008,2009 Jake Scruggs
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,25 @@
1
+ History.txt
2
+ Manifest.txt
3
+ metric_fu-0.7.gem
4
+ metric_fu.gemspec
5
+ MIT-LICENSE
6
+ Rakefile
7
+ README
8
+ TODO.txt
9
+ lib/metric_fu.rb
10
+ lib/metric_fu/base.rb
11
+ lib/metric_fu/churn.rb
12
+ lib/metric_fu/flay_reporter.rb
13
+ lib/metric_fu/flog_reporter.rb
14
+ lib/metric_fu/md5_tracker.rb
15
+ lib/metric_fu/saikuro/saikuro.rb
16
+ lib/metric_fu/saikuro/SAIKURO_README
17
+ lib/tasks/churn.rake
18
+ lib/tasks/coverage.rake
19
+ lib/tasks/flog.rake
20
+ lib/tasks/metric_fu.rake
21
+ lib/tasks/metric_fu.rb
22
+ lib/tasks/saikuro.rake
23
+ lib/tasks/stats.rake
24
+ test/test_helper.rb
25
+ test/test_md5_tracker.rb
data/README ADDED
@@ -0,0 +1,154 @@
1
+ Version 0.9.0
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.
@@ -0,0 +1,11 @@
1
+ require 'rake'
2
+ require 'rake/rdoctask'
3
+ require 'spec/rake/spectask'
4
+ require File.join(File.dirname(__FILE__), 'lib', 'metric_fu')
5
+
6
+ desc "Run all specs in spec directory"
7
+ Spec::Rake::SpecTask.new(:spec) do |t|
8
+ t.spec_files = FileList['spec/**/*_spec.rb']
9
+ end
10
+
11
+ task :default => [:"metrics:all"]
data/TODO ADDED
@@ -0,0 +1,13 @@
1
+ == TODO list
2
+
3
+ * Color code flog results with scale from: http://jakescruggs.blogspot.com/2008/08/whats-good-flog-score.html
4
+ * Extract functionality from rake files and put under test
5
+ * Integrate Flog, Saikuro, and Coverage into one report so you can see methods that have high complexity and low coverage (this is a big one)
6
+ * Integrate MD5 hashing with remainder of reports
7
+ * Move HTML out of code and into templates/
8
+ * Replace #generate_report with #new on each metric class
9
+ * Make each class descend from MetricFu::CodeMetric
10
+ * Generate metrics:* rake tasks for each of CodeMetric's descendants
11
+ * Update flog specs so that they actually run flog
12
+ * Add flay specs that run flay
13
+ * Convert readme to markdown and rename to README.mkdn so github will render it
@@ -0,0 +1,3 @@
1
+ require File.join(File.dirname(__FILE__), 'metric_fu', 'base') #require first because of dependecies
2
+ require File.join(File.dirname(__FILE__), 'tasks', 'metric_fu')
3
+ Dir[File.join(File.dirname(__FILE__), 'metric_fu/*.rb')].each{|l| require l }
@@ -0,0 +1,160 @@
1
+ require 'erb'
2
+ module MetricFu
3
+
4
+ TEMPLATE_DIR = File.join(File.dirname(__FILE__), '..', 'templates')
5
+ BASE_DIRECTORY = ENV['CC_BUILD_ARTIFACTS'] || 'tmp/metric_fu'
6
+ RAILS = File.exist?("config/environment.rb")
7
+
8
+ if RAILS
9
+ CODE_DIRS = ['app', 'lib']
10
+ DEFAULT_METRICS = [:coverage, :churn, :flog, :flay, :reek, :roodi, :stats, :saikuro ]
11
+ else
12
+ CODE_DIRS = ['lib']
13
+ DEFAULT_METRICS = [:coverage, :churn, :flog, :flay, :reek, :roodi, :saikuro ]
14
+ end
15
+
16
+ module Base
17
+
18
+ ######################################################################
19
+ # Base class for report Generators
20
+ #
21
+ class Generator
22
+
23
+ def initialize(options={})
24
+ @base_dir = self.class.metric_dir
25
+ end
26
+
27
+ def self.metric_dir
28
+ File.join(BASE_DIRECTORY, template_name)
29
+ end
30
+
31
+ def self.template_name
32
+ self.to_s.split('::').last.downcase
33
+ end
34
+
35
+ def self.generate_report(options={})
36
+ FileUtils.mkdir_p(metric_dir, :verbose => false) unless File.directory?(metric_dir)
37
+ self.new(options).generate_report
38
+ end
39
+
40
+ def save_html(content, file='index.html')
41
+ open("#{@base_dir}/#{file}", "w") do |f|
42
+ f.puts content
43
+ end
44
+ end
45
+
46
+ def generate_report
47
+ save_html(generate_html)
48
+ end
49
+
50
+ def generate_html
51
+ analyze
52
+ html = ERB.new(File.read(template_file)).result(binding)
53
+ end
54
+
55
+ def template_name
56
+ self.class.template_name
57
+ end
58
+
59
+ def template_file
60
+ File.join(MetricFu::TEMPLATE_DIR, "#{template_name}.html.erb")
61
+ end
62
+
63
+ ########################
64
+ # Template methods
65
+
66
+ def inline_css(css)
67
+ open(File.join(MetricFu::TEMPLATE_DIR, css)) { |f| f.read }
68
+ end
69
+
70
+ def link_to_filename(name, line = nil)
71
+ filename = File.expand_path(name)
72
+ if PLATFORM['darwin']
73
+ %{<a href="txmt://open/?url=file://#{filename}&line=#{line}">#{name}:#{line}</a>}
74
+ else
75
+ %{<a href="file://#{filename}">#{name}:#{line}</a>}
76
+ end
77
+ end
78
+
79
+ def cycle(first_value, second_value, iteration)
80
+ return first_value if iteration % 2 == 0
81
+ return second_value
82
+ end
83
+ end
84
+ end
85
+
86
+ class << self
87
+ # The Configuration instance used to configure the Rails environment
88
+ def configuration
89
+ @@configuration ||= Configuration.new
90
+ end
91
+
92
+ def churn
93
+ configuration.churn
94
+ end
95
+
96
+ def coverage
97
+ configuration.coverage
98
+ end
99
+
100
+ def flay
101
+ configuration.flay
102
+ end
103
+
104
+ def flog
105
+ configuration.flog
106
+ end
107
+
108
+ def metrics
109
+ configuration.metrics
110
+ end
111
+
112
+ def open_in_browser?
113
+ PLATFORM['darwin'] && !ENV['CC_BUILD_ARTIFACTS']
114
+ end
115
+
116
+ def saikuro
117
+ configuration.saikuro
118
+ end
119
+
120
+ def reek
121
+ configuration.reek
122
+ end
123
+
124
+ def roodi
125
+ configuration.roodi
126
+ end
127
+
128
+ end
129
+
130
+ class Configuration
131
+ attr_accessor :churn, :coverage, :flay, :flog, :metrics, :reek, :roodi, :saikuro
132
+ def initialize
133
+ raise "Use config.churn instead of MetricFu::CHURN_OPTIONS" if defined? ::MetricFu::CHURN_OPTIONS
134
+ raise "Use config.flog[:dirs_to_flog] instead of MetricFu::DIRECTORIES_TO_FLOG" if defined? ::MetricFu::DIRECTORIES_TO_FLOG
135
+ raise "Use config.saikuro instead of MetricFu::SAIKURO_OPTIONS" if defined? ::MetricFu::SAIKURO_OPTIONS
136
+ reset
137
+ end
138
+
139
+ def self.run()
140
+ yield MetricFu.configuration
141
+ end
142
+
143
+ def reset
144
+ @churn = {}
145
+ @coverage = { :test_files => ['test/**/*_test.rb', 'spec/**/*_spec.rb'],
146
+ :rcov_opts => ["--sort coverage", "--html", "--rails", "--exclude /gems/,/Library/,spec"] }
147
+ @flay = { :dirs_to_flay => CODE_DIRS}
148
+ @flog = { :dirs_to_flog => CODE_DIRS}
149
+ @reek = { :dirs_to_reek => CODE_DIRS}
150
+ @roodi = { :dirs_to_roodi => CODE_DIRS}
151
+ @metrics = DEFAULT_METRICS
152
+ @saikuro = {}
153
+ end
154
+
155
+ def saikuro=(options)
156
+ raise "saikuro need to be a Hash" unless options.is_a?(Hash)
157
+ @saikuro = options
158
+ end
159
+ end
160
+ end