|
2 | 2 |
|
3 | 3 | module RailsStats |
4 | 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 | | - @code_total, @tests_total, @grand_total = calculate_totals |
| 5 | + def initialize(root_directory, opts = {}) |
| 6 | + @calculator = RailsStats::StatsCalculator.new(root_directory) |
| 7 | + @formatter = load_formatter(opts) |
19 | 8 | end |
20 | 9 |
|
21 | 10 | def to_s |
22 | | - print_header |
23 | | - sorted_keys = @statistics.keys.sort |
24 | | - sorted_keys.each { |key| print_line(key, @statistics[key]) } |
25 | | - print_splitter |
26 | | - |
27 | | - print_line("Code", @code_total) |
28 | | - print_line("Tests", @tests_total) |
29 | | - print_line("Total", @grand_total) |
30 | | - print_splitter |
31 | | - |
32 | | - print_code_test_stats |
| 11 | + @formatter.to_s |
33 | 12 | end |
34 | 13 |
|
35 | 14 | private |
36 | | - def calculate_key_concepts |
37 | | - # returns names of main things like models, controllers, services, etc |
38 | | - concepts = {} |
39 | | - app_projects.each do |project| |
40 | | - project.key_concepts.each do |key| |
41 | | - concepts[key] = true |
42 | | - end |
43 | | - end |
44 | | - |
45 | | - # TODO: maybe gem names? |
46 | | - |
47 | | - concepts.keys |
48 | | - end |
49 | | - |
50 | | - def calculate_projects |
51 | | - out = [] |
52 | | - out += app_projects |
53 | | - out += calculate_root_projects |
54 | | - out += calculate_gem_projects |
55 | | - out += calculate_spec_projects |
56 | | - out += calculate_test_projects |
57 | | - out += calculate_cucumber_projects |
58 | | - out |
59 | | - end |
60 | | - |
61 | | - def app_projects |
62 | | - @app_projects ||= calculate_app_projects |
63 | | - end |
64 | | - |
65 | | - def calculate_app_projects |
66 | | - apps = Util.calculate_projects(@root_directory, "**", "app", RAILS_APP_FOLDERS) |
67 | | - apps.collect do |root_path| |
68 | | - AppStatistics.new(root_path) |
69 | | - end |
70 | | - end |
71 | | - |
72 | | - def calculate_gem_projects |
73 | | - gems = Util.calculate_projects(@root_directory, "*", "**", "*.gemspec") |
74 | | - gems.collect do |root_path| |
75 | | - GemStatistics.new(root_path) |
76 | | - end |
77 | | - end |
78 | | - |
79 | | - def calculate_spec_projects |
80 | | - specs = Util.calculate_shared_projects("spec", @root_directory, "**", "spec", "**", "*_spec.rb") |
81 | | - specs.collect do |root_path| |
82 | | - SpecStatistics.new(root_path, @key_concepts) |
83 | | - end |
84 | | - end |
85 | | - |
86 | | - def calculate_test_projects |
87 | | - tests = Util.calculate_shared_projects("test", @root_directory, "**", "test", "**", "*_test.rb") |
88 | | - tests.collect do |root_path| |
89 | | - TestStatistics.new(root_path, @key_concepts) |
90 | | - end |
91 | | - end |
92 | | - |
93 | | - def calculate_root_projects |
94 | | - [RootStatistics.new(@root_directory)] |
95 | | - end |
96 | | - |
97 | | - def calculate_cucumber_projects |
98 | | - cukes = Util.calculate_projects(@root_directory, "**", "*.feature") |
99 | | - cukes.collect do |root_path| |
100 | | - CucumberStatistics.new(root_path) |
101 | | - end |
102 | | - end |
103 | 15 |
|
104 | | - def calculate_statistics |
105 | | - out = {} |
106 | | - @projects.each do |project| |
107 | | - project.statistics.each do |key, stats| |
108 | | - out[key] ||= CodeStatisticsCalculator.new(project.test) |
109 | | - out[key].add(stats) |
110 | | - end |
| 16 | + def load_formatter(opts = {}) |
| 17 | + if opts[:format] == "json" |
| 18 | + RailsStats::JSONFormatter.new(@calculator, opts) |
| 19 | + else |
| 20 | + RailsStats::ConsoleFormatter.new(@calculator, opts) |
111 | 21 | end |
112 | | - out |
113 | | - end |
114 | | - |
115 | | - def calculate_totals |
116 | | - # TODO: make this a single loop |
117 | | - code_total = @statistics.each_with_object(CodeStatisticsCalculator.new) do |pair, code_total| |
118 | | - code_total.add(pair.last) unless pair.last.test |
119 | | - end |
120 | | - |
121 | | - tests_total = @statistics.each_with_object(CodeStatisticsCalculator.new) do |pair, tests_total| |
122 | | - tests_total.add(pair.last) if pair.last.test |
123 | | - end |
124 | | - |
125 | | - grand_total = @statistics.each_with_object(CodeStatisticsCalculator.new) do |pair, total| |
126 | | - total.add(pair.last) |
127 | | - end |
128 | | - |
129 | | - [code_total, tests_total, grand_total] |
130 | | - end |
131 | | - |
132 | | - def print_header |
133 | | - print_splitter |
134 | | - puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |" |
135 | | - print_splitter |
136 | | - end |
137 | | - |
138 | | - def print_splitter |
139 | | - puts "+----------------------+---------+---------+---------+---------+-----+-------+" |
140 | | - end |
141 | | - |
142 | | - def print_line(name, statistics) |
143 | | - m_over_c = (statistics.methods / statistics.classes) rescue m_over_c = 0 |
144 | | - loc_over_m = (statistics.code_lines / statistics.methods) - 2 rescue loc_over_m = 0 |
145 | | - |
146 | | - puts "| #{name.ljust(20)} " \ |
147 | | - "| #{statistics.lines.to_s.rjust(7)} " \ |
148 | | - "| #{statistics.code_lines.to_s.rjust(7)} " \ |
149 | | - "| #{statistics.classes.to_s.rjust(7)} " \ |
150 | | - "| #{statistics.methods.to_s.rjust(7)} " \ |
151 | | - "| #{m_over_c.to_s.rjust(3)} " \ |
152 | | - "| #{loc_over_m.to_s.rjust(5)} |" |
153 | | - end |
154 | | - |
155 | | - def print_code_test_stats |
156 | | - code_to_test_ratio = @tests_total.code_lines.to_f / @code_total.code_lines |
157 | | - puts " Code LOC: #{@code_total.code_lines} Test LOC: #{@tests_total.code_lines} Code to Test Ratio: 1:#{sprintf("%.1f", code_to_test_ratio)}" |
158 | | - puts "" |
159 | 22 | end |
160 | 23 | end |
161 | 24 | end |
0 commit comments