rails_stats 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/test.yml +42 -0
  3. data/.gitignore +2 -0
  4. data/CHANGELOG.md +21 -0
  5. data/CODE_OF_CONDUCT.md +76 -0
  6. data/Gemfile +12 -2
  7. data/README.md +48 -5
  8. data/Rakefile +12 -0
  9. data/codecov.yml +2 -0
  10. data/lib/rails_stats/all.rb +4 -0
  11. data/lib/rails_stats/app_statistics.rb +9 -8
  12. data/lib/rails_stats/code_statistics.rb +9 -148
  13. data/lib/rails_stats/code_statistics_calculator.rb +5 -1
  14. data/lib/rails_stats/console_formatter.rb +59 -0
  15. data/lib/rails_stats/json_formatter.rb +46 -0
  16. data/lib/rails_stats/spec_statistics.rb +1 -1
  17. data/lib/rails_stats/stats_calculator.rb +137 -0
  18. data/lib/rails_stats/stats_formatter.rb +13 -0
  19. data/lib/rails_stats/tasks.rb +4 -3
  20. data/lib/rails_stats/test_statistics.rb +1 -1
  21. data/lib/rails_stats/util.rb +3 -3
  22. data/lib/rails_stats/version.rb +3 -1
  23. data/pull_request_template.md +9 -0
  24. data/rails_stats.gemspec +2 -2
  25. data/test/dummy/Gemfile +43 -0
  26. data/test/dummy/Rakefile +6 -0
  27. data/test/dummy/app/assets/config/manifest.js +2 -0
  28. data/test/dummy/app/assets/images/.keep +0 -0
  29. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  30. data/test/dummy/app/channels/application_cable/channel.rb +4 -0
  31. data/test/dummy/app/channels/application_cable/connection.rb +4 -0
  32. data/test/dummy/app/controllers/application_controller.rb +7 -0
  33. data/test/dummy/app/controllers/concerns/.keep +0 -0
  34. data/test/dummy/app/helpers/application_helper.rb +3 -0
  35. data/test/dummy/app/javascript/channels/consumer.js +6 -0
  36. data/test/dummy/app/javascript/channels/index.js +5 -0
  37. data/test/dummy/app/javascript/packs/application.js +16 -0
  38. data/test/dummy/app/jobs/application_job.rb +7 -0
  39. data/test/dummy/app/mailers/application_mailer.rb +4 -0
  40. data/test/dummy/app/models/application_record.rb +3 -0
  41. data/test/dummy/app/models/concerns/.keep +0 -0
  42. data/test/dummy/app/views/layouts/application.html.erb +15 -0
  43. data/test/dummy/app/views/layouts/mailer.html.erb +13 -0
  44. data/test/dummy/app/views/layouts/mailer.text.erb +1 -0
  45. data/test/dummy/bin/rails +4 -0
  46. data/test/dummy/bin/rake +4 -0
  47. data/test/dummy/bin/setup +36 -0
  48. data/test/dummy/bin/yarn +11 -0
  49. data/test/dummy/config/application.rb +35 -0
  50. data/test/dummy/config/boot.rb +4 -0
  51. data/test/dummy/config/cable.yml +10 -0
  52. data/test/dummy/config/credentials.yml.enc +1 -0
  53. data/test/dummy/config/database.yml +85 -0
  54. data/test/dummy/config/environment.rb +5 -0
  55. data/test/dummy/config/environments/development.rb +62 -0
  56. data/test/dummy/config/environments/production.rb +112 -0
  57. data/test/dummy/config/environments/test.rb +49 -0
  58. data/test/dummy/config/initializers/application_controller_renderer.rb +8 -0
  59. data/test/dummy/config/initializers/assets.rb +14 -0
  60. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  61. data/test/dummy/config/initializers/content_security_policy.rb +30 -0
  62. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  63. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  64. data/test/dummy/config/initializers/inflections.rb +16 -0
  65. data/test/dummy/config/initializers/mime_types.rb +4 -0
  66. data/test/dummy/config/initializers/pagy.rb +1 -0
  67. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  68. data/test/dummy/config/locales/en.yml +33 -0
  69. data/test/dummy/config/master.key +1 -0
  70. data/test/dummy/config/puma.rb +38 -0
  71. data/test/dummy/config/routes.rb +3 -0
  72. data/test/dummy/config/spring.rb +6 -0
  73. data/test/dummy/config/storage.yml +34 -0
  74. data/test/dummy/config.ru +5 -0
  75. data/test/dummy/lib/monkeypatches.rb +1 -0
  76. data/test/dummy/package.json +10 -0
  77. data/test/dummy/spec/models/user_spec.rb +3 -0
  78. data/test/dummy/spec/support/support_file.rb +1 -0
  79. data/test/dummy/test/models/user_test.rb +2 -0
  80. data/test/dummy/test/support/test_helper.rb +1 -0
  81. data/test/fixtures/console-output.txt +40 -0
  82. data/test/lib/rails_stats/code_statistics_test.rb +20 -0
  83. data/test/lib/rails_stats/json_formatter_test.rb +144 -0
  84. data/test/test_helper.rb +27 -0
  85. metadata +143 -15
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d49b8204fe1e35492bb2e33e38658e2c98b1d902
4
- data.tar.gz: 4804e75ac83f121ae925b552b66de5c07919a576
2
+ SHA256:
3
+ metadata.gz: 06f66a7f398a3206774d6dc5b80ce346e8c518f3469c386da5b23e2d4ebdc63e
4
+ data.tar.gz: a3628487b836738a60f55facfbb79d5617f641391e30ac0488e294a995b9332e
5
5
  SHA512:
