Skip to content

Start counting files and displaying it in JSON and console output #25

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

Merged
merged 11 commits into from
Dec 19, 2024
94 changes: 15 additions & 79 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,103 +4,39 @@ name: Test
on:
push:
branches:
- main
- main
pull_request:
branches:
- main
- main
jobs:
test-ruby-2-4-x:
test:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ["2.5", "2.6", "2.7", "3.0"]

steps:
- uses: actions/checkout@v1
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.4
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true
- name: Build and run tests
env:
COVERAGE: true
TERM: xterm
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: |
gem install bundler
bundle install --jobs 4 --retry 3
bundle exec rake test
test-ruby-2-5-x:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.5
bundler-cache: true
- name: Build and run tests
env:
COVERAGE: true
TERM: xterm
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: |
gem install bundler
bundle install --jobs 4 --retry 3
bundle exec rake test
test-ruby-2-6-x:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
bundler-cache: true
- name: Build and run tests
env:
COVERAGE: true
TERM: xterm
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
- name: Uninstall existing Bundler
run: |
gem install bundler
bundle install --jobs 4 --retry 3
bundle exec rake test
test-ruby-2-7-x:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7
bundler-cache: true
- name: Build and run tests
env:
COVERAGE: true
TERM: xterm
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
gem uninstall bundler -a -x || true
- name: Install Bundler
run: |
gem install bundler
bundle install --jobs 4 --retry 3
bundle exec rake test
test-ruby-3-0-x:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.0
bundler-cache: true
if [[ "${{ matrix.ruby-version }}" == "2.6" || "${{ matrix.ruby-version }}" == "2.7" ]]; then
gem install bundler -v "~> 2.4.0"
else
gem install bundler
fi
- name: Build and run tests
env:
COVERAGE: true
TERM: xterm
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: |
gem install bundler
bundle install --jobs 4 --retry 3
bundle exec rake test
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* [BUGFIX: Add methods for calculating M/C and LOC/M](https://github.com/fastruby/rails_stats/pull/19)
* [FEATURE: Support JSON output](https://github.com/fastruby/rails_stats/pull/20)
* [FEATURE: Add dependency on bundler-stats and improve output](https://github.com/fastruby/rails_stats/pull/21)

* [FEATURE: Add files count to the console and JSON output](https://github.com/fastruby/rails_stats/pull/25)

# v1.0.2 / 2020-10-7 ([commits](https://github.com/fastruby/rails_stats/compare/v1.0.1...v1.0.2))

Expand Down
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
codecov:
token: d7028c0e-97c5-485f-85e5-f63daabeef63
6 changes: 5 additions & 1 deletion lib/rails_stats/code_statistics_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module RailsStats
class CodeStatisticsCalculator #:nodoc:
attr_reader :lines, :code_lines, :classes, :methods, :test
attr_reader :files_total, :lines, :code_lines, :classes, :methods, :test

PATTERNS = {
rb: {
Expand Down Expand Up @@ -33,20 +33,24 @@ class CodeStatisticsCalculator #:nodoc:

def initialize(test=false)
@test = test
@files_total = 0
@lines = 0
@code_lines = 0
@classes = 0
@methods = 0
end

def add(code_statistics_calculator)
@files_total += code_statistics_calculator.files_total
@lines += code_statistics_calculator.lines
@code_lines += code_statistics_calculator.code_lines
@classes += code_statistics_calculator.classes
@methods += code_statistics_calculator.methods
end

def add_by_file_path(file_path)
@files_total += 1

File.open(file_path) do |f|
self.add_by_io(f, file_type(file_path))
end
Expand Down
9 changes: 6 additions & 3 deletions lib/rails_stats/console_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,22 @@ def to_s

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

def print_splitter
puts "+----------------------+---------+---------+---------+---------+-----+-------+"
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

require 'byebug'; byebug if statistics.nil?

puts "| #{name.ljust(20)} " \
"| #{statistics.files_total.to_s.rjust(7)} " \
"| #{statistics.lines.to_s.rjust(7)} " \
"| #{statistics.code_lines.to_s.rjust(7)} " \
"| #{statistics.classes.to_s.rjust(7)} " \
Expand All @@ -49,7 +52,7 @@ 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 " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)} Files: #{calculator.files_total}"
puts ""
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/rails_stats/json_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def stat_hash(name, statistics)

{
"name" => name,
"files" => statistics.files_total.to_s,
"lines" => statistics.lines.to_s,
"loc" => statistics.code_lines.to_s,
"classes" => statistics.classes.to_s,
Expand Down
10 changes: 7 additions & 3 deletions lib/rails_stats/stats_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def initialize(root_directory)
@statistics = calculate_statistics
@code_loc = calculate_code
@test_loc = calculate_tests
@code_total, @tests_total, @grand_total = calculate_totals
@files_total, @code_total, @tests_total, @grand_total = calculate_totals
end

attr_reader :code_loc, :code_total, :grand_total, :statistics, :test_loc, :tests_total
attr_reader :code_loc, :code_total, :files_total, :grand_total, :statistics, :test_loc, :tests_total

private

Expand Down Expand Up @@ -103,6 +103,10 @@ def calculate_statistics
end

def calculate_totals
files_total = @statistics.sum do |k,value|
value.files_total
end

code_total = @statistics.each_with_object(CodeStatisticsCalculator.new) do |pair, code_total|
code_total.add(pair.last) unless pair.last.test
end
Expand All @@ -115,7 +119,7 @@ def calculate_totals
total.add(pair.last)
end

[code_total, tests_total, grand_total]
[files_total, code_total, tests_total, grand_total]
end

def calculate_code
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_stats/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
root_directory = File.absolute_path(path)
puts "\nDirectory: #{root_directory}\n\n" if args[:path]
RailsStats::CodeStatistics.new(root_directory, format: fmt).to_s
end
end
2 changes: 1 addition & 1 deletion rails_stats.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
spec.email = ["[email protected]"]
spec.summary = %q{Analyze a Rails project}
spec.description = %q{Point it to a directory and see stuff about the app}
spec.homepage = "https://github.com/bleonard/rails_stats"
spec.homepage = "https://github.com/fastruby/rails_stats"
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0")
Expand Down
3 changes: 2 additions & 1 deletion test/dummy/Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.3'
ruby '2.6.8'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3', '>= 6.0.3.3'
Expand All @@ -20,6 +20,7 @@ gem 'jbuilder', '~> 2.7'

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
gem 'rails_stats', path: '../../../rails_stats'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
Expand Down
40 changes: 40 additions & 0 deletions test/fixtures/console-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
+-----------------------|------------|----------------+
| Name | Total Deps | 1st Level Deps |
+-----------------------|------------|----------------+
| simplecov-console | 7 | 3 |
| codecov | 5 | 2 |
| rails_stats | 4 | 2 |
| simplecov | 3 | 3 |
| minitest-around | 1 | 1 |
| bundler | 0 | 0 |
| byebug | 0 | 0 |
| minitest | 0 | 0 |
| minitest-spec-context | 0 | 0 |
+-----------------------|------------|----------------+

Declared Gems 9
Total Gems 18
Unpinned Versions 8
Github Refs 0

+----------------------+---------+---------+---------+---------+---------+-----+-------+
| Name | Files | Lines | LOC | Classes | Methods | M/C | LOC/M |
+----------------------+---------+---------+---------+---------+---------+-----+-------+
| Channels | 2 | 8 | 8 | 2 | 0 | 0 | 0 |
| Configuration | 19 | 417 | 111 | 1 | 0 | 0 | 0 |
| Controllers | 1 | 7 | 6 | 1 | 1 | 1 | 4 |
| Helpers | 1 | 3 | 3 | 0 | 0 | 0 | 0 |
| Javascripts | 3 | 27 | 7 | 0 | 0 | 0 | 0 |
| Jobs | 1 | 7 | 2 | 1 | 0 | 0 | 0 |
| Libraries | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
| Mailers | 1 | 4 | 4 | 1 | 0 | 0 | 0 |
| Model Tests | 2 | 5 | 4 | 2 | 0 | 0 | 0 |
| Models | 1 | 3 | 3 | 1 | 0 | 0 | 0 |
| Spec Support | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
| Test Support | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
+----------------------+---------+---------+---------+---------+---------+-----+-------+
| Code | 30 | 477 | 145 | 7 | 1 | 0 | 143 |
| Tests | 4 | 7 | 6 | 2 | 0 | 0 | 0 |
| Total | 34 | 484 | 151 | 9 | 1 | 0 | 149 |
+----------------------+---------+---------+---------+---------+---------+-----+-------+
Code LOC: 145 Test LOC: 6 Code to Test Ratio: 1:0.0 Files: 34
Loading
Loading