Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 122 lines (99 sloc) 3.609 kB
ae294af @jamis Documentation updates/fixes for railties
jamis authored
1 class CodeStatistics #:nodoc:
ec019d7 @jeremy Ticket 2297 - rake stats clearly labels functional and unit tests
jeremy authored
2
1a91abe @jamis Add integration test support to app generation and testing
jamis authored
3 TEST_TYPES = %w(Units Functionals Unit\ tests Functional\ tests Integration\ tests)
ec019d7 @jeremy Ticket 2297 - rake stats clearly labels functional and unit tests
jeremy authored
4
db045db @dhh Initial
dhh authored
5 def initialize(*pairs)
6 @pairs = pairs
7 @statistics = calculate_statistics
8 @total = calculate_total if pairs.length > 1
9 end
10
11 def to_s
12 print_header
ebf2b12 @dhh Fixed "rake stats" to work with sub-directories in models and control…
dhh authored
13 @pairs.each { |pair| print_line(pair.first, @statistics[pair.first]) }
db045db @dhh Initial
dhh authored
14 print_splitter
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
15
db045db @dhh Initial
dhh authored
16 if @total
17 print_line("Total", @total)
18 print_splitter
19 end
ebf2b12 @dhh Fixed "rake stats" to work with sub-directories in models and control…
dhh authored
20
21 print_code_test_stats
db045db @dhh Initial
dhh authored
22 end
23
24 private
25 def calculate_statistics
63039b9 @miloops Fix typo and add sanity test for code statistics rake task.
miloops authored
26 Hash[@pairs.map{|pair| [pair.first, calculate_directory_statistics(pair.last)]}]
db045db @dhh Initial
dhh authored
27 end
28
a48b3f1 @arunagw Add code statistics for Javascript and
arunagw authored
29 def calculate_directory_statistics(directory, pattern = /.*\.(rb|js|coffee)$/)
db045db @dhh Initial
dhh authored
30 stats = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
31
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
32 Dir.foreach(directory) do |file_name|
624b118 @thedarkone Use shorter class-level File methods instead of going through File.stat.
thedarkone authored
33 if File.directory?(directory + "/" + file_name) and (/^\./ !~ file_name)
ebf2b12 @dhh Fixed "rake stats" to work with sub-directories in models and control…
dhh authored
34 newstats = calculate_directory_statistics(directory + "/" + file_name, pattern)
35 stats.each { |k, v| stats[k] += newstats[k] }
36 end
37
db045db @dhh Initial
dhh authored
38 next unless file_name =~ pattern
39
d78a702 @HeeL improved code stats calculation, check on multiline comments and rewr…
HeeL authored
40 comment_started = false
6831ab1 @rahul100885 Used block to make sure file get auto closed after use
rahul100885 authored
41
a48b3f1 @arunagw Add code statistics for Javascript and
arunagw authored
42 case file_name
43 when /.*\.js$/
44 comment_pattern = /^\s*\/\//
45 else
46 comment_pattern = /^\s*#/
47 end
48
6831ab1 @rahul100885 Used block to make sure file get auto closed after use
rahul100885 authored
49 File.open(directory + "/" + file_name) do |f|
50 while line = f.gets
51 stats["lines"] += 1
52 if(comment_started)
53 if line =~ /^=end/
54 comment_started = false
55 end
d78a702 @HeeL improved code stats calculation, check on multiline comments and rewr…
HeeL authored
56 next
6831ab1 @rahul100885 Used block to make sure file get auto closed after use
rahul100885 authored
57 else
58 if line =~ /^=begin/
59 comment_started = true
60 next
61 end
d78a702 @HeeL improved code stats calculation, check on multiline comments and rewr…
HeeL authored
62 end
6831ab1 @rahul100885 Used block to make sure file get auto closed after use
rahul100885 authored
63 stats["classes"] += 1 if line =~ /^\s*class\s+[_A-Z]/
64 stats["methods"] += 1 if line =~ /^\s*def\s+[_a-z]/
a48b3f1 @arunagw Add code statistics for Javascript and
arunagw authored
65 stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ comment_pattern
d78a702 @HeeL improved code stats calculation, check on multiline comments and rewr…
HeeL authored
66 end
db045db @dhh Initial
dhh authored
67 end
68 end
69
70 stats
71 end
72
73 def calculate_total
74 total = { "lines" => 0, "codelines" => 0, "classes" => 0, "methods" => 0 }
75 @statistics.each_value { |pair| pair.each { |k, v| total[k] += v } }
76 total
77 end
78
ebf2b12 @dhh Fixed "rake stats" to work with sub-directories in models and control…
dhh authored
79 def calculate_code
80 code_loc = 0
ec019d7 @jeremy Ticket 2297 - rake stats clearly labels functional and unit tests
jeremy authored
81 @statistics.each { |k, v| code_loc += v['codelines'] unless TEST_TYPES.include? k }
ebf2b12 @dhh Fixed "rake stats" to work with sub-directories in models and control…
dhh authored
82 code_loc
83 end
84
85 def calculate_tests
86 test_loc = 0
ec019d7 @jeremy Ticket 2297 - rake stats clearly labels functional and unit tests
jeremy authored
87 @statistics.each { |k, v| test_loc += v['codelines'] if TEST_TYPES.include? k }
ebf2b12 @dhh Fixed "rake stats" to work with sub-directories in models and control…
dhh authored
88 test_loc
89 end
90
db045db @dhh Initial
dhh authored
91 def print_header
92 print_splitter
93 puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
94 print_splitter
95 end
96
97 def print_splitter
98 puts "+----------------------+-------+-------+---------+---------+-----+-------+"
99 end
100
101 def print_line(name, statistics)
102 m_over_c = (statistics["methods"] / statistics["classes"]) rescue m_over_c = 0
103 loc_over_m = (statistics["codelines"] / statistics["methods"]) - 2 rescue loc_over_m = 0
104
68926dd @phiggins Remove useless conditional.
phiggins authored
105 puts "| #{name.ljust(20)} " +
db045db @dhh Initial
dhh authored
106 "| #{statistics["lines"].to_s.rjust(5)} " +
107 "| #{statistics["codelines"].to_s.rjust(5)} " +
108 "| #{statistics["classes"].to_s.rjust(7)} " +
109 "| #{statistics["methods"].to_s.rjust(7)} " +
110 "| #{m_over_c.to_s.rjust(3)} " +
111 "| #{loc_over_m.to_s.rjust(5)} |"
112 end
ebf2b12 @dhh Fixed "rake stats" to work with sub-directories in models and control…
dhh authored
113
114 def print_code_test_stats
115 code = calculate_code
116 tests = calculate_tests
117
2aac6d9 @dhh Flipped code-to-test ratio around to be more readable #468 [Scott Baron]
dhh authored
118 puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}"
ebf2b12 @dhh Fixed "rake stats" to work with sub-directories in models and control…
dhh authored
119 puts ""
120 end
548e5ba @vijaydev fix indentation
vijaydev authored
121 end
Something went wrong with that request. Please try again.