2005-10-15 23:00:44 -04:00
|
|
|
class CodeStatistics #:nodoc:
|
2005-09-27 18:09:21 -04:00
|
|
|
|
2006-02-28 13:57:32 -05:00
|
|
|
TEST_TYPES = %w(Units Functionals Unit\ tests Functional\ tests Integration\ tests)
|
2005-09-27 18:09:21 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def initialize(*pairs)
|
|
|
|
@pairs = pairs
|
|
|
|
@statistics = calculate_statistics
|
|
|
|
@total = calculate_total if pairs.length > 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
print_header
|
2004-12-22 08:08:26 -05:00
|
|
|
@pairs.each { |pair| print_line(pair.first, @statistics[pair.first]) }
|
2004-11-23 20:04:44 -05:00
|
|
|
print_splitter
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
if @total
|
|
|
|
print_line("Total", @total)
|
|
|
|
print_splitter
|
|
|
|
end
|
2004-12-22 08:08:26 -05:00
|
|
|
|
|
|
|
print_code_test_stats
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def calculate_statistics
|
2010-09-24 09:06:35 -04:00
|
|
|
Hash[@pairs.map{|pair| [pair.first, calculate_directory_statistics(pair.last)]}]
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2005-09-27 19:31:32 -04:00
|
|
|
def calculate_directory_statistics(directory, pattern = /.*\.rb$/)
|
2004-11-23 20:04:44 -05:00
|
|
|
stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
|
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
Dir.foreach(directory) do |file_name|
|
2011-07-25 11:01:30 -04:00
|
|
|
if File.directory?(directory + "/" + file_name) and (/^\./ !~ file_name)
|
2004-12-22 08:08:26 -05:00
|
|
|
newstats = calculate_directory_statistics(directory + "/" + file_name, pattern)
|
|
|
|
stats.each { |k, v| stats[k] += newstats[k] }
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
next unless file_name =~ pattern
|
|
|
|
|
2011-11-23 07:10:30 -05:00
|
|
|
comment_started = false
|
2012-01-30 04:11:22 -05:00
|
|
|
|
|
|
|
File.open(directory + "/" + file_name) do |f|
|
|
|
|
while line = f.gets
|
|
|
|
stats["lines"] += 1
|
|
|
|
if(comment_started)
|
|
|
|
if line =~ /^=end/
|
|
|
|
comment_started = false
|
|
|
|
end
|
2011-11-23 07:10:30 -05:00
|
|
|
next
|
2012-01-30 04:11:22 -05:00
|
|
|
else
|
|
|
|
if line =~ /^=begin/
|
|
|
|
comment_started = true
|
|
|
|
next
|
|
|
|
end
|
2011-11-23 07:10:30 -05:00
|
|
|
end
|
2012-01-30 04:11:22 -05:00
|
|
|
stats["classes"] += 1 if line =~ /^\s*class\s+[_A-Z]/
|
|
|
|
stats["methods"] += 1 if line =~ /^\s*def\s+[_a-z]/
|
|
|
|
stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ /^\s*#/
|
2011-11-23 07:10:30 -05:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
stats
|
|
|
|
end
|
|
|
|
|
|
|
|
def calculate_total
|
|
|
|
total = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
|
|
|
|
@statistics.each_value { |pair| pair.each { |k, v| total[k] += v } }
|
|
|
|
total
|
|
|
|
end
|
|
|
|
|
2004-12-22 08:08:26 -05:00
|
|
|
def calculate_code
|
|
|
|
code_loc = 0
|
2005-09-27 18:09:21 -04:00
|
|
|
@statistics.each { |k, v| code_loc += v['codelines'] unless TEST_TYPES.include? k }
|
2004-12-22 08:08:26 -05:00
|
|
|
code_loc
|
|
|
|
end
|
|
|
|
|
|
|
|
def calculate_tests
|
|
|
|
test_loc = 0
|
2005-09-27 18:09:21 -04:00
|
|
|
@statistics.each { |k, v| test_loc += v['codelines'] if TEST_TYPES.include? k }
|
2004-12-22 08:08:26 -05:00
|
|
|
test_loc
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def print_header
|
|
|
|
print_splitter
|
|
|
|
puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
|
|
|
|
print_splitter
|
|
|
|
end
|
|
|
|
|
|
|
|
def print_splitter
|
|
|
|
puts "+----------------------+-------+-------+---------+---------+-----+-------+"
|
|
|
|
end
|
|
|
|
|
|
|
|
def print_line(name, statistics)
|
|
|
|
m_over_c = (statistics["methods"] / statistics["classes"]) rescue m_over_c = 0
|
|
|
|
loc_over_m = (statistics["codelines"] / statistics["methods"]) - 2 rescue loc_over_m = 0
|
|
|
|
|
2012-03-25 21:20:40 -04:00
|
|
|
puts "| #{name.ljust(20)} " +
|
2004-11-23 20:04:44 -05:00
|
|
|
"| #{statistics["lines"].to_s.rjust(5)} " +
|
|
|
|
"| #{statistics["codelines"].to_s.rjust(5)} " +
|
|
|
|
"| #{statistics["classes"].to_s.rjust(7)} " +
|
|
|
|
"| #{statistics["methods"].to_s.rjust(7)} " +
|
|
|
|
"| #{m_over_c.to_s.rjust(3)} " +
|
|
|
|
"| #{loc_over_m.to_s.rjust(5)} |"
|
|
|
|
end
|
2004-12-22 08:08:26 -05:00
|
|
|
|
|
|
|
def print_code_test_stats
|
|
|
|
code = calculate_code
|
|
|
|
tests = calculate_tests
|
|
|
|
|
2005-01-15 13:19:51 -05:00
|
|
|
puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}"
|
2004-12-22 08:08:26 -05:00
|
|
|
puts ""
|
|
|
|
end
|
2011-05-25 14:17:31 -04:00
|
|
|
end
|