2013-01-24 15:19:51 -05:00
|
|
|
require 'rails/code_statistics_calculator'
|
|
|
|
|
2005-10-15 23:00:44 -04:00
|
|
|
class CodeStatistics #:nodoc:
|
2005-09-27 18:09:21 -04:00
|
|
|
|
2012-12-09 00:04:36 -05:00
|
|
|
TEST_TYPES = ['Controller tests',
|
|
|
|
'Helper tests',
|
|
|
|
'Model tests',
|
|
|
|
'Mailer tests',
|
2014-11-05 08:17:37 -05:00
|
|
|
'Job tests',
|
2015-01-09 04:54:11 -05:00
|
|
|
'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
|
|
|
|
|
2012-05-31 06:45:35 -04:00
|
|
|
def calculate_directory_statistics(directory, pattern = /.*\.(rb|js|coffee)$/)
|
2013-01-24 15:19:51 -05:00
|
|
|
stats = CodeStatisticsCalculator.new
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2010-08-14 01:13:00 -04:00
|
|
|
Dir.foreach(directory) do |file_name|
|
2013-01-24 15:19:51 -05:00
|
|
|
path = "#{directory}/#{file_name}"
|
|
|
|
|
|
|
|
if File.directory?(path) && (/^\./ !~ file_name)
|
|
|
|
stats.add(calculate_directory_statistics(path, pattern))
|
2015-04-09 03:48:01 -04:00
|
|
|
elsif file_name =~ pattern
|
|
|
|
stats.add_by_file_path(path)
|
2004-12-22 08:08:26 -05:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
stats
|
|
|
|
end
|
|
|
|
|
|
|
|
def calculate_total
|
2013-01-24 15:19:51 -05:00
|
|
|
@statistics.each_with_object(CodeStatisticsCalculator.new) do |pair, total|
|
|
|
|
total.add(pair.last)
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2004-12-22 08:08:26 -05:00
|
|
|
def calculate_code
|
|
|
|
code_loc = 0
|
2013-01-24 15:19:51 -05:00
|
|
|
@statistics.each { |k, v| code_loc += v.code_lines unless TEST_TYPES.include? k }
|
2004-12-22 08:08:26 -05:00
|
|
|
code_loc
|
|
|
|
end
|
|
|
|
|
|
|
|
def calculate_tests
|
|
|
|
test_loc = 0
|
2013-01-24 15:19:51 -05:00
|
|
|
@statistics.each { |k, v| test_loc += v.code_lines 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
|
2015-03-01 03:42:37 -05:00
|
|
|
puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
|
2004-11-23 20:04:44 -05:00
|
|
|
print_splitter
|
|
|
|
end
|
|
|
|
|
|
|
|
def print_splitter
|
2015-03-01 03:42:37 -05:00
|
|
|
puts "+----------------------+--------+--------+---------+---------+-----+-------+"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def print_line(name, statistics)
|
2013-01-24 15:19:51 -05:00
|
|
|
m_over_c = (statistics.methods / statistics.classes) rescue m_over_c = 0
|
|
|
|
loc_over_m = (statistics.code_lines / statistics.methods) - 2 rescue loc_over_m = 0
|
|
|
|
|
|
|
|
puts "| #{name.ljust(20)} " \
|
2015-03-01 03:42:37 -05:00
|
|
|
"| #{statistics.lines.to_s.rjust(6)} " \
|
|
|
|
"| #{statistics.code_lines.to_s.rjust(6)} " \
|
2013-01-24 15:19:51 -05:00
|
|
|
"| #{statistics.classes.to_s.rjust(7)} " \
|
|
|
|
"| #{statistics.methods.to_s.rjust(7)} " \
|
|
|
|
"| #{m_over_c.to_s.rjust(3)} " \
|
2004-11-23 20:04:44 -05:00
|
|
|
"| #{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
|