code_keeper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: da70c28c42b1b848e40adb6145d25686ed8e8bea4dbf25adeb49bebbb82555a8
4
+ data.tar.gz: dcf7b9aebd57660ded6869224c87769d836c8518a19b46c74a8d1c86edd1e9b4
5
+ SHA512:
6
+ metadata.gz: e6dcab3c8280feb01e837e87025d5d71f6913f74b64c5afa22c36e126e8d69f8321c4445653088fc8fa82a4ee5c4bf2dffef825f92e6c5e5fbf4177a9ce725fd
7
+ data.tar.gz: 6d40b0fe93e685cbe3d982dd467dc843138686bdf08a95964b90da4187587fb9e4a3ffcab454dbc1b90ad086e9df6bf56d96cb2152dabf72450bc203790bde42
@@ -0,0 +1,18 @@
1
+ name: Ruby CI
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 3.0.0
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.3
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,46 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+ Exclude:
4
+ - spec/fixtures/**/*.rb
5
+
6
+ Style/StringLiterals:
7
+ Enabled: false
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 160
15
+
16
+ Layout/EmptyLineBetweenDefs:
17
+ Exclude:
18
+ - lib/code_keeper.rb
19
+
20
+ Metrics/BlockLength:
21
+ Exclude:
22
+ - spec/**/*.rb
23
+
24
+ Layout/HeredocIndentation:
25
+ Exclude:
26
+ - spec/code_keeper/formatter_spec.rb
27
+ - lib/code_keeper/formatter.rb
28
+
29
+ Naming/HeredocDelimiterNaming:
30
+ Enabled: false
31
+
32
+ Metrics/MethodLength:
33
+ Max: 20
34
+ Exclude:
35
+ - lib/code_keeper/formatter.rb # because uses here doc
36
+
37
+ # Because TargetFileNotFoundError needs to be passed an argument to initializer.
38
+ Style/RaiseArgs:
39
+ Enabled: false
40
+
41
+ Style/Documentation:
42
+ Exclude:
43
+ - lib/code_keeper.rb # Does not need documentation.
44
+
45
+ Metrics/AbcSize:
46
+ Max: 25
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at dev.biibiebi@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in code_keeper.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ code_keeper (0.1.0)
5
+ parallel
6
+ rubocop
7
+ rubocop-ast
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ ast (2.4.2)
13
+ coderay (1.1.3)
14
+ diff-lcs (1.4.4)
15
+ method_source (0.9.2)
16
+ parallel (1.20.1)
17
+ parser (3.0.1.0)
18
+ ast (~> 2.4.1)
19
+ pry (0.12.2)
20
+ coderay (~> 1.1.0)
21
+ method_source (~> 0.9.0)
22
+ pry-nav (0.3.0)
23
+ pry (>= 0.9.10, < 0.13.0)
24
+ rainbow (3.0.0)
25
+ rake (13.0.3)
26
+ regexp_parser (2.1.1)
27
+ rexml (3.2.5)
28
+ rspec (3.10.0)
29
+ rspec-core (~> 3.10.0)
30
+ rspec-expectations (~> 3.10.0)
31
+ rspec-mocks (~> 3.10.0)
32
+ rspec-core (3.10.1)
33
+ rspec-support (~> 3.10.0)
34
+ rspec-expectations (3.10.1)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.10.0)
37
+ rspec-mocks (3.10.2)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.10.0)
40
+ rspec-support (3.10.2)
41
+ rubocop (1.13.0)
42
+ parallel (~> 1.10)
43
+ parser (>= 3.0.0.0)
44
+ rainbow (>= 2.2.2, < 4.0)
45
+ regexp_parser (>= 1.8, < 3.0)
46
+ rexml
47
+ rubocop-ast (>= 1.2.0, < 2.0)
48
+ ruby-progressbar (~> 1.7)
49
+ unicode-display_width (>= 1.4.0, < 3.0)
50
+ rubocop-ast (1.4.1)
51
+ parser (>= 2.7.1.5)
52
+ ruby-progressbar (1.11.0)
53
+ unicode-display_width (2.0.0)
54
+
55
+ PLATFORMS
56
+ x86_64-linux
57
+
58
+ DEPENDENCIES
59
+ code_keeper!
60
+ parallel
61
+ pry
62
+ pry-nav
63
+ rake (~> 13.0)
64
+ rspec (~> 3.0)
65
+ rubocop
66
+ rubocop-ast
67
+
68
+ BUNDLED WITH
69
+ 2.2.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Yusuke Ebihara
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # CodeKeeper
2
+ The CodeKeeper measures metrics especially about complexity and size of Ruby files, aiming to be a Ruby version of [gmetrics](https://github.com/dx42/gmetrics)
3
+
4
+ Mesuring metrics leads to keep codebase simple and clean, and I name the gem CodeKeeper.
5
+
6
+ Now CodeKeeper supports the cyclomatic complexity. The scores are output to stdout.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'code_keeper'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install code_keeper
23
+
24
+ ## Usage
25
+ Run CodeKeeper and you get scores of metrics from stdout like
26
+
27
+ ```rb
28
+ $ bundle exec code_keeper ./app/models
29
+ Scores:
30
+
31
+ Metric: cyclomatic_complexity
32
+ Filename: app/models/admin.rb
33
+ Score: 1
34
+ ---
35
+ Metric: cyclomatic_complexity
36
+ Filename: app/models/user.rb
37
+ Score: 23
38
+ ---
39
+ ```
40
+
41
+ ### Run CodeKeeper
42
+ To measure metrics of all the ruby files recursively in the current directory, run
43
+
44
+ ```rb
45
+ $ bundle exec code_keeper ./
46
+ ```
47
+
48
+ You can specify a single file or multiple files.
49
+ ```rb
50
+ $ bundle exec code_keeper ./dir/a.rb ./dir/b.rb
51
+ ```
52
+
53
+ CodeKeeper makes you configure the following way:
54
+
55
+ ```rb
56
+ CodeKeeper.configure do |config|
57
+ # If you choose metrics, specify as follows:
58
+ config.metrics = [:cyclomatic_complexity]
59
+ # The number of threads. The default is 2. Executed sequentially if you set 1.
60
+ config.number_of_threads = 4
61
+ end
62
+ ```
63
+
64
+ ## Development
65
+
66
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
67
+
68
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ebihara99999/code_keeper. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/ebihara99999/code_keeper/blob/master/CODE_OF_CONDUCT.md).
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
77
+
78
+ ## Code of Conduct
79
+
80
+ Everyone interacting in the CodeKeeper project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ebihara99999/code_keeper/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "code_keeper"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/code_keeper/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "code_keeper"
7
+ spec.version = CodeKeeper::VERSION
8
+ spec.authors = ["Yusuke Ebihara"]
9
+ spec.email = ["dev.biibiebi@gmail.com"]
10
+
11
+ spec.summary = "Measures metrics about complexity and size."
12
+ spec.description = "The CodeKeeper measures metrics especially about complexity and size of Ruby files, aiming to be a Ruby version of gmetrics."
13
+ spec.homepage = "https://github.com/ebihara99999/code_keeper"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 3.0.0")
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/ebihara99999/code_keeper/"
19
+ spec.metadata["changelog_uri"] = "https://github.com/ebihara99999/code_keeper/blob/main/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ # Uncomment to register a new dependency of your gem
31
+ spec.add_dependency "parallel"
32
+ spec.add_dependency "rubocop"
33
+ spec.add_dependency "rubocop-ast"
34
+
35
+ # For more information and examples about making a new gem, checkout our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ end
data/exe/code_keeper ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ #!/usr/bin/env ruby
3
+
4
+ require 'code_keeper'
5
+
6
+ result = CodeKeeper::Cli.run(ARGV)
7
+ exit result
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "code_keeper/version"
4
+ require "rubocop"
5
+ require 'code_keeper/parser'
6
+ require 'code_keeper/finder'
7
+ require 'code_keeper/cli'
8
+ require 'code_keeper/formatter'
9
+ require 'code_keeper/config'
10
+ require 'code_keeper/scorer'
11
+ require 'code_keeper/result'
12
+ require 'code_keeper/metrics'
13
+ require 'code_keeper/metrics/cyclomatic_complexity'
14
+
15
+ module CodeKeeper
16
+ class << self
17
+ def configure
18
+ yield config
19
+ end
20
+
21
+ def config
22
+ @config ||= Config.new
23
+ end
24
+ end
25
+
26
+ # Raised when a file does not exist
27
+ class TargetFileNotFoundError < StandardError
28
+ def initialize(path)
29
+ msg = "The target file does not exist. Check the file path: #{path}."
30
+ super(msg)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodeKeeper
4
+ # Offers cli interface and execute measurement.
5
+ class Cli
6
+ SUCCESS_CODE = 0
7
+ GENERAL_ERROR_CODE = 1
8
+ ERROR_CODE = 2
9
+ INTERRUPTION_CODE = 128 + Signal.list['INT']
10
+
11
+ def self.run(paths)
12
+ if paths.empty?
13
+ puts 'Specify at least one argument, a file or a directory.'
14
+ return ERROR_CODE
15
+ end
16
+
17
+ result = CodeKeeper::Scorer.keep(paths)
18
+
19
+ puts ::CodeKeeper::Formatter.format(result)
20
+ SUCCESS_CODE
21
+ rescue Interrupt
22
+ puts 'Exiting...'
23
+ INTERRUPTION_CODE
24
+ rescue CodeKeeper::TargetFileNotFoundError => e
25
+ puts e.message
26
+ ERROR_CODE
27
+ rescue StandardError => e
28
+ puts e.message
29
+ GENERAL_ERROR_CODE
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodeKeeper
4
+ # Provide configuration
5
+ class Config
6
+ attr_accessor :metrics, :number_of_threads
7
+
8
+ def initialize
9
+ @metrics = [:cyclomatic_complexity]
10
+ @number_of_threads = 2
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodeKeeper
4
+ # Find ruby files and manage the list of files.
5
+ class Finder
6
+ attr_reader :file_paths
7
+
8
+ def initialize(paths)
9
+ @file_paths = search_recursively(paths.uniq).map do |path|
10
+ raise ::CodeKeeper::TargetFileNotFoundError.new(path) unless File.exist?(path)
11
+
12
+ path if FileTest.file?(path) && File.extname(path) == '.rb'
13
+ end.compact
14
+ end
15
+
16
+ private
17
+
18
+ def search_recursively(file_or_dir_paths)
19
+ checked = {}
20
+
21
+ file_or_dir_paths.each do |edge|
22
+ next if checked[:"#{edge}"]
23
+
24
+ checked[:"#{edge}"] = true
25
+
26
+ if FileTest.file?(edge)
27
+ file_or_dir_paths << edge unless file_or_dir_paths.include?(edge)
28
+ else
29
+ Dir.glob(("#{edge}/**/*")).each do |path|
30
+ file_or_dir_paths << path unless file_or_dir_paths.include?(path)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodeKeeper
4
+ # Format a result and make it human-readable.
5
+ class Formatter
6
+ class << self
7
+ def format(result)
8
+ title = "Scores:\n\n"
9
+ formatted_result = +title
10
+
11
+ result.scores.each_key do |metric|
12
+ result.scores[metric].each do |k, v|
13
+ formatted_result.concat(
14
+ <<~EOS
15
+ Metric: #{metric}
16
+ Filename: #{k}
17
+ Score: #{v}
18
+ ---
19
+ EOS
20
+ )
21
+ end
22
+ end
23
+ formatted_result
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'code_keeper/metrics/cyclomatic_complexity'
4
+
5
+ module CodeKeeper
6
+ # Manage config values of metrics and the correspond classes
7
+ module Metrics
8
+ MAPPINGS = { cyclomatic_complexity: CodeKeeper::Metrics::CyclomaticComplexity }.freeze
9
+ end
10
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodeKeeper
4
+ module Metrics
5
+ # Caluculate cyclomatic complexity
6
+ class CyclomaticComplexity
7
+ include ::RuboCop::Cop::Metrics::Utils::IteratingBlock
8
+ include ::RuboCop::Cop::Metrics::Utils::RepeatedCsendDiscount
9
+
10
+ CONSIDERED_NODES = %i[if while until for csend block block_pass rescue when and or or_asgnand_asgn].freeze
11
+
12
+ def initialize(file_path)
13
+ ps = Parser.parse(file_path)
14
+ @body = ps.ast
15
+ end
16
+
17
+ # returns score of cyclomatic complexity
18
+ def score
19
+ @body.each_node(:lvasgn, *CONSIDERED_NODES).reduce(1) do |score, node|
20
+ next score if !iterating_block?(node) || node.lvasgn_type?
21
+ next score if node.csend_type? && discount_for_repeated_csend?(node)
22
+
23
+ next 1 + score
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodeKeeper
4
+ # Search and parse ruby file
5
+ class Parser
6
+ attr_reader :processed_source
7
+
8
+ def initialize(file_path)
9
+ source = File.read(File.expand_path(file_path))
10
+ @processed_source = ::RuboCop::AST::ProcessedSource.new(source, 3.0)
11
+ rescue Errno::ENOENT
12
+ raise TargetFileNotFoundError, "The target file does not exist. Check the file path: #{file_path}."
13
+ end
14
+
15
+ class << self
16
+ def parse(file_path)
17
+ parser = new(file_path)
18
+ parser.processed_source
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodeKeeper
4
+ # Store results of each score.
5
+ class Result
6
+ attr_reader :scores
7
+
8
+ def initialize
9
+ @scores = CodeKeeper.config.metrics.map { |key| [key, {}] }.to_h
10
+ end
11
+
12
+ def add(metric, path, score)
13
+ scores[:"#{metric}"].store(path, score)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodeKeeper
4
+ # Run and store score of metrics.
5
+ class Scorer
6
+ class << self
7
+ def keep(paths)
8
+ result = CodeKeeper::Result.new
9
+ metrics = result.scores.keys
10
+ ruby_file_paths = Finder.new(paths).file_paths
11
+ num_threads = CodeKeeper.config.number_of_threads
12
+
13
+ # NOTE: If the configuration says no concurrent execution, the parallel gem is not used.
14
+ # `in_threads: 1` makes 2 threads, a sleep_forever thread and the main thread.
15
+ if num_threads == 1
16
+ ruby_file_paths.each do |path|
17
+ metrics.each do |metric|
18
+ result.add(:cyclomatic_complexity, path, ::CodeKeeper::Metrics::MAPPINGS[metric].new(path).score)
19
+ end
20
+ end
21
+ else
22
+ Parallel.map(ruby_file_paths, in_threads: num_threads) do |path|
23
+ metrics.each do |metric|
24
+ result.add(:cyclomatic_complexity, path, ::CodeKeeper::Metrics::MAPPINGS[metric].new(path).score)
25
+ end
26
+ end
27
+ end
28
+
29
+ result
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CodeKeeper
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: code_keeper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yusuke Ebihara
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-08-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: parallel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop-ast
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: The CodeKeeper measures metrics especially about complexity and size
56
+ of Ruby files, aiming to be a Ruby version of gmetrics.
57
+ email:
58
+ - dev.biibiebi@gmail.com
59
+ executables:
60
+ - code_keeper
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".github/workflows/main.yml"
65
+ - ".gitignore"
66
+ - ".rspec"
67
+ - ".rubocop.yml"
68
+ - CODE_OF_CONDUCT.md
69
+ - Gemfile
70
+ - Gemfile.lock
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - bin/console
75
+ - bin/setup
76
+ - code_keeper.gemspec
77
+ - exe/code_keeper
78
+ - lib/code_keeper.rb
79
+ - lib/code_keeper/cli.rb
80
+ - lib/code_keeper/config.rb
81
+ - lib/code_keeper/finder.rb
82
+ - lib/code_keeper/formatter.rb
83
+ - lib/code_keeper/metrics.rb
84
+ - lib/code_keeper/metrics/cyclomatic_complexity.rb
85
+ - lib/code_keeper/parser.rb
86
+ - lib/code_keeper/result.rb
87
+ - lib/code_keeper/scorer.rb
88
+ - lib/code_keeper/version.rb
89
+ homepage: https://github.com/ebihara99999/code_keeper
90
+ licenses:
91
+ - MIT
92
+ metadata:
93
+ homepage_uri: https://github.com/ebihara99999/code_keeper
94
+ source_code_uri: https://github.com/ebihara99999/code_keeper/
95
+ changelog_uri: https://github.com/ebihara99999/code_keeper/blob/main/CHANGELOG.md
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: 3.0.0
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubygems_version: 3.2.3
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Measures metrics about complexity and size.
115
+ test_files: []