Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/output json string #13

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
require "rails_stats"
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rake/testtask"

Rake::TestTask.new do |task|
task.libs.push "lib"
task.libs.push "test"
task.pattern = "test/**/*_test.rb"
end

task default: %i[test]
4 changes: 4 additions & 0 deletions lib/rails_stats/all.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
require 'rails_stats/stats_calculator'
require 'rails_stats/stats_formatter'
require 'rails_stats/json_formatter'
require 'rails_stats/console_formatter'
require 'rails_stats/inflector'
require 'rails_stats/code_statistics_calculator'
require 'rails_stats/util'
Expand Down
163 changes: 11 additions & 152 deletions lib/rails_stats/code_statistics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,162 +2,21 @@

module RailsStats
class CodeStatistics

RAILS_APP_FOLDERS = ['models',
'controllers',
'helpers',
'mailers',
'views',
'assets']

def initialize(root_directory)
@root_directory = root_directory
@key_concepts = calculate_key_concepts
@projects = calculate_projects
@statistics = calculate_statistics
@total = calculate_total
def initialize(root_directory, opts = {})
@calculator = RailsStats::StatsCalculator.new(root_directory)
@formatter = load_formatter(opts)
end

def to_s
print_header
@statistics.each { |key, stats| print_line(key, stats) }
print_splitter

if @total
print_line("Total", @total)
print_splitter
def load_formatter(opts = {})
if opts[:format] == "json"
RailsStats::JSONFormatter.new(@calculator, opts)
else
RailsStats::ConsoleFormatter.new(@calculator, opts)
end

print_code_test_stats
end

private
def calculate_key_concepts
# returns names of main things like models, controllers, services, etc
concepts = {}
app_projects.each do |project|
project.key_concepts.each do |key|
concepts[key] = true
end
end

# TODO: maybe gem names?

concepts.keys
end

def calculate_projects
out = []
out += app_projects
out += calculate_root_projects
out += calculate_gem_projects
out += calculate_spec_projects
out += calculate_test_projects
out += calculate_cucumber_projects
out
end

def app_projects
@app_projects ||= calculate_app_projects
end

def calculate_app_projects
apps = Util.calculate_projects(@root_directory, "**", "app", RAILS_APP_FOLDERS)
apps.collect do |root_path|
AppStatistics.new(root_path)
end
end

def calculate_gem_projects
gems = Util.calculate_projects(@root_directory, "*", "**", "*.gemspec")
gems.collect do |root_path|
GemStatistics.new(root_path)
end
end

def calculate_spec_projects
specs = Util.calculate_shared_projects("spec", @root_directory, "**", "spec", "**", "*_spec.rb")
specs.collect do |root_path|
SpecStatistics.new(root_path, @key_concepts)
end
end

def calculate_test_projects
tests = Util.calculate_shared_projects("test", @root_directory, "**", "test", "**", "*_test.rb")
tests.collect do |root_path|
TestStatistics.new(root_path, @key_concepts)
end
end

def calculate_root_projects
[RootStatistics.new(@root_directory)]
end

def calculate_cucumber_projects
cukes = Util.calculate_projects(@root_directory, "**", "*.feature")
cukes.collect do |root_path|
CucumberStatistics.new(root_path)
end
end

def calculate_statistics
out = {}
@projects.each do |project|
project.statistics.each do |key, stats|
out[key] ||= CodeStatisticsCalculator.new(project.test)
out[key].add(stats)
end
end
out
end

def calculate_total
@statistics.each_with_object(CodeStatisticsCalculator.new) do |pair, total|
total.add(pair.last)
end
end

def calculate_code
code_loc = 0
@statistics.each { |k, v| code_loc += v.code_lines unless v.test }
code_loc
end

def calculate_tests
test_loc = 0
@statistics.each { |k, v| test_loc += v.code_lines if v.test }
test_loc
end

def print_header
print_splitter
puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
print_splitter
end

def print_splitter
puts "+----------------------+-------+-------+---------+---------+-----+-------+"
end

def print_line(name, statistics)
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)} " \
"| #{statistics.lines.to_s.rjust(5)} " \
"| #{statistics.code_lines.to_s.rjust(5)} " \
"| #{statistics.classes.to_s.rjust(7)} " \
"| #{statistics.methods.to_s.rjust(7)} " \
"| #{m_over_c.to_s.rjust(3)} " \
"| #{loc_over_m.to_s.rjust(5)} |"
end

def print_code_test_stats
code = calculate_code
tests = calculate_tests

puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}"
puts ""
end
def to_s
@formatter.to_s
end
end
end
49 changes: 49 additions & 0 deletions lib/rails_stats/console_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module RailsStats
class ConsoleFormatter < StatsFormatter
def to_s
print_header
@statistics.each { |key, stats| print_line(key, stats) }
print_splitter

if @total
print_line("Total", @total)
print_splitter
end

print_code_test_stats
end

private

def print_header
print_splitter
puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
print_splitter
end

def print_splitter
puts "+----------------------+-------+-------+---------+---------+-----+-------+"
end

def print_line(name, statistics)
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)} " \
"| #{statistics.lines.to_s.rjust(5)} " \
"| #{statistics.code_lines.to_s.rjust(5)} " \
"| #{statistics.classes.to_s.rjust(7)} " \
"| #{statistics.methods.to_s.rjust(7)} " \
"| #{m_over_c.to_s.rjust(3)} " \
"| #{loc_over_m.to_s.rjust(5)} |"
end

def print_code_test_stats
code = calculator.code_loc
tests = calculator.test_loc

puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}"
puts ""
end
end
end
41 changes: 41 additions & 0 deletions lib/rails_stats/json_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "json"

module RailsStats
class JSONFormatter < StatsFormatter
def to_s
@result = @statistics.map { |key, stats| stat_hash(key, stats) }

if @total
@result << stat_hash("Total", @total).merge(code_test_hash)
end

puts @result.to_json
end

private

def code_test_hash
code = calculator.code_loc
tests = calculator.test_loc

{
"code_to_test_ratio" => "#{sprintf("%.1f", tests.to_f/code)}", "total" => true
}
end

def stat_hash(name, statistics)
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

{
"name" => name,
"lines" => statistics.lines.to_s,
"loc" => statistics.code_lines.to_s,
"classes" => statistics.classes.to_s,
"methods" => statistics.methods.to_s,
"m_over_c" => m_over_c.to_s,
"loc_over_m" => loc_over_m.to_s
}
end
end
end
Loading