1
1
# railties/lib/rails/code_statistics.rb
2
2
3
- require 'rails_stats/code_statistics_calculator'
4
-
5
3
module RailsStats
6
- class CodeStatistics #:nodoc:
7
-
8
- TEST_TYPES = [ 'Controller tests' ,
9
- 'Helper tests' ,
10
- 'Model tests' ,
11
- 'Mailer tests' ,
12
- 'Integration tests' ,
13
- 'Request tests' ,
14
- 'Library tests' ,
15
- 'Cucumber tests' ,
16
- 'Functional tests (old)' ,
17
- 'Unit tests (old)' ]
18
-
19
- def initialize ( *pairs )
20
- @pairs = pairs
21
- @statistics = calculate_statistics
22
- @total = calculate_total if pairs . length > 1
4
+ class CodeStatistics
5
+
6
+ RAILS_APP_FOLDERS = [ 'models' ,
7
+ 'controllers' ,
8
+ 'helpers' ,
9
+ 'mailers' ,
10
+ 'views' ,
11
+ 'assets' ]
12
+
13
+ def initialize ( root_directory )
14
+ @root_directory = root_directory
15
+ @key_concepts = calculate_key_concepts
16
+ @projects = calculate_projects
17
+ @statistics = calculate_statistics
18
+ @total = calculate_total
23
19
end
24
20
25
21
def to_s
26
22
print_header
27
- @pairs . each { |pair | print_line ( pair . first , @statistics [ pair . first ] ) }
23
+ @statistics . each { |key , stats | print_line ( key , stats ) }
28
24
print_splitter
29
25
30
26
if @total
@@ -36,26 +32,80 @@ def to_s
36
32
end
37
33
38
34
private
39
- def calculate_statistics
40
- Hash [ @pairs . map { |pair | [ pair . first , calculate_directory_statistics ( pair . last ) ] } ]
35
+ def calculate_key_concepts
36
+ # returns names of main things like models, controllers, services, etc
37
+ concepts = { }
38
+ app_projects . each do |project |
39
+ project . key_concepts . each do |key |
40
+ concepts [ key ] = true
41
+ end
42
+ end
43
+
44
+ # TODO: maybe gem names?
45
+
46
+ concepts . keys
41
47
end
42
48
43
- def calculate_directory_statistics ( directory , pattern = /.*\. (rb|js|coffee|feature)$/ )
44
- stats = CodeStatisticsCalculator . new
49
+ def calculate_projects
50
+ out = [ ]
51
+ out += app_projects
52
+ out += calculate_root_projects
53
+ out += calculate_gem_projects
54
+ out += calculate_spec_projects
55
+ out += calculate_test_projects
56
+ out += calculate_cucumber_projects
57
+ out
58
+ end
45
59
46
- Dir . foreach ( directory ) do |file_name |
47
- path = "#{ directory } /#{ file_name } "
60
+ def app_projects
61
+ @app_projects ||= calculate_app_projects
62
+ end
48
63
49
- if File . directory? ( path ) && ( /^\. / !~ file_name )
50
- stats . add ( calculate_directory_statistics ( path , pattern ) )
51
- end
64
+ def calculate_app_projects
65
+ apps = Util . calculate_projects ( @root_directory , "**" , "app" , RAILS_APP_FOLDERS )
66
+ apps . collect do |root_path |
67
+ AppStatistics . new ( root_path )
68
+ end
69
+ end
70
+
71
+ def calculate_gem_projects
72
+ gems = Util . calculate_projects ( @root_directory , "**" , "*.gemspec" )
73
+ gems . collect do |root_path |
74
+ GemStatistics . new ( root_path )
75
+ end
76
+ end
77
+
78
+ def calculate_spec_projects
79
+ specs = Util . calculate_projects ( @root_directory , "**" , "spec" , "spec_helper.rb" )
80
+ specs . collect do |root_path |
81
+ SpecStatistics . new ( root_path , @key_concepts )
82
+ end
83
+ end
84
+
85
+ def calculate_test_projects
86
+ [ ] # TODO: test unit
87
+ end
52
88
53
- next unless file_name =~ pattern
89
+ def calculate_root_projects
90
+ [ RootStatistics . new ( @root_directory ) ]
91
+ end
54
92
55
- stats . add_by_file_path ( path )
93
+ def calculate_cucumber_projects
94
+ cukes = Util . calculate_projects ( @root_directory , "**" , "*.feature" )
95
+ cukes . collect do |root_path |
96
+ CucumberStatistics . new ( root_path )
56
97
end
98
+ end
57
99
58
- stats
100
+ def calculate_statistics
101
+ out = { }
102
+ @projects . each do |project |
103
+ project . statistics . each do |key , stats |
104
+ out [ key ] ||= CodeStatisticsCalculator . new ( project . test )
105
+ out [ key ] . add ( stats )
106
+ end
107
+ end
108
+ out
59
109
end
60
110
61
111
def calculate_total
@@ -66,13 +116,13 @@ def calculate_total
66
116
67
117
def calculate_code
68
118
code_loc = 0
69
- @statistics . each { |k , v | code_loc += v . code_lines unless TEST_TYPES . include? k }
119
+ @statistics . each { |k , v | code_loc += v . code_lines unless v . test }
70
120
code_loc
71
121
end
72
122
73
123
def calculate_tests
74
124
test_loc = 0
75
- @statistics . each { |k , v | test_loc += v . code_lines if TEST_TYPES . include? k }
125
+ @statistics . each { |k , v | test_loc += v . code_lines if v . test }
76
126
test_loc
77
127
end
78
128
0 commit comments