Skip to content

Commit

Permalink
Merge pull request rails#52701 from p8/railties/namespace-code-statis…
Browse files Browse the repository at this point in the history
…tics

Namespace CodeStatistics and CodeStatisticsCalculator to Rails
  • Loading branch information
kamipo authored Aug 25, 2024
2 parents ec667e5 + 8c19758 commit c6e3336
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 212 deletions.
6 changes: 3 additions & 3 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
* Deprecate `::STATS_DIRECTORIES`.

The global constant `STATS_DIRECTORIES` has been deprecated in favor of
`CodeStatistics.add_directory`.
`Rails::CodeStatistics.add_directory`.

Add extra directories with `CodeStatistics.add_directory(label, path)`:
Add extra directories with `Rails::CodeStatistics.add_directory(label, path)`:

```ruby
require "rails/code_statistics"
CodeStatistics.add_directory('My Directory', 'path/to/dir')
Rails::CodeStatistics.add_directory('My Directory', 'path/to/dir')
```

*Petrik de Heus*
Expand Down
242 changes: 122 additions & 120 deletions railties/lib/rails/code_statistics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,149 +3,151 @@
require "rails/code_statistics_calculator"
require "active_support/core_ext/enumerable"

class CodeStatistics
DIRECTORIES = [
%w(Controllers app/controllers),
%w(Helpers app/helpers),
%w(Jobs app/jobs),
%w(Models app/models),
%w(Mailers app/mailers),
%w(Mailboxes app/mailboxes),
%w(Channels app/channels),
%w(Views app/views),
%w(JavaScripts app/assets/javascripts),
%w(Stylesheets app/assets/stylesheets),
%w(JavaScript app/javascript),
%w(Libraries lib/),
%w(APIs app/apis),
%w(Controller\ tests test/controllers),
%w(Helper\ tests test/helpers),
%w(Job\ tests test/jobs),
%w(Model\ tests test/models),
%w(Mailer\ tests test/mailers),
%w(Mailbox\ tests test/mailboxes),
%w(Channel\ tests test/channels),
%w(Integration\ tests test/integration),
%w(System\ tests test/system),
]

TEST_TYPES = ["Controller tests",
"Helper tests",
"Model tests",
"Mailer tests",
"Mailbox tests",
"Channel tests",
"Job tests",
"Integration tests",
"System tests"]

HEADERS = { lines: " Lines", code_lines: " LOC", classes: "Classes", methods: "Methods" }

class_attribute :directories, default: DIRECTORIES

# Add directories to the output of the `bin/rails stats` command.
#
# CodeStatistics.add_directory("My Directory", "path/to/dir")
def self.add_directory(label, path)
self.directories << [label, path]
end

def initialize(*pairs)
@pairs = pairs
@statistics = calculate_statistics
@total = calculate_total if pairs.length > 1
end
module Rails
class CodeStatistics
DIRECTORIES = [
%w(Controllers app/controllers),
%w(Helpers app/helpers),
%w(Jobs app/jobs),
%w(Models app/models),
%w(Mailers app/mailers),
%w(Mailboxes app/mailboxes),
%w(Channels app/channels),
%w(Views app/views),
%w(JavaScripts app/assets/javascripts),
%w(Stylesheets app/assets/stylesheets),
%w(JavaScript app/javascript),
%w(Libraries lib/),
%w(APIs app/apis),
%w(Controller\ tests test/controllers),
%w(Helper\ tests test/helpers),
%w(Job\ tests test/jobs),
%w(Model\ tests test/models),
%w(Mailer\ tests test/mailers),
%w(Mailbox\ tests test/mailboxes),
%w(Channel\ tests test/channels),
%w(Integration\ tests test/integration),
%w(System\ tests test/system),
]

TEST_TYPES = ["Controller tests",
"Helper tests",
"Model tests",
"Mailer tests",
"Mailbox tests",
"Channel tests",
"Job tests",
"Integration tests",
"System tests"]

HEADERS = { lines: " Lines", code_lines: " LOC", classes: "Classes", methods: "Methods" }

class_attribute :directories, default: DIRECTORIES

# Add directories to the output of the `bin/rails stats` command.
#
# Rails::CodeStatistics.add_directory("My Directory", "path/to/dir")
def self.add_directory(label, path)
self.directories << [label, path]
end

def to_s
print_header
@pairs.each { |pair| print_line(pair.first, @statistics[pair.first]) }
print_splitter
def initialize(*pairs)
@pairs = pairs
@statistics = calculate_statistics
@total = calculate_total if pairs.length > 1
end

if @total
print_line("Total", @total)
def to_s
print_header
@pairs.each { |pair| print_line(pair.first, @statistics[pair.first]) }
print_splitter
end

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

private
def calculate_statistics
Hash[@pairs.map { |pair| [pair.first, calculate_directory_statistics(pair.last)] }]
print_code_test_stats
end

def calculate_directory_statistics(directory, pattern = /^(?!\.).*?\.(rb|js|ts|css|scss|coffee|rake|erb)$/)
stats = CodeStatisticsCalculator.new
private
def calculate_statistics
Hash[@pairs.map { |pair| [pair.first, calculate_directory_statistics(pair.last)] }]
end

def calculate_directory_statistics(directory, pattern = /^(?!\.).*?\.(rb|js|ts|css|scss|coffee|rake|erb)$/)
stats = Rails::CodeStatisticsCalculator.new

Dir.foreach(directory) do |file_name|
path = "#{directory}/#{file_name}"
Dir.foreach(directory) do |file_name|
path = "#{directory}/#{file_name}"

if File.directory?(path) && !file_name.start_with?(".")
stats.add(calculate_directory_statistics(path, pattern))
elsif file_name&.match?(pattern)
stats.add_by_file_path(path)
if File.directory?(path) && !file_name.start_with?(".")
stats.add(calculate_directory_statistics(path, pattern))
elsif file_name&.match?(pattern)
stats.add_by_file_path(path)
end
end
end

stats
end
stats
end

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

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

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

def width_for(label)
[@statistics.values.sum { |s| s.public_send(label) }.to_s.size, HEADERS[label].length].max
end
def width_for(label)
[@statistics.values.sum { |s| s.public_send(label) }.to_s.size, HEADERS[label].length].max
end

def print_header
print_splitter
print "| Name "
HEADERS.each do |k, v|
print " | #{v.rjust(width_for(k))}"
def print_header
print_splitter
print "| Name "
HEADERS.each do |k, v|
print " | #{v.rjust(width_for(k))}"
end
puts " | M/C | LOC/M |"
print_splitter
end
puts " | M/C | LOC/M |"
print_splitter
end

def print_splitter
print "+----------------------"
HEADERS.each_key do |k|
print "+#{'-' * (width_for(k) + 2)}"
def print_splitter
print "+----------------------"
HEADERS.each_key do |k|
print "+#{'-' * (width_for(k) + 2)}"
end
puts "+-----+-------+"
end
puts "+-----+-------+"
end

def print_line(name, statistics)
m_over_c = (statistics.methods / statistics.classes) rescue 0
loc_over_m = (statistics.code_lines / statistics.methods) - 2 rescue 0
def print_line(name, statistics)
m_over_c = (statistics.methods / statistics.classes) rescue 0
loc_over_m = (statistics.code_lines / statistics.methods) - 2 rescue 0

print "| #{name.ljust(20)} "
HEADERS.each_key do |k|
print "| #{statistics.send(k).to_s.rjust(width_for(k))} "
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)} |"
end
puts "| #{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
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
puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f / code)}"
puts ""
end
end
end
Loading

0 comments on commit c6e3336

Please sign in to comment.