6
- metadata.gz: ff5d3503d081cef1b68099febae02f312f42f9614de256b7ad7796ec72662df2155868561df004875f160c0a1b6f6693327c9cba09d6a9057dc83eb16b10b233
7
- data.tar.gz: 6569a5564619e65358ece062b1c35d479788d787fa30823535e8cb0527675862d8ef1e67a3e60fe060f32da18a6d021b2db941feb9db7023129c2241949a42a5
6
+ metadata.gz: 03d2f39eeace55a4580f7222c1c50f53bfd2110459f69be3a976889d836f4477e7ba3d9e4969818270529a457a88c9929b2346cc81effc446a9c71a239ee9093
7
+ data.tar.gz: 75d02d452ef3f44b8a429bb80bd07e67bece730d5de1e60c25b4cd215a66deb8d7ebbe6134d305873856f1793488ba1bde41778204edc3e7a60515c3093c1474
@@ -0,0 +1,42 @@
1
+ # .github/workflows/ci.yml
2
+
3
+ name: Test
4
+ on:
5
+ push:
6
+ branches:
7
+ - main
8
+ pull_request:
9
+ branches:
10
+ - main
11
+ jobs:
12
+ test:
13
+ runs-on: ubuntu-latest
14
+ strategy:
15
+ matrix:
16
+ ruby-version: ["2.5", "2.6", "2.7", "3.0"]
17
+
18
+ steps:
19
+ - uses: actions/checkout@v1
20
+ - name: Setup Ruby
21
+ uses: ruby/setup-ruby@v1
22
+ with:
23
+ ruby-version: ${{ matrix.ruby-version }}
24
+ bundler-cache: true
25
+ - name: Uninstall existing Bundler
26
+ run: |
27
+ gem uninstall bundler -a -x || true
28
+ - name: Install Bundler
29
+ run: |
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
35
+ - name: Build and run tests
36
+ env:
37
+ COVERAGE: true
38
+ TERM: xterm
39
+ CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
40
+ run: |
41
+ bundle install --jobs 4 --retry 3
42
+ bundle exec rake test
data/.gitignore CHANGED
@@ -1,7 +1,9 @@
1
+ .byebug_history
1
2
  *.gem
2
3
  *.rbc
3
4
  .bundle
4
5
  .config
6
+ .ruby-version
5
7
  .yardoc
6
8
  Gemfile.lock
7
9
  InstalledFiles
