p8-metric_fu 0.8.0.16 → 0.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/README +17 -63
  2. data/Rakefile +1 -1
  3. data/lib/metric_fu/base.rb +8 -114
  4. data/lib/metric_fu/churn.rb +7 -8
  5. data/lib/metric_fu/flay_reporter.rb +17 -0
  6. data/lib/metric_fu/flog_reporter/base.rb +49 -0
  7. data/lib/metric_fu/flog_reporter/generator.rb +39 -0
  8. data/lib/metric_fu/flog_reporter/operator.rb +10 -0
  9. data/lib/metric_fu/flog_reporter/page.rb +33 -0
  10. data/lib/metric_fu/flog_reporter/scanned_method.rb +28 -0
  11. data/lib/metric_fu/flog_reporter.rb +5 -0
  12. data/lib/tasks/churn.rake +3 -1
  13. data/lib/tasks/coverage.rake +35 -49
  14. data/lib/tasks/flay.rake +4 -1
  15. data/lib/tasks/flog.rake +10 -10
  16. data/lib/tasks/metric_fu.rake +4 -8
  17. data/lib/tasks/metric_fu.rb +1 -1
  18. data/lib/tasks/saikuro.rake +1 -1
  19. data/lib/tasks/stats.rake +1 -1
  20. data/lib/templates/churn.css +38 -0
  21. data/lib/templates/churn.html.erb +3 -6
  22. data/lib/templates/flay.css +38 -0
  23. data/lib/templates/flay.html.erb +10 -17
  24. data/lib/templates/flog.css +39 -0
  25. data/lib/templates/flog.html.erb +14 -19
  26. data/lib/templates/flog_page.html.erb +3 -15
  27. data/spec/base_spec.rb +8 -30
  28. data/spec/churn_spec.rb +3 -10
  29. data/spec/{flay_spec.rb → flay_reporter_spec.rb} +2 -9
  30. data/spec/flog_reporter/base_spec.rb +69 -0
  31. data/spec/md5_tracker_spec.rb +3 -1
  32. data/spec/spec_helper.rb +3 -7
  33. metadata +17 -41
  34. data/Manifest.txt +0 -25
  35. data/lib/metric_fu/flay.rb +0 -17
  36. data/lib/metric_fu/flog.rb +0 -139
  37. data/lib/metric_fu/reek.rb +0 -17
  38. data/lib/metric_fu/roodi.rb +0 -17
  39. data/lib/tasks/railroad.rake +0 -36
  40. data/lib/tasks/reek.rake +0 -6
  41. data/lib/tasks/roodi.rake +0 -7
  42. data/lib/templates/default.css +0 -45
  43. data/lib/templates/reek.html.erb +0 -30
  44. data/lib/templates/roodi.html.erb +0 -26
  45. data/spec/config_spec.rb +0 -110
  46. data/spec/flog_spec.rb +0 -147
  47. data/spec/reek_spec.rb +0 -26
data/README CHANGED
@@ -23,13 +23,13 @@ You must have Rcov and Flog installed to get coverage and flog reports. Metric_
23
23
  ****Usage****
24
24
 
25
25
  Out of the box metrics provides these tasks:
26
- rake metrics:all # Generate coverage, cyclomatic complexity, flog, flay, railroad and churn reports
26
+ rake metrics:all
27
+ rake metrics:all_with_migrate # Useful for continuous integration
27
28
  rake metrics:churn # Which files change the most
28
29
  rake metrics:coverage # Generate and open coverage report
29
30
  rake metrics:coverage:clean # Delete aggregate coverage data.
30
31
  rake metrics:coverage:clobber_do # Remove rcov products for do
31
32
  rake metrics:coverage:do # RCov task to generate report
32
- rake metrics:flay # Generate code duplication report with flay
33
33
  rake metrics:flog:all # Generate and open flog report
34
34
  rake metrics:flog:clean # Delete aggregate flog data.
35
35
  rake metrics:flog:controllers # Flog code in app/controllers
@@ -37,12 +37,8 @@ rake metrics:flog:custom # Generate a flog report from specified direct
37
37
  rake metrics:flog:helpers # Flog code in app/helpers
