2016-08-06 13:15:47 -04:00
|
|
|
require "rails/code_statistics_calculator"
|
|
|
|
require "active_support/core_ext/enumerable"
|
2013-01-24 15:19:51 -05:00
|
|
|
|
2005-10-15 23:00:44 -04:00
|
|
|
class CodeStatistics #:nodoc:
|
2016-08-06 13:15:47 -04:00
|
|
|
TEST_TYPES = ["Controller tests",
|
|
|
|
"Helper tests",
|
|
|
|
"Model tests",
|
|
|
|
"Mailer tests",
|
|
|
|
"Job tests",
|
|
|
|
"Integration tests"]
|
2005-09-27 18:09:21 -04:00
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
HEADERS = { lines: " Lines", code_lines: " LOC", classes: "Classes", methods: "Methods" }
|
2016-02-04 04:12:28 -05: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
|
2016-08-16 03:30:11 -04:00
|
|
|
Hash[@pairs.map { |pair| [pair.first, calculate_directory_statistics(pair.last)] }]
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2016-01-11 02:42:23 -05:00
|
|
|
def calculate_directory_statistics(directory, pattern = /^(?!\.).*?\.(rb|js|coffee|rake)$/)
|
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
|
|
|
|
|
2016-02-04 04:12:28 -05:00
|
|
|
def width_for(label)
|
2016-08-16 03:30:11 -04:00
|
|
|
[@statistics.values.sum { |s| s.send(label) }.to_s.size, HEADERS[label].length].max
|
2016-02-04 04:12:28 -05:00
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def print_header
|
|
|
|
print_splitter
|
2016-08-06 13:15:47 -04:00
|
|
|
print "| Name "
|
2016-02-04 04:12:28 -05:00
|
|
|
HEADERS.each do |k, v|
|
|
|
|
print " | #{v.rjust(width_for(k))}"
|
|
|
|
end
|
2016-08-06 13:15:47 -04:00
|
|
|
puts " | M/C | LOC/M |"
|
2004-11-23 20:04:44 -05:00
|
|
|
print_splitter
|
|
|
|
end
|
|
|
|
|
|
|
|
def print_splitter
|
2016-08-06 13:15:47 -04:00
|
|
|
print "+----------------------"
|
2016-02-04 04:12:28 -05:00
|
|
|
HEADERS.each_key do |k|
|
|
|
|
print "+#{'-' * (width_for(k) + 2)}"
|
|
|
|
end
|
2016-08-06 13:15:47 -04: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
|
|
|
|
|
2016-02-04 04:12:28 -05:00
|
|
|
print "| #{name.ljust(20)} "
|
|
|
|
HEADERS.each_key do |k|
|
|
|
|
print "| #{statistics.send(k).to_s.rjust(width_for(k))} "
|
|
|
|
end
|
|
|
|
puts "| #{m_over_c.to_s.rjust(3)} | #{loc_over_m.to_s.rjust(5)} |"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2004-12-22 08:08:26 -05:00
|
|
|
|
|
|
|
def print_code_test_stats
|
|
|
|
code = calculate_code
|
|
|
|
tests = calculate_tests
|
|
|
|
|
2016-10-28 23:05:58 -04: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
|