data/CHANGELOG.md ADDED
@@ -0,0 +1,21 @@
1
+ # main ([unreleased](https://github.com/fastruby/rails_stats/compare/v1.0.2...main))
2
+
3
+ *
4
+
5
+ # v2.0.0 ([commits](https://github.com/fastruby/rails_stats/compare/v1.0.2...v2.0.0))
6
+
7
+ * [FEATURE: Add basic coverage to RailsStats::CodeStatistics#to_s](https://github.com/fastruby/rails_stats/pull/10)
8
+ * [FEATURE: Enable simplecov and codecov](https://github.com/fastruby/rails_stats/pull/18)
9
+ * [BUGFIX: Add methods for calculating M/C and LOC/M](https://github.com/fastruby/rails_stats/pull/19)
10
+ * [FEATURE: Support JSON output](https://github.com/fastruby/rails_stats/pull/20)
11
+ * [FEATURE: Add dependency on bundler-stats and improve output](https://github.com/fastruby/rails_stats/pull/21)
12
+ * [FEATURE: Add files count to the console and JSON output](https://github.com/fastruby/rails_stats/pull/25)
13
+
14
+ # v1.0.2 / 2020-10-7 ([commits](https://github.com/fastruby/rails_stats/compare/v1.0.1...v1.0.2))
15
+
16
+ * [CHORE: Relax bundler dependency to make it easier to get started contributing](https://github.com/fastruby/rails_stats/pull/9)
17
+
18
+ # v1.0.1 / 2018-10-8
19
+
20
+ * [FEATURE: add more aggregate stats](https://github.com/fastruby/rails_stats/pull/5)
21
+ * [FEATURE: More detailed introspection of app](https://github.com/fastruby/rails_stats/pull/1)
@@ -0,0 +1,76 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to make participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, sex characteristics, gender identity and expression,
9
+ level of experience, education, socio-economic status, nationality, personal
10
+ appearance, race, religion, or sexual identity and orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies within all project spaces, and it also applies when
49
+ an individual is representing the project or its community in public spaces.
50
+ Examples of representing a project or community include using an official
51
+ project e-mail address, posting via an official social media account, or acting
52
+ as an appointed representative at an online or offline event. Representation of
53
+ a project may be further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at oss@ombulabs.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72
+
73
+ [homepage]: https://www.contributor-covenant.org
74
+
75
+ For answers to common questions about this code of conduct, see
76
+ https://www.contributor-covenant.org/faq
data/Gemfile CHANGED
@@ -1,4 +1,14 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in rails_stats.gemspec
4
3
  gemspec
4
+
5
+ group :development, :test do
6
+ gem "bundler", ">= 1.6", "< 3.0"
7
+ gem "byebug"
8
+ gem "codecov"
9
+ gem "minitest"
10
+ gem "minitest-around"
11
+ gem "minitest-spec-context"
12
+ gem "simplecov"
13
+ gem "simplecov-console"
14
+ end
data/README.md CHANGED
@@ -3,13 +3,23 @@
3
3
  See stuff about a Rails app.
4
4
 
5
5
  There were a few things missing to the included `rake stats`
6
+
6
7
  RailsStats mainly adds the ability to be run from outside the project in question. This can be helpful if the app you are interested in can not be booted for some reason.
7
8
 
8
- ### Run it
9
+ ### Run it outside Rails project
9
10
 
10
- ```bash
11
+ You will need a `Rakefile` in the directory where you call `rake` and you will
12
+ need to require `rails_stats`:
13
+
14
+ ```ruby
15
+ # Rakefile
16
+ require "rails_stats"
17
+ ```
11
18
 
12
- $ bundle exec rake stats[/path/to/app/]
19
+ Then you can call it:
20
+
21
+ ```bash
22
+ $ rake stats\[/path/to/app/\]
13
23
 
14
24
  Directory: /path/to/app/
15
25
 
@@ -33,7 +43,12 @@ Directory: /path/to/app/
33
43
  | Total | 21805 | 16980 | 218 | 1483 | 6 | 9 |
34
44
  +----------------------+-------+-------+---------+---------+-----+-------+
35
45
  Code LOC: 10142 Test LOC: 6838 Code to Test Ratio: 1:0.7
46
+ ```
36
47
 
48
+ ### Run it on many Rails engines
49
+
50
+ ```bash
51
+ $ for dir in /path/to/many/engines/*/; do bundle exec rake stats[$dir]; done
37
52
  ```
38
53
 
39
54
  ### Within your Rails project
@@ -62,7 +77,7 @@ RailsStats adds more coverage than the default.
62
77
 
63
78
  ### Example output
64
79
 
65
- Here are some open source Rails projects an their output.
80
+ Here are some open source Rails projects and their output.
66
81
 
67
82
  ```bash
68
83
  $ bundle exec rake stats[/users/brian/examples/diaspora/]
@@ -252,7 +267,35 @@ Directory: /users/brian/examples/spree
252
267
  | Total | 56347 | 44503 | 341 | 2317 | 6 | 17 |
253
268
  +----------------------+-------+-------+---------+---------+-----+-------+
254
269
  Code LOC: 17870 Test LOC: 26633 Code to Test Ratio: 1:1.5
255
-
270
+
271
+ ```
272
+
273
+ #### JSON Format
274
+
275
+ If you want to export the details using JSON, you can use this command:
276
+
277
+ ```
278
+ $ rake stats\[test/dummy,json\]
279
+
280
+ Directory: /Users/etagwerker/Projects/fastruby/rails_stats/test/dummy
281
+
282
+ [{"name":"Mailers","lines":"4","loc":"4","classes":"1","methods":"0","m_over_c":"0","loc_over_m":"0"},{"name":"Models","lines":"3","loc":"3","classes":"1","methods":"0","m_over_c":"0","loc_over_m":"0"},{"name":"Javascripts","lines":"27","loc":"7","classes":"0","methods":"0","m_over_c":"0","loc_over_m":"0"},{"name":"Jobs","lines":"7","loc":"2","classes":"1","methods":"0","m_over_c":"0","loc_over_m":"0"},{"name":"Controllers","lines":"7","loc":"6","classes":"1","methods":"1","m_over_c":"1","loc_over_m":"4"},{"name":"Helpers","lines":"3","loc":"3","classes":"0","methods":"0","m_over_c":"0","loc_over_m":"0"},{"name":"Channels","lines":"8","loc":"8","classes":"2","methods":"0","m_over_c":"0","loc_over_m":"0"},{"name":"Configuration","lines":"417","loc":"111","classes":"1","methods":"0","m_over_c":"0","loc_over_m":"0"},{"name":"Total","lines":"476","loc":"144","classes":"7","methods":"1","m_over_c":"0","loc_over_m":"142","code_to_test_ratio":"0.0","total":true}]
283
+ ```
284
+
285
+ ### Testing
286
+
287
+ In order to run the tests for this gem:
288
+
289
+ ```bash
290
+ bundle exec rake
291
+
292
+ # Running:
293
+
294
+ .
295
+
296
+ Fabulous run in 0.013349s, 74.9120 runs/s, 74.9120 assertions/s.
297
+
298
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
256
299
  ```
257
300
 
258
301
  ### TODO
data/Rakefile CHANGED
@@ -1 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
1
5
  require "rails_stats"
6
+
7
+ Rake::TestTask.new do |task|
8
+ task.libs.push "lib"
9
+ task.libs.push "test"
10
+ task.pattern = "test/**/*_test.rb"
11
+ end
12
+
13
+ task default: %i[test]
data/codecov.yml ADDED
@@ -0,0 +1,2 @@
1
+ codecov:
2
+ token: d7028c0e-97c5-485f-85e5-f63daabeef63
@@ -1,3 +1,7 @@
1
+ require 'rails_stats/stats_calculator'
2
+ require 'rails_stats/stats_formatter'
3
+ require 'rails_stats/json_formatter'
4
+ require 'rails_stats/console_formatter'
1
5
  require 'rails_stats/inflector'
2
6
  require 'rails_stats/code_statistics_calculator'
3
7
  require 'rails_stats/util'
@@ -3,10 +3,11 @@ module RailsStats
3
3
  attr_reader :statistics, :total, :test
4
4
 
5
5
  def initialize(directory)
6
+ @directories = []
6
7
  @test = false
7
- @directory = directory
8
- @statistics = calculate_statistics
9
- @total = calculate_total
8
+ @directory = directory
9
+ @statistics = calculate_statistics
10
+ @total = calculate_total
10
11
  end
11
12
 
12
13
  def key_concepts
@@ -28,15 +29,15 @@ module RailsStats
28
29
  end
29
30
 
30
31
  def directories
31
- return @directories if @directories
32
- out = []
32
+ return @directories if @directories.any?
33
+
33
34
  Dir.foreach(@directory) do |file_name|
34
35
  path = File.join(@directory, file_name)
35
36
  next unless File.directory?(path)
36
37
  next if (/^\./ =~ file_name)
37
38
  next if file_name == "assets" # doing separately
38
39
  next if file_name == "views" # TODO
39
- out << path
40
+ @directories << path
40
41
  end
41
42
 
42
43
  assets = File.join(@directory, "assets")
@@ -48,13 +49,13 @@ module RailsStats
48
49
 
49
50
  case file_name
50
51
  when "javascripts"
51
- out << path
52
+ @directories << path
52
53
  # TODO when "css"
53
54
  end
54
55
  end
55
56
  end
56
57
 
57
- out
58
+ @directories
58
59
  end
59
60
  end
60
61
 
@@ -2,162 +2,23 @@
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
- @total = calculate_total
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
- @statistics.each { |key, stats| print_line(key, stats) }
24
- print_splitter
25
-
26
- if @total
27
- print_line("Total", @total)
28
- print_splitter
29
- end
30
-
31
- print_code_test_stats
11
+ @formatter.to_s
32
12
  end
33
13
 
34
14
  private
35
- def calculate_key_concepts
36
- # returns names of main things like models, controllers, services, etc
37
- concepts = {}
38
- app_projects.each do |project|
39
- project.key_concepts.each do |key|
40
- concepts[key] = true
41
- end
42
- end
43
-
44
- # TODO: maybe gem names?
45
-
46
- concepts.keys
47
- end
48
-
49
- def calculate_projects
50
- out = []
51
- out += app_projects
52
- out += calculate_root_projects
53
- out += calculate_gem_projects
54
- out += calculate_spec_projects
55
- out += calculate_test_projects
56
- out += calculate_cucumber_projects
57
- out
58
- end
59
-
60
- def app_projects
61
- @app_projects ||= calculate_app_projects
62
- end
63
-
64
- def calculate_app_projects
65
- apps = Util.calculate_projects(@root_directory, "**", "app", RAILS_APP_FOLDERS)
66
- apps.collect do |root_path|
67
- AppStatistics.new(root_path)
68
- end
69
- end
70
-
71
- def calculate_gem_projects
72
- gems = Util.calculate_projects(@root_directory, "*", "**", "*.gemspec")
73
- gems.collect do |root_path|
74
- GemStatistics.new(root_path)
75
- end
76
- end
77
-
78
- def calculate_spec_projects
79
- specs = Util.calculate_shared_projects("spec", @root_directory, "**", "spec", "**", "*_spec.rb")
80
- specs.collect do |root_path|
81
- SpecStatistics.new(root_path, @key_concepts)
82
- end
83
- end
84
15
 
85
- def calculate_test_projects
86
- tests = Util.calculate_shared_projects("test", @root_directory, "**", "test", "**", "*_test.rb")
87
- tests.collect do |root_path|
88
- TestStatistics.new(root_path, @key_concepts)
16
+ def load_formatter(opts = {})
17
+ if opts[:format] == "json"
18
+ RailsStats::JSONFormatter.new(@calculator, opts)
19
+ else
20
+ RailsStats::ConsoleFormatter.new(@calculator, opts)
89
21
  end
90
22
  end
91
-
92
- def calculate_root_projects
93
- [RootStatistics.new(@root_directory)]
94
- end
95
-
96
- def calculate_cucumber_projects
97
- cukes = Util.calculate_projects(@root_directory, "**", "*.feature")
98
- cukes.collect do |root_path|
99
- CucumberStatistics.new(root_path)
100
- end
101
- end
102
-
103
- def calculate_statistics
104
- out = {}
105
- @projects.each do |project|
106
- project.statistics.each do |key, stats|
107
- out[key] ||= CodeStatisticsCalculator.new(project.test)
108
- out[key].add(stats)
109
- end
110
- end
111
- out
112
- end
113
-
114
- def calculate_total
115
- @statistics.each_with_object(CodeStatisticsCalculator.new) do |pair, total|
116
- total.add(pair.last)
117
- end
118
- end
119
-
120
- def calculate_code
121
- code_loc = 0
122
- @statistics.each { |k, v| code_loc += v.code_lines unless v.test }
123
- code_loc
124
- end
125
-
126
- def calculate_tests
127
- test_loc = 0
128
- @statistics.each { |k, v| test_loc += v.code_lines if v.test }
129
- test_loc
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(5)} " \
148
- "| #{statistics.code_lines.to_s.rjust(5)} " \
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 = calculate_code
157
- tests = calculate_tests
158
-
159
- puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}"
160
- puts ""
161
- end
162
23
  end
163
24
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module RailsStats
4
4
  class CodeStatisticsCalculator #:nodoc:
5
- attr_reader :lines, :code_lines, :classes, :methods, :test
5
+ attr_reader :files_total, :lines, :code_lines, :classes, :methods, :test
6
6
 
7
7
  PATTERNS = {
8
8
  rb: {
@@ -33,6 +33,7 @@ module RailsStats
33
33
 
34
34
  def initialize(test=false)
35
35
  @test = test
36
+ @files_total = 0
36
37
  @lines = 0
37
38
  @code_lines = 0
38
39
  @classes = 0
@@ -40,6 +41,7 @@ module RailsStats
40
41
  end
41
42
 
42
43
  def add(code_statistics_calculator)
44
+ @files_total += code_statistics_calculator.files_total
43
45
  @lines += code_statistics_calculator.lines
44
46
  @code_lines += code_statistics_calculator.code_lines
45
47
  @classes += code_statistics_calculator.classes
@@ -47,6 +49,8 @@ module RailsStats
47
49
  end
48
50
 
49
51
  def add_by_file_path(file_path)
52
+ @files_total += 1
53
+
50
54
  File.open(file_path) do |f|
51
55
  self.add_by_io(f, file_type(file_path))
52
56
  end
@@ -0,0 +1,59 @@
1
+ require "bundler/stats/cli"
2
+
3
+ module RailsStats
4
+ class ConsoleFormatter < StatsFormatter
5
+ def to_s
6
+ Bundler::Stats::CLI.start
7
+
8
+ print_header
9
+ sorted_keys = @statistics.keys.sort
10
+ sorted_keys.each { |key| print_line(key, @statistics[key]) }
11
+ print_splitter
12
+
13
+ if @grand_total
14
+ print_line("Code", @code_total)
15
+ print_line("Tests", @tests_total)
16
+ print_line("Total", @grand_total)
17
+ print_splitter
18
+ end
19
+
20
+ print_code_test_stats
21
+ end
22
+
23
+ private
24
+
25
+ def print_header
26
+ print_splitter
27
+ puts "| Name | Files | Lines | LOC | Classes | Methods | M/C | LOC/M |"
28
+ print_splitter
29
+ end
30
+
31
+ def print_splitter
32
+ puts "+----------------------+---------+---------+---------+---------+---------+-----+-------+"
33
+ end
34
+
35
+ def print_line(name, statistics)
36
+ m_over_c = (statistics.methods / statistics.classes) rescue m_over_c = 0
37
+ loc_over_m = (statistics.code_lines / statistics.methods) - 2 rescue loc_over_m = 0
38
+
39
+ require 'byebug'; byebug if statistics.nil?
40
+
41
+ puts "| #{name.ljust(20)} " \
42
+ "| #{statistics.files_total.to_s.rjust(7)} " \
43
+ "| #{statistics.lines.to_s.rjust(7)} " \
44
+ "| #{statistics.code_lines.to_s.rjust(7)} " \
45
+ "| #{statistics.classes.to_s.rjust(7)} " \
46
+ "| #{statistics.methods.to_s.rjust(7)} " \
47
+ "| #{m_over_c.to_s.rjust(3)} " \
48
+ "| #{loc_over_m.to_s.rjust(5)} |"
49
+ end
50
+
51
+ def print_code_test_stats
52
+ code = calculator.code_loc
53
+ tests = calculator.test_loc
54
+
55
+ puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)} Files: #{calculator.files_total}"
56
+ puts ""
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,46 @@
1
+ require "json"
2
+
3
+ module RailsStats
4
+ class JSONFormatter < StatsFormatter
5
+ def result
6
+ @result = @statistics.map { |key, stats| stat_hash(key, stats) }
7
+
8
+ if @grand_total
9
+ @result << stat_hash("Total", @grand_total).merge(code_test_hash)
10
+ end
11
+
12
+ @result
13
+ end
14
+
15
+ def to_s
16
+ puts result.to_json
17
+ end
18
+
19
+ private
20
+
21
+ def code_test_hash
22
+ code = calculator.code_loc
23
+ tests = calculator.test_loc
24
+
25
+ {
26
+ "code_to_test_ratio" => "#{sprintf("%.1f", tests.to_f/code)}", "total" => true
27
+ }
28
+ end
29
+
30
+ def stat_hash(name, statistics)
31
+ m_over_c = (statistics.methods / statistics.classes) rescue m_over_c = 0
32
+ loc_over_m = (statistics.code_lines / statistics.methods) - 2 rescue loc_over_m = 0
33
+
34
+ {
35
+ "name" => name,
36
+ "files" => statistics.files_total.to_s,
37
+ "lines" => statistics.lines.to_s,
38
+ "loc" => statistics.code_lines.to_s,
39
+ "classes" => statistics.classes.to_s,
40
+ "methods" => statistics.methods.to_s,
41
+ "m_over_c" => m_over_c.to_s,
42
+ "loc_over_m" => loc_over_m.to_s
43
+ }
44
+ end
45
+ end
46
+ end
@@ -37,7 +37,7 @@ module RailsStats
37
37
  out = {}
38
38
  categorize_files.each do |key, list|
39
39
  out[key] = Util.calculate_file_statistics(list)
40
- end
40
+ end
41
41
  out
42
42
  end
43
43