indirect-metric_fu 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY ADDED
@@ -0,0 +1,79 @@
1
+ === MetricFu 0.8.0 / 2008-10-06
2
+
3
+ * Source Control Churn now supports git (thanks to Erik St Martin)
4
+ * Flog Results are sorted by Highest Flog Score
5
+ * Fix for a bunch of 'already initialized constant' warnings that metric_fu caused
6
+ * Fixing bug so the flog reporter can handle methods with digits in the name (thanks to Andy Gregorowicz)
7
+ * Internal Rake task now allows metric_fu to flog/churn itself
8
+
9
+ === MetricFu 0.7.6 / 2008-09-15
10
+
11
+ * CHURN_OPTIONS has become MetricFu::CHURN_OPTIONS
12
+ * SAIKURO_OPTIONS has become MetricFu::SAIKURO_OPTIONS
13
+ * Rcov now looks at test and specs
14
+ * Exclude gems and Library ruby code from rcov
15
+ * Fixed bug with churn start_date functionality (bad path)
16
+
17
+ === MetricFu 0.7.5 / 2008-09-12
18
+
19
+ * Flog can now flog any set of directories you like (see README).
20
+ * Saikuro can now look at any set of directories you like (see README).
21
+
22
+ === MetricFu 0.7.1 / 2008-09-12
23
+
24
+ * Fixed filename bugs pointed out by Bastien
25
+
26
+ === MetricFu 0.7.0 / 2008-09-11
27
+
28
+ * Merged in Sean Soper's changes to metric_fu.
29
+ * Metric_fu is now a gem.
30
+ * Flogging now uses a MD5 hash to figure out if it should re-flog a file (if it's changed)
31
+ * Flogging also has a cool new output screen(s)
32
+ * Thanks Sean!
33
+
34
+ === Metricks 0.4.2 / 2008-07-01
35
+
36
+ * Changed rcov output directory so that it is no longer 'coverage/unit' but just 'coverage' for better integration with CC.rb
37
+
38
+ === Metricks 0.4.1 / 2008-06-13
39
+
40
+ * Rcov tests now extend beyond one level depth directory by using RcovTask instead of the shell
41
+
42
+ === Metricks 0.4.0 / 2008-06-13
43
+
44
+ * Implementing functionality for use as a gem
45
+ * Added Rakefile to facilitate testing
46
+
47
+ === Metricks 0.3.0 / 2008-06-11
48
+
49
+ * Generated reports now open on darwin automatically
50
+ * Generated reports reside under tmp/metricks unless otherwise specified by ENV['CC_BUILD_ARTIFACTS']
51
+ * MD5Tracker works with Flog reports for speed optimization
52
+
53
+ === Metricks 0.2.0 / 2008-06-11
54
+
55
+ * Integrated use of base directory constant
56
+ * Have all reports automatically open in a browser if platform is darwin
57
+ * Namespaced under Metricks
58
+ * Dropped use of shell md5 command in favor of Ruby's Digest::MD5 libraries
59
+
60
+ === Metricks 0.1.0 / 2008-06-10
61
+
62
+ * Initial integration of metric_fu and my enhancements to flog
63
+ * Metrics are generated but are all over the place
64
+
65
+ === MetricFu 0.6.0 / 2008-05-11
66
+
67
+ * Add source control churn report
68
+
69
+ === MetricFu 0.5.1 / 2008-04-25
70
+
71
+ * Fixed bug with Saikuro report generation
72
+
73
+ === MetricFu 0.5.0 / 2008-04-25
74
+
75
+ * create MetricFu as a Rails Plugin
76
+ * Add Flog Report
77
+ * Add Coverage Report
78
+ * Add Saikuro Report
79
+ * Add Stats Report
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2008 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.
data/README ADDED
@@ -0,0 +1,101 @@
1
+ Version 0.8.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.8.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
27
+ rake metrics:all_with_migrate # Useful for continuous integration
28
+ rake metrics:churn # Which files change the most
29
+ rake metrics:coverage # Generate and open coverage report
30
+ rake metrics:coverage:clean # Delete aggregate coverage data.
31
+ rake metrics:coverage:clobber_do # Remove rcov products for do
32
+ rake metrics:coverage:do # RCov task to generate report
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:saikuro # A cyclomatic complexity report using Saikuro
41
+ rake metrics:stats # A stats report
42
+
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:
44
+
45
+ project.rake_task = 'metrics:all_with_migrate'
46
+ project.scheduler.polling_interval = 24.hours
47
+
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.
49
+
50
+
51
+ ****Notes on metrics:coverage****
52
+
53
+ When creating a coverage report, metric_fu runs all the tests in the test folder and specs in spec folder using Rcov.
54
+
55
+
56
+ ****Notes on metrics:saikuro****
57
+
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" }
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.
63
+
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"'}
66
+
67
+
68
+ ****Notes on metrics:flog****
69
+
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.
71
+
72
+ 'rake metrics:flog:custom' allows you to specify a custom set of directories to Flog (in your rakefile) like so:
73
+
74
+ MetricFu::DIRECTORIES_TO_FLOG = ['cms/app', 'cms/lib']
75
+
76
+
77
+ ****Notes on metrics:stats****
78
+
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.
80
+
81
+
82
+ ****Notes on metrics:churn****
83
+
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:
85
+
86
+ MetricFu::CHURN_OPTIONS = { :start_date => lambda{3.months.ago} }
87
+
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.
89
+
90
+ You can also change the minimum churn count like so:
91
+
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}
97
+
98
+
99
+ ****Thanks****
100
+
101
+ I'd like to thank the authors of Saikuro, Flog, Rcov, CruiseControl.rb, and Rails for creating such excellent open source products. Also Sean Soper, Michael Schubert, Kurtis Seebaldt, Toby Tripp, Paul Gross, and Chirdeep Shetty for their help and advice.
data/Rakefile ADDED
@@ -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,127 @@
1
+ module MetricFu
2
+ class Churn
3
+ class << self
4
+ def generate_report(output_dir, options)
5
+ date_range, minimum_churn_count, source_control_type = churn_options(options)
6
+
7
+ changes = parse_log_for_changes(source_control_type, date_range)
8
+ changes.reject! {|file, change_count| change_count < minimum_churn_count}
9
+ write_churn_file(changes, output_dir)
10
+ end
11
+
12
+ private
13
+
14
+ def parse_log_for_changes(source_control_type, date_range)
15
+ changes = {}
16
+
17
+ logs = get_logs(source_control_type, date_range)
18
+ logs.each do |line|
19
+ changes[line] ? changes[line] += 1 : changes[line] = 1
20
+ end
21
+
22
+ changes
23
+ end
24
+
25
+ def get_logs(source_control_type, date_range)
26
+ if source_control_type == :git
27
+ `git log #{date_range} --name-only --pretty=format:`.split(/\n/).reject{|line| line == ""}
28
+ else
29
+ `svn log #{date_range} --verbose`.split(/\n/).map { |line| clean_up_svn_line(line) }.compact
30
+ end
31
+ end
32
+
33
+ def clean_up_svn_line(line)
34
+ m = line.match(/\W*[A,M]\W+(\/.*)\b/)
35
+ m ? m[1] : nil
36
+ end
37
+
38
+ def churn_options(options)
39
+ if File.exist?(".git")
40
+ scm = :git
41
+ elsif File.exist?(".svn")
42
+ scm = :svn
43
+ end
44
+ raise "Churning requires a subversion or git repo" unless scm
45
+
46
+ if options[:start_date]
47
+ require 'activesupport'
48
+ if scm == :git
49
+ date_range = "--after=#{options[:start_date].call.strftime('%Y-%m-%d')}"
50
+ else
51
+ date_range = "--revision {#{options[:start_date].call.strftime('%Y-%m-%d')}}:{#{Time.now.strftime('%Y-%m-%d')}}"
52
+ end
53
+ end
54
+
55
+ minimum_churn_count = options[:minimum_churn_count] ? options[:minimum_churn_count] : 5
56
+ return date_range, minimum_churn_count, scm
57
+ end
58
+
59
+ def write_churn_file(changes, output_dir)
60
+ FileUtils.mkdir_p(output_dir, :verbose => false) unless File.directory?(output_dir)
61
+ File.open("#{output_dir}/index.html", "w+") do |file|
62
+ file << CHURN_FILE_BEGINING
63
+ changes.to_a.sort {|x,y| y[1] <=> x[1]}.each do |change|
64
+ file << "<tr><td>#{change[0]}</td><td class='warning'>#{change[1]}</td></tr>\n"
65
+ end
66
+ file << CHURN_FILE_END
67
+ end
68
+ end
69
+ end
70
+
71
+ CHURN_FILE_BEGINING = <<-EOS
72
+ <html><head><title>Source Control Churn Results</title></head>
73
+ <style>
74
+ body {
75
+ margin: 20px;
76
+ padding: 0;
77
+ font-size: 12px;
78
+ font-family: bitstream vera sans, verdana, arial, sans serif;
79
+ background-color: #efefef;
80
+ }
81
+
82
+ table {
83
+ border-collapse: collapse;
84
+ /*border-spacing: 0;*/
85
+ border: 1px solid #666;
86
+ background-color: #fff;
87
+ margin-bottom: 20px;
88
+ }
89
+
90
+ table, th, th+th, td, td+td {
91
+ border: 1px solid #ccc;
92
+ }
93
+
94
+ table th {
95
+ font-size: 12px;
96
+ color: #fc0;
97
+ padding: 4px 0;
98
+ background-color: #336;
99
+ }
100
+
101
+ th, td {
102
+ padding: 4px 10px;
103
+ }
104
+
105
+ td {
106
+ font-size: 13px;
107
+ }
108
+
109
+ .warning {
110
+ background-color: yellow;
111
+ }
112
+ </style>
113
+
114
+ <body>
115
+ <h1>Source Control Churn Results</h1>
116
+ <table width="100%" border="1">
117
+ <tr><th>File Path</th><th>Times Changed</th></tr>
118
+ EOS
119
+
120
+ CHURN_FILE_END = <<-EOS
121
+ </table>
122
+ </body>
123
+ </html>
124
+ EOS
125
+
126
+ end
127
+ end
@@ -0,0 +1,40 @@
1
+ require 'erb'
2
+ require 'fileutils'
3
+
4
+ module MetricFu
5
+ class Flay
6
+
7
+ class << self
8
+ def generate_report(options = {})
9
+ puts Dir.pwd
10
+ files_to_flay = MetricFu::CODE_DIRS.map{|dir| Dir[File.join(dir, "**/*.rb")] }
11
+ output = `flay #{files_to_flay.join(" ")}`
12
+ @matches = output.chomp.split("\n\n").map{|m| m.split("\n ") }
13
+ write_html_output("flay")
14
+ end
15
+
16
+ private
17
+ def write_html_output(template_name)
18
+ output_dir = File.join(MetricFu::BASE_DIRECTORY, template_name)
19
+ FileUtils.mkdir_p(output_dir, :verbose => false)
20
+
21
+ template_file = File.join(MetricFu::TEMPLATE_DIR, "#{template_name}.html.erb")
22
+ html = ERB.new(File.read(template_file)).result(binding)
23
+
24
+ File.open(File.join(output_dir, 'index.html'), "w"){|f| f << html }
25
+ FileUtils.cp(File.join(MetricFu::TEMPLATE_DIR, "#{template_name}.css"), output_dir)
26
+ `open #{output_dir}/index.html`
27
+ end
28
+
29
+ def link_to_filename(name, line = nil)
30
+ filename = File.expand_path(name)
31
+ if PLATFORM['darwin']
32
+ %{<a href="txmt://open/?url=file://#{filename}&line=#{line}">#{name}</a>}
33
+ else
34
+ %{<a href="file://#{filename}">#{name}</a>}
35
+ end
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,58 @@
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 load_css(css_file = nil)
26
+ filepath = css_file || File.join(File.dirname(__FILE__), 'flog_reporter.css')
27
+ css = ""
28
+ file = File.open(filepath, "r")
29
+ file.each_line { |line| css << line }
30
+ file.close
31
+ css
32
+ end
33
+
34
+ def parse(text)
35
+ score = text[/\w+ = (\d+\.\d+)/, 1]
36
+ return nil unless score
37
+ page = Page.new(score)
38
+
39
+ text.each_line do |method_line|
40
+ if METHOD_LINE_RE =~ method_line and
41
+ method_name = method_line[METHOD_NAME_RE] and
42
+ score = method_line[SCORE_RE]
43
+ page.scanned_methods << ScannedMethod.new(method_name, score)
44
+ end
45
+
46
+ if OPERATOR_LINE_RE =~ method_line and
47
+ operator = method_line[OPERATOR_LINE_RE, 2] and
48
+ score = method_line[SCORE_RE]
49
+ raise InvalidFlog if page.scanned_methods.empty?
50
+ page.scanned_methods.last.operators << Operator.new(score, operator)
51
+ end
52
+ end
53
+
54
+ page
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,71 @@
1
+ module MetricFu::FlogReporter
2
+ class Generator
3
+ class << self
4
+ def generate_report(base_dir)
5
+ flog_hashes = []
6
+ Dir.glob("#{base_dir}/**/*.txt").each do |filename|
7
+ content = ""
8
+ File.open(filename, "r").each_line do |file|
9
+ content << file
10
+ end
11
+
12
+ begin
13
+ page = Base.parse(content)
14
+ rescue InvalidFlog
15
+ puts "Invalid flog for #{filename}"
16
+ next
17
+ end
18
+
19
+ next unless page
20
+
21
+ if MetricFu::MD5Tracker.file_already_counted?(filename)
22
+ flog_hashes << {
23
+ :page => page,
24
+ :path => filename.sub('.txt', '.html').sub("#{base_dir}/", "")
25
+ }
26
+ else
27
+ flog_hashes << generate_page(filename, page, base_dir)
28
+ end
29
+ end
30
+
31
+ generate_index(flog_hashes, base_dir)
32
+ end
33
+
34
+ def generate_page(filename, page, base_dir)
35
+ html_file = File.new(filename.gsub(/\.txt/, '.html'), "w")
36
+ html_file.puts page.to_html
37
+ html_file.close
38
+ return { :path => html_file.path.sub("#{base_dir}/", ''),
39
+ :page => page }
40
+ end
41
+
42
+ def generate_index(flog_hashes, base_dir)
43
+ html = "<html><head><title>Flog Reporter</title><style>"
44
+ html << Base.load_css
45
+ html << "</style></head><body>"
46
+ html << "<p><strong>Flogged files</strong></p>\n"
47
+ html << "<p>Generated on #{Time.now.localtime} with <a href='http://ruby.sadi.st/Flog.html'>flog</a></p>\n"
48
+ html << "<table class='report'>\n"
49
+ html << "<tr><th>File</th><th>Total score</th><th>Methods</th><th>Average score</th><th>Highest score</th></tr>"
50
+ count = 0
51
+ flog_hashes.sort {|x,y| y[:page].highest_score <=> x[:page].highest_score }.each do |flog_hash|
52
+ html << <<-EOF
53
+ <tr class='#{Base.cycle("light", "dark", count)}'>
54
+ <td><a href='#{flog_hash[:path]}'>#{flog_hash[:path].sub('.html', '.rb')}</a></td>
55
+ <td class='score'>#{sprintf(SCORE_FORMAT, flog_hash[:page].score)}</td>
56
+ <td class='score'>#{flog_hash[:page].scanned_methods.length}</td>
57
+ <td class='score'>#{sprintf(SCORE_FORMAT, flog_hash[:page].average_score)}</td>
58
+ <td class='score'>#{sprintf(SCORE_FORMAT, flog_hash[:page].highest_score)}</td>
59
+ </tr>
60
+ EOF
61
+ count += 1
62
+ end
63
+ html << "</table>\n"
64
+ html << "</body></html>\n"
65
+ index = File.new("#{base_dir}/index.html", "w")
66
+ index.puts html
67
+ index.close
68
+ end
69
+ end
70
+ end
71
+ 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
@@ -0,0 +1,36 @@
1
+ module MetricFu::FlogReporter
2
+ class Page
3
+ attr_accessor :score, :scanned_methods
4
+
5
+ def initialize(score, scanned_methods = [])
6
+ @score = score.to_f
7
+ @scanned_methods = scanned_methods
8
+ end
9
+
10
+ def to_html
11
+ output = "<html><head><style>"
12
+ output << Base.load_css
13
+ output << "</style></head><body>"
14
+ output << "Score: #{score}\n"
15
+ scanned_methods.each do |sm|
16
+ output << sm.to_html
17
+ end
18
+ output << "</body></html>"
19
+ output
20
+ end
21
+
22
+ def average_score
23
+ sum = 0
24
+ scanned_methods.each do |m|
25
+ sum += m.score
26
+ end
27
+ sum / scanned_methods.length
28
+ end
29
+
30
+ def highest_score
31
+ scanned_methods.inject(0) do |highest, m|
32
+ m.score > highest ? m.score : highest
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,28 @@
1
+ module MetricFu::FlogReporter
2
+ class ScannedMethod
3
+ attr_accessor :name, :score, :operators
4
+
5
+ def initialize(name, score, operators = [])
6
+ @name = name
7
+ @score = score.to_f
8
+ @operators = operators
9
+ end
10
+
11
+ def to_html
12
+ output = "<p><strong>#{name} (#{score})</strong></p>\n"
13
+ output << "<table>\n"
14
+ output << "<tr><th>Score</th><th>Operator</th></tr>\n"
15
+ count = 0
16
+ operators.each do |operator|
17
+ output << <<-EOF
18
+ <tr class='#{Base.cycle("light", "dark", count)}'>
19
+ <td class='score'>#{sprintf(SCORE_FORMAT, operator.score)}</td>
20
+ <td class='score'>#{operator.operator}</td>
21
+ </tr>
22
+ EOF
23
+ count += 1
24
+ end
25
+ output << "</table>\n\n"
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,5 @@
1
+ require File.join(File.dirname(__FILE__), 'flog_reporter', 'base')
2
+ require File.join(File.dirname(__FILE__), 'flog_reporter', 'page')
3
+ require File.join(File.dirname(__FILE__), 'flog_reporter', 'scanned_method')
4
+ require File.join(File.dirname(__FILE__), 'flog_reporter', 'operator')
5
+ require File.join(File.dirname(__FILE__), 'flog_reporter', 'generator')
@@ -0,0 +1,52 @@
1
+ require 'digest/md5'
2
+ require 'fileutils'
3
+
4
+ module MetricFu
5
+ class MD5Tracker
6
+
7
+ @@unchanged_md5s = []
8
+
9
+ class << self
10
+ def md5_dir(path_to_file, base_dir)
11
+ File.join(base_dir,
12
+ path_to_file.split('/')[0..-2].join('/'))
13
+ end
14
+
15
+ def md5_file(path_to_file, base_dir)
16
+ File.join(md5_dir(path_to_file, base_dir),
17
+ path_to_file.split('/').last.sub(/\.[a-z]+/, '.md5'))
18
+ end
19
+
20
+ def track(path_to_file, base_dir)
21
+ md5 = Digest::MD5.hexdigest(File.read(path_to_file))
22
+ FileUtils.mkdir_p(md5_dir(path_to_file, base_dir), :verbose => false)
23
+ f = File.new(md5_file(path_to_file, base_dir), "w")
24
+ f.puts(md5)
25
+ f.close
26
+ md5
27
+ end
28
+
29
+ def file_changed?(path_to_file, base_dir)
30
+ orig_md5_file = md5_file(path_to_file, base_dir)
31
+ return !!track(path_to_file, base_dir) unless File.exist?(orig_md5_file)
32
+
33
+ current_md5 = ""
34
+ file = File.open(orig_md5_file, 'r')
35
+ file.each_line { |line| current_md5 << line }
36
+ file.close
37
+ current_md5.chomp!
38
+
39
+ new_md5 = Digest::MD5.hexdigest(File.read(path_to_file))
40
+ new_md5.chomp!
41
+
42
+ @@unchanged_md5s << path_to_file if new_md5 == current_md5
43
+
44
+ return new_md5 != current_md5
45
+ end
46
+
47
+ def file_already_counted?(path_to_file)
48
+ return @@unchanged_md5s.include?(path_to_file)
49
+ end
50
+ end
51
+ end
52
+ end