38
38
  rake metrics:flog:lib # Flog code in lib
39
39
  rake metrics:flog:models # Flog code in app/models
40
- rake metrics:reek # A code smell report using Reek
41
40
  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
41
+ rake metrics:stats # A stats report
46
42
 
47
43
  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
44
 
@@ -52,78 +48,32 @@ project.scheduler.polling_interval = 24.hours
52
48
  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
49
 
54
50
 
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
51
  ****Notes on metrics:coverage****
71
52
 
72
53
  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
54
 
78
55
 
79
56
  ****Notes on metrics:saikuro****
80
57
 
81
- 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:
82
-
83
- config.saikuro = { "--warn_cyclo" => "3", "--error_cyclo" => "4" }
84
-
85
- 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.
86
-
87
- If you want to have Saikuro look at multiple folders you can put something like this in your configuration:
88
-
89
- config.saikuro = {"--input_directory" => '"cms/app | cms/lib"'}
58
+ 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 Rakefile:
59
+
60
+ MetricFu::SAIKURO_OPTIONS = { "--warn_cyclo" => "3", "--error_cyclo" => "4" }
90
61
 
62
+ MetricFu::SAIKURO_OPTIONS 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.
91
63
 
92
- ****Notes on metrics:flay****
93
-
94
- Flay analyzes ruby code for structural similarities.
95
- You can configure which directories need to be flayed.
96
- The defaults are 'lib' for non Rails projects and ['app', 'lib'] for Rails projects.
97
-
98
- config.flay[:dirs_to_flay] = ['cms/app', 'cms/lib']
64
+ If you want to have Saikuro look at multiple folders you can put something like this in your rakefile:
65
+ MetricFu::SAIKURO_OPTIONS = {"--input_directory" => '"cms/app | cms/lib"'}
99
66
 
100
67
 
101
68
  ****Notes on metrics:flog****
102
69
 
103
70
  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.
104
- 'rake metrics:flog:custom' allows you to specify a custom set of directories to Flog (in your configuration).
105
- The defaults are 'lib' for non Rails projects and ['app', 'lib'] for Rails projects.
106
-
107
- config.flog[:dirs_to_flog] = ['cms/app', 'cms/lib']
108
-
109
-
110
- ****Notes on metrics:reek****
111
71
 
112
- Reek detects common code smells in ruby code.
113
- You can configure which directories need to be checked.
114
- The defaults are 'lib' for non Rails projects and ['app', 'lib'] for Rails projects.
72
+ 'rake metrics:flog:custom' allows you to specify a custom set of directories to Flog (in your rakefile) like so:
115
73
 
116
- config.reek[:dirs_to_reek] = ['cms/app', 'cms/lib']
74
+ MetricFu::DIRECTORIES_TO_FLOG = ['cms/app', 'cms/lib']
117
75
 
118
76
 
119
- ****Notes on metrics:roodi****
120
-
121
- Roodi parses your Ruby code and warns you about design issues you have based on the checks that is has configured.
122
- You can configure which directories need to be checked.
123
- The defaults are 'lib' for non Rails projects and ['app', 'lib'] for Rails projects.
124
-
125
- config.roodi[:dirs_to_roodi] = ['cms/app', 'cms/lib']
126
-
127
77
  ****Notes on metrics:stats****
128
78
 
129
79
  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.
@@ -133,13 +83,17 @@ This is just 'rake stats' for Rails put into a file. On my projects I like to b
133
83
 
134
84
  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:
135
85
 
136
- config.churn = { :start_date => lambda{ 3.months.ago } }
86
+ MetricFu::CHURN_OPTIONS = { :start_date => lambda{3.months.ago} }
137
87
 
138
88
  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.
139
89
 
140
90
  You can also change the minimum churn count like so:
141
91
 
142
- config.churn = { :minimum_churn_count => 3 }
92
+ MetricFu::CHURN_OPTIONS = { :minimum_churn_count => 3 }
93
+
94
+ If you use git, then tell churn about it:
95
+
96
+ MetricFu::CHURN_OPTIONS = {:scm => :git}
143
97
 
144
98
 
145
99
  ****Thanks****
