Skip to content

Commit e236c90

Browse files
authored
Merge pull request #25 from fastruby/feature/count-files
Start counting files and displaying it in JSON and console output
2 parents 3aa647a + 362f3a8 commit e236c90

13 files changed

+98
-173
lines changed

.github/workflows/test.yml

+15-79
Original file line numberDiff line numberDiff line change
@@ -4,103 +4,39 @@ name: Test
44
on:
55
push:
66
branches:
7-
- main
7+
- main
88
pull_request:
99
branches:
10-
- main
10+
- main
1111
jobs:
12-
test-ruby-2-4-x:
12+
test:
1313
runs-on: ubuntu-latest
14+
strategy:
15+
matrix:
16+
ruby-version: ["2.5", "2.6", "2.7", "3.0"]
1417

1518
steps:
1619
- uses: actions/checkout@v1
1720
- name: Setup Ruby
1821
uses: ruby/setup-ruby@v1
1922
with:
20-
ruby-version: 2.4
23+
ruby-version: ${{ matrix.ruby-version }}
2124
bundler-cache: true
22-
- name: Build and run tests
23-
env:
24-
COVERAGE: true
25-
TERM: xterm
26-
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
27-
run: |
28-
gem install bundler
29-
bundle install --jobs 4 --retry 3
30-
bundle exec rake test
31-
test-ruby-2-5-x:
32-
runs-on: ubuntu-latest
33-
34-
steps:
35-
- uses: actions/checkout@v1
36-
- name: Setup Ruby
37-
uses: ruby/setup-ruby@v1
38-
with:
39-
ruby-version: 2.5
40-
bundler-cache: true
41-
- name: Build and run tests
42-
env:
43-
COVERAGE: true
44-
TERM: xterm
45-
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
46-
run: |
47-
gem install bundler
48-
bundle install --jobs 4 --retry 3
49-
bundle exec rake test
50-
test-ruby-2-6-x:
51-
runs-on: ubuntu-latest
52-
53-
steps:
54-
- uses: actions/checkout@v1
55-
- name: Setup Ruby
56-
uses: ruby/setup-ruby@v1
57-
with:
58-
ruby-version: 2.6
59-
bundler-cache: true
60-
- name: Build and run tests
61-
env:
62-
COVERAGE: true
63-
TERM: xterm
64-
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
25+
- name: Uninstall existing Bundler
6526
run: |
66-
gem install bundler
67-
bundle install --jobs 4 --retry 3
68-
bundle exec rake test
69-
test-ruby-2-7-x:
70-
runs-on: ubuntu-latest
71-
72-
steps:
73-
- uses: actions/checkout@v1
74-
- name: Setup Ruby
75-
uses: ruby/setup-ruby@v1
76-
with:
77-
ruby-version: 2.7
78-
bundler-cache: true
79-
- name: Build and run tests
80-
env:
81-
COVERAGE: true
82-
TERM: xterm
83-
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
27+
gem uninstall bundler -a -x || true
28+
- name: Install Bundler
8429
run: |
85-
gem install bundler
86-
bundle install --jobs 4 --retry 3
87-
bundle exec rake test
88-
test-ruby-3-0-x:
89-
runs-on: ubuntu-latest
90-
91-
steps:
92-
- uses: actions/checkout@v1
93-
- name: Setup Ruby
94-
uses: ruby/setup-ruby@v1
95-
with:
96-
ruby-version: 3.0
97-
bundler-cache: true
30+
if [[ "${{ matrix.ruby-version }}" == "2.6" || "${{ matrix.ruby-version }}" == "2.7" ]]; then
31+
gem install bundler -v "~> 2.4.0"
32+
else
33+
gem install bundler
34+
fi
9835
- name: Build and run tests
9936
env:
10037
COVERAGE: true
10138
TERM: xterm
10239
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
10340
run: |
104-
gem install bundler
10541
bundle install --jobs 4 --retry 3
10642
bundle exec rake test

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* [BUGFIX: Add methods for calculating M/C and LOC/M](https://github.com/fastruby/rails_stats/pull/19)
66
* [FEATURE: Support JSON output](https://github.com/fastruby/rails_stats/pull/20)
77
* [FEATURE: Add dependency on bundler-stats and improve output](https://github.com/fastruby/rails_stats/pull/21)
8-
8+
* [FEATURE: Add files count to the console and JSON output](https://github.com/fastruby/rails_stats/pull/25)
99

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

codecov.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
codecov:
2+
token: d7028c0e-97c5-485f-85e5-f63daabeef63

lib/rails_stats/code_statistics_calculator.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module RailsStats
44
class CodeStatisticsCalculator #:nodoc:
5-
attr_reader :lines, :code_lines, :classes, :methods, :test
5+
attr_reader :files_total, :lines, :code_lines, :classes, :methods, :test
66

77
PATTERNS = {
88
rb: {
@@ -33,20 +33,24 @@ class CodeStatisticsCalculator #:nodoc:
3333

3434
def initialize(test=false)
3535
@test = test
36+
@files_total = 0
3637
@lines = 0
3738
@code_lines = 0
3839
@classes = 0
3940
@methods = 0
4041
end
4142

4243
def add(code_statistics_calculator)
44+
@files_total += code_statistics_calculator.files_total
4345
@lines += code_statistics_calculator.lines
4446
@code_lines += code_statistics_calculator.code_lines
4547
@classes += code_statistics_calculator.classes
4648
@methods += code_statistics_calculator.methods
4749
end
4850

4951
def add_by_file_path(file_path)
52+
@files_total += 1
53+
5054
File.open(file_path) do |f|
5155
self.add_by_io(f, file_type(file_path))
5256
end

lib/rails_stats/console_formatter.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,22 @@ def to_s
2424

2525
def print_header
2626
print_splitter
27-
puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |"
27+
puts "| Name | Files | Lines | LOC | Classes | Methods | M/C | LOC/M |"
2828
print_splitter
2929
end
3030

3131
def print_splitter
32-
puts "+----------------------+---------+---------+---------+---------+-----+-------+"
32+
puts "+----------------------+---------+---------+---------+---------+---------+-----+-------+"
3333
end
3434

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

39+
require 'byebug'; byebug if statistics.nil?
40+
3941
puts "| #{name.ljust(20)} " \
42+
"| #{statistics.files_total.to_s.rjust(7)} " \
4043
"| #{statistics.lines.to_s.rjust(7)} " \
4144
"| #{statistics.code_lines.to_s.rjust(7)} " \
4245
"| #{statistics.classes.to_s.rjust(7)} " \
@@ -49,7 +52,7 @@ def print_code_test_stats
4952
code = calculator.code_loc
5053
tests = calculator.test_loc
5154

52-
puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}"
55+
puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)} Files: #{calculator.files_total}"
5356
puts ""
5457
end
5558
end

lib/rails_stats/json_formatter.rb

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def stat_hash(name, statistics)
3333

3434
{
3535
"name" => name,
36+
"files" => statistics.files_total.to_s,
3637
"lines" => statistics.lines.to_s,
3738
"loc" => statistics.code_lines.to_s,
3839
"classes" => statistics.classes.to_s,

lib/rails_stats/stats_calculator.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ def initialize(root_directory)
1414
@statistics = calculate_statistics
1515
@code_loc = calculate_code
1616
@test_loc = calculate_tests
17-
@code_total, @tests_total, @grand_total = calculate_totals
17+
@files_total, @code_total, @tests_total, @grand_total = calculate_totals
1818
end
1919

20-
attr_reader :code_loc, :code_total, :grand_total, :statistics, :test_loc, :tests_total
20+
attr_reader :code_loc, :code_total, :files_total, :grand_total, :statistics, :test_loc, :tests_total
2121

2222
private
2323

@@ -103,6 +103,10 @@ def calculate_statistics
103103
end
104104

105105
def calculate_totals
106+
files_total = @statistics.sum do |k,value|
107+
value.files_total
108+
end
109+
106110
code_total = @statistics.each_with_object(CodeStatisticsCalculator.new) do |pair, code_total|
107111
code_total.add(pair.last) unless pair.last.test
108112
end
@@ -115,7 +119,7 @@ def calculate_totals
115119
total.add(pair.last)
116120
end
117121

118-
[code_total, tests_total, grand_total]
122+
[files_total, code_total, tests_total, grand_total]
119123
end
120124

121125
def calculate_code

lib/rails_stats/tasks.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
root_directory = File.absolute_path(path)
1212
puts "\nDirectory: #{root_directory}\n\n" if args[:path]
1313
RailsStats::CodeStatistics.new(root_directory, format: fmt).to_s
14-
end
14+
end

rails_stats.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
1010
spec.email = ["[email protected]"]
1111
spec.summary = %q{Analyze a Rails project}
1212
spec.description = %q{Point it to a directory and see stuff about the app}
13-
spec.homepage = "https://github.com/bleonard/rails_stats"
13+
spec.homepage = "https://github.com/fastruby/rails_stats"
1414
spec.license = "MIT"
1515

1616
spec.files = `git ls-files -z`.split("\x0")

test/dummy/Gemfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
source 'https://rubygems.org'
22
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
33

4-
ruby '2.6.3'
4+
ruby '2.6.8'
55

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

2121
# Use Active Storage variant
2222
# gem 'image_processing', '~> 1.2'
23+
gem 'rails_stats', path: '../../../rails_stats'
2324

2425
# Reduces boot times through caching; required in config/boot.rb
2526
gem 'bootsnap', '>= 1.4.2', require: false

test/fixtures/console-output.txt

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
+-----------------------|------------|----------------+
2+
| Name | Total Deps | 1st Level Deps |
3+
+-----------------------|------------|----------------+
4+
| simplecov-console | 7 | 3 |
5+
| codecov | 5 | 2 |
6+
| rails_stats | 4 | 2 |
7+
| simplecov | 3 | 3 |
8+
| minitest-around | 1 | 1 |
9+
| bundler | 0 | 0 |
10+
| byebug | 0 | 0 |
11+
| minitest | 0 | 0 |
12+
| minitest-spec-context | 0 | 0 |
13+
+-----------------------|------------|----------------+
14+
15+
Declared Gems 9
16+
Total Gems 18
17+
Unpinned Versions 8
18+
Github Refs 0
19+
20+
+----------------------+---------+---------+---------+---------+---------+-----+-------+
21+
| Name | Files | Lines | LOC | Classes | Methods | M/C | LOC/M |
22+
+----------------------+---------+---------+---------+---------+---------+-----+-------+
23+
| Channels | 2 | 8 | 8 | 2 | 0 | 0 | 0 |
24+
| Configuration | 19 | 417 | 111 | 1 | 0 | 0 | 0 |
25+
| Controllers | 1 | 7 | 6 | 1 | 1 | 1 | 4 |
26+
| Helpers | 1 | 3 | 3 | 0 | 0 | 0 | 0 |
27+
| Javascripts | 3 | 27 | 7 | 0 | 0 | 0 | 0 |
28+
| Jobs | 1 | 7 | 2 | 1 | 0 | 0 | 0 |
29+
| Libraries | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
30+
| Mailers | 1 | 4 | 4 | 1 | 0 | 0 | 0 |
31+
| Model Tests | 2 | 5 | 4 | 2 | 0 | 0 | 0 |
32+
| Models | 1 | 3 | 3 | 1 | 0 | 0 | 0 |
33+
| Spec Support | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
34+
| Test Support | 1 | 1 | 1 | 0 | 0 | 0 | 0 |
35+
+----------------------+---------+---------+---------+---------+---------+-----+-------+
36+
| Code | 30 | 477 | 145 | 7 | 1 | 0 | 143 |
37+
| Tests | 4 | 7 | 6 | 2 | 0 | 0 | 0 |
38+
| Total | 34 | 484 | 151 | 9 | 1 | 0 | 149 |
39+
+----------------------+---------+---------+---------+---------+---------+-----+-------+
40+
Code LOC: 145 Test LOC: 6 Code to Test Ratio: 1:0.0 Files: 34

0 commit comments

Comments
 (0)