data/Rakefile CHANGED
@@ -8,4 +8,4 @@ Spec::Rake::SpecTask.new(:spec) do |t|
8
8
  t.spec_files = FileList['spec/**/*_spec.rb']
9
9
  end
10
10
 
11
- task :default => [:"metrics:all"]
11
+ task :default => [:"metrics:all"]
@@ -1,4 +1,3 @@
1
- require 'erb'
2
1
  module MetricFu
3
2
 
4
3
  TEMPLATE_DIR = File.join(File.dirname(__FILE__), '..', 'templates')
@@ -7,38 +6,23 @@ module MetricFu
7
6
 
8
7
  if RAILS
9
8
  CODE_DIRS = ['app', 'lib']
10
- DEFAULT_METRICS = [:coverage, :churn, :flog, :flay, :reek, :roodi, :stats, :saikuro ]
11
9
  else
12
10
  CODE_DIRS = ['lib']
13
- DEFAULT_METRICS = [:coverage, :churn, :flog, :flay, :reek, :roodi, :saikuro ]
14
- end
11
+ end
15
12
 
16
13
  module Base
17
-
18
- ######################################################################
19
- # Base class for report Generators
20
- #
21
14
  class Generator
22
15
 
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)
16
+ def initialize(base_dir, options={})
17
+ @base_dir = base_dir
29
18
  end
30
-
31
- def self.template_name
32
- self.to_s.split('::').last.downcase
33
- end
34
19
 
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
20
+ def self.generate_report(base_dir, options={})
21
+ self.new(base_dir, options).generate_report
38
22
  end
39
23
 
40
- def save_html(content, file='index.html')
41
- open("#{@base_dir}/#{file}", "w") do |f|
24
+ def save_html(content, file='index')
25
+ open("#{@base_dir}/#{file}.html", "w") do |f|
42
26
  f.puts content
43
27
  end
44
28
  end
@@ -51,22 +35,11 @@ module MetricFu
51
35
  analyze
52
36
  html = ERB.new(File.read(template_file)).result(binding)
53
37
  end
54
-
55
- def template_name
56
- self.class.template_name
57
- end
58
38
 
59
39
  def template_file
60
40
  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
41
  end
69
-
42
+
70
43
  def link_to_filename(name, line = nil)
71
44
  filename = File.expand_path(name)
72
45
  if PLATFORM['darwin']
@@ -75,85 +48,6 @@ module MetricFu
75
48
  %{<a href="file://#{filename}">#{name}</a>}
76
49
  end
77
50
  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
- @flay = { :dirs_to_flay => CODE_DIRS}
147
- @flog = { :dirs_to_flog => CODE_DIRS}
148
- @reek = { :dirs_to_reek => CODE_DIRS}
149
- @roodi = { :dirs_to_roodi => CODE_DIRS}
150
- @metrics = DEFAULT_METRICS
151
- @saikuro = {}
152
- end
153
-
154
- def saikuro=(options)
155
- raise "saikuro need to be a Hash" unless options.is_a?(Hash)
156
- @saikuro = options
157
51
  end
158
52
  end
159
53
  end
@@ -1,14 +1,8 @@
1
1
  module MetricFu
2
-
3
- def self.generate_churn_report
4
- Churn.generate_report(MetricFu.churn)
5
- system("open #{Churn.metric_dir}/index.html") if open_in_browser?
6
- end
7
-
8
2
  class Churn < Base::Generator
9
3
 
10
- def initialize(options={})
11
- @base_dir = File.join(MetricFu::BASE_DIRECTORY, template_name)
4
+ def initialize(base_dir, options={})
5
+ @base_dir = base_dir
12
6
  if File.exist?(".git")
13
7
  @source_control = Git.new(options[:start_date])
14
8
  elsif File.exist?(".svn")
@@ -24,6 +18,11 @@ module MetricFu
24
18
  @changes = parse_log_for_changes.reject! {|file, change_count| change_count < @minimum_churn_count}
25
19
  end
26
20
 
21
+ # should be dynamically read from the class
22
+ def template_name
23
+ 'churn'
24
+ end
25
+
27
26
  private
28
27
 
29
28
  def parse_log_for_changes
@@ -0,0 +1,17 @@
1
+ require 'erb'
2
+
3
+ module MetricFu
4
+ class FlayReporter < Base::Generator
5
+
6
+ def analyze
7
+ files_to_flay = MetricFu::CODE_DIRS.map{|dir| Dir[File.join(dir, "**/*.rb")] }
8
+ output = `flay #{files_to_flay.join(" ")}`
9
+ @matches = output.chomp.split("\n\n").map{|m| m.split("\n ") }
10
+ end
11
+
12
+ def template_name
13
+ 'flay'
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,49 @@
1
+ module MetricFu::FlogReporter
2
+
3
+ SCORE_FORMAT = "%0.2f"
4
+
5
+ class InvalidFlog < RuntimeError
6
+ end
7
+
8
+ class Base
9
+ MODULE_NAME = "([A-Za-z]+)+"
10
+ METHOD_NAME = "#([a-z0-9]+_?)+\\??\\!?"
11
+ SCORE = "\\d+\\.\\d+"
12
+
13
+ METHOD_NAME_RE = Regexp.new("#{MODULE_NAME}#{METHOD_NAME}")
14
+ SCORE_RE = Regexp.new(SCORE)
15
+
16
+ METHOD_LINE_RE = Regexp.new("#{MODULE_NAME}#{METHOD_NAME}:\\s\\(#{SCORE}\\)")
17
+ OPERATOR_LINE_RE = Regexp.new("\\s+(#{SCORE}):\\s(.*)$")
18
+
19
+ class << self
20
+ def cycle(first_value, second_value, iteration)
21
+ return first_value if iteration % 2 == 0
22
+ return second_value
23
+ end
24
+
25
+ def parse(text)
26
+ score = text[/\w+ = (\d+\.\d+)/, 1]
27
+ return nil unless score
28
+ page = Page.new(score)
29
+
30
+ text.each_line do |method_line|
31
+ if METHOD_LINE_RE =~ method_line and
32
+ method_name = method_line[METHOD_NAME_RE] and
33
+ score = method_line[SCORE_RE]
34
+ page.scanned_methods << ScannedMethod.new(method_name, score)
35
+ end
36
+
37
+ if OPERATOR_LINE_RE =~ method_line and
38
+ operator = method_line[OPERATOR_LINE_RE, 2] and
39
+ score = method_line[SCORE_RE]
40
+ raise InvalidFlog if page.scanned_methods.empty?
41
+ page.scanned_methods.last.operators << Operator.new(score, operator)
42
+ end
43
+ end
44
+
45
+ page
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,39 @@
1
+ module MetricFu::FlogReporter
2
+ class Generator < MetricFu::Base::Generator
3
+ def generate_report
4
+ flog_hashes = []
5
+ Dir.glob("#{@base_dir}/**/*.txt").each do |filename|
6
+ begin
7
+ page = Base.parse(open(filename, "r") { |f| f.read })
8
+ rescue InvalidFlog
9
+ puts "Invalid flog for #{filename}"
10
+ next
11
+ end
12
+
13
+ next unless page
14
+
15
+ unless MetricFu::MD5Tracker.file_already_counted?(filename)
16
+ generate_page(filename, page)
17
+ end
18
+ flog_hashes << { :path => File.basename(filename, ".txt") + '.html',
19
+ :page => page }
20
+ end
21
+
22
+ generate_index(flog_hashes)
23
+ end
24
+
25
+ def generate_page(filename, page)
26
+ save_html(page.to_html, File.basename(filename, ".txt"))
27
+ end
28
+
29
+ # should be dynamically read from the class
30
+ def template_name
31
+ 'flog'
32
+ end
33
+
34
+ def generate_index(flog_hashes)
35
+ html = ERB.new(File.read(template_file)).result(binding)
36
+ save_html(html)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,10 @@
1
+ module MetricFu::FlogReporter
2
+ class Operator
3
+ attr_accessor :score, :operator
4
+
5
+ def initialize(score, operator)
6
+ @score = score.to_f
7
+ @operator = operator
8
+ end
9
+ end
10
+ end