From 658715c2bb6cdf35b1521819210f2a4dc175db9b Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 29 Jan 2025 14:29:34 +0900 Subject: [PATCH] Pluginfy RuboCop Rake This PR makes RuboCop Rake support RuboCop's Plugin feature. It replaces the ad-hoc `Inject` with RuboCop plugins introduced in RuboCop 1.72. Additionally, since RuboCop already requires Ruby 2.7 or higher as its runtime, the `required_ruby_version` will be updated to 2.7. Follow-up https://github.com/rubocop/rubocop/pull/13792 --- .rubocop.yml | 6 ++++-- CHANGELOG.md | 1 + Gemfile | 2 +- README.md | 12 +++++++----- lib/rubocop-rake.rb | 4 +--- lib/rubocop/rake.rb | 6 ------ lib/rubocop/rake/inject.rb | 19 ------------------- lib/rubocop/rake/plugin.rb | 31 +++++++++++++++++++++++++++++++ rubocop-rake.gemspec | 6 ++++-- spec/spec_helper.rb | 2 -- 10 files changed, 49 insertions(+), 40 deletions(-) delete mode 100644 lib/rubocop/rake/inject.rb create mode 100644 lib/rubocop/rake/plugin.rb diff --git a/.rubocop.yml b/.rubocop.yml index ed98f5f..56698bd 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,8 +1,10 @@ inherit_from: .rubocop_todo.yml -require: - - rubocop/cop/internal_affairs +plugins: + - rubocop-internal_affairs - rubocop-rake + +require: - rubocop-rspec AllCops: diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ce46e5..9781fa0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## master (unreleased) +* [#58](https://github.com/rubocop/rubocop-rake/pull/58): Pluginfy RuboCop Rake. ([@koic][]) * [#57](https://github.com/rubocop/rubocop-rake/pull/57): Drop support Ruby 2.5 and 2.6 for runtime environment. ([@koic][]) ## 0.6.0 (2021-06-29) diff --git a/Gemfile b/Gemfile index 63e6a26..7123cb3 100644 --- a/Gemfile +++ b/Gemfile @@ -7,5 +7,5 @@ gemspec gem "rake", "~> 13.0" gem 'rspec' -gem 'rubocop', '>= 0.76' +gem 'rubocop', '>= 1.72.1' gem 'rubocop-rspec' diff --git a/README.md b/README.md index 3647297..cf38c47 100644 --- a/README.md +++ b/README.md @@ -31,13 +31,13 @@ ways to do this: Put this into your `.rubocop.yml`. ```yaml -require: rubocop-rake +plugins: rubocop-rake ``` Alternatively, use the following array notation when specifying multiple extensions. ```yaml -require: +plugins: - rubocop-other-extension - rubocop-rake ``` @@ -45,10 +45,13 @@ require: Now you can run `rubocop` and it will automatically load the RuboCop Rake cops together with the standard cops. +> [!NOTE] +> The plugin system is supported in RuboCop 1.72+. In earlier versions, use `require` instead of `plugins`. + ### Command line ```bash -rubocop --require rubocop-rake +rubocop --plugin rubocop-rake ``` ### Rake task @@ -57,7 +60,7 @@ rubocop --require rubocop-rake require 'rubocop/rake_task' RuboCop::RakeTask.new do |task| - task.requires << 'rubocop-rake' + task.plugins << 'rubocop-rake' end ``` @@ -71,4 +74,3 @@ To install this gem onto your local machine, run `bundle exec rake install`. To ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/rubocop/rubocop-rake. - diff --git a/lib/rubocop-rake.rb b/lib/rubocop-rake.rb index 4e8689d..8a148d6 100644 --- a/lib/rubocop-rake.rb +++ b/lib/rubocop-rake.rb @@ -4,9 +4,7 @@ require_relative 'rubocop/rake' require_relative 'rubocop/rake/version' -require_relative 'rubocop/rake/inject' - -RuboCop::Rake::Inject.defaults! +require_relative 'rubocop/rake/plugin' require_relative 'rubocop/cop/rake/helper/class_definition' require_relative 'rubocop/cop/rake/helper/on_task' diff --git a/lib/rubocop/rake.rb b/lib/rubocop/rake.rb index ed8b07f..d719116 100644 --- a/lib/rubocop/rake.rb +++ b/lib/rubocop/rake.rb @@ -6,11 +6,5 @@ module RuboCop # :nodoc: module Rake class Error < StandardError; end - - PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze - CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze - CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze - - private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT) end end diff --git a/lib/rubocop/rake/inject.rb b/lib/rubocop/rake/inject.rb deleted file mode 100644 index 2b472bc..0000000 --- a/lib/rubocop/rake/inject.rb +++ /dev/null @@ -1,19 +0,0 @@ -# frozen_string_literal: true - -# The original code is from https://github.com/rubocop/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb -# See https://github.com/rubocop/rubocop-rspec/blob/master/MIT-LICENSE.md -module RuboCop - module Rake - # Because RuboCop doesn't yet support plugins, we have to monkey patch in a - # bit of our configuration. - module Inject - def self.defaults! - path = CONFIG_DEFAULT.to_s - hash = ConfigLoader.send(:load_yaml_configuration, path) - config = Config.new(hash, path) - new_config = ConfigLoader.merge_with_default(config, path) - ConfigLoader.instance_variable_set(:@default_configuration, new_config) - end - end - end -end diff --git a/lib/rubocop/rake/plugin.rb b/lib/rubocop/rake/plugin.rb new file mode 100644 index 0000000..975dce1 --- /dev/null +++ b/lib/rubocop/rake/plugin.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'lint_roller' + +module RuboCop + module Rake + # A plugin that integrates RuboCop Rake with RuboCop's plugin system. + class Plugin < LintRoller::Plugin + def about + LintRoller::About.new( + name: 'rubocop-rake', + version: Version::STRING, + homepage: 'https://github.com/rubocop/rubocop-rake', + description: 'A RuboCop plugin for Rake.', + ) + end + + def supported?(context) + context.engine == :rubocop + end + + def rules(_context) + LintRoller::Rules.new( + type: :path, + config_format: :rubocop, + value: Pathname.new(__dir__).join('../../../config/default.yml'), + ) + end + end + end +end diff --git a/rubocop-rake.gemspec b/rubocop-rake.gemspec index 85a190e..af5c742 100644 --- a/rubocop-rake.gemspec +++ b/rubocop-rake.gemspec @@ -19,7 +19,8 @@ Gem::Specification.new do |spec| 'homepage_uri' => spec.homepage, 'source_code_uri' => spec.homepage, 'changelog_uri' => "https://github.com/rubocop/rubocop-rake/blob/master/CHANGELOG.md", - 'rubygems_mfa_required' => 'true' + 'rubygems_mfa_required' => 'true', + 'default_lint_roller_plugin' => 'RuboCop::Rake::Plugin' } # Specify which files should be added to the gem when it is released. @@ -31,5 +32,6 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] - spec.add_dependency 'rubocop', '~> 1.0' + spec.add_dependency 'lint_roller', '~> 1.1' + spec.add_dependency 'rubocop', '>= 1.72.1' end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 85577ac..812b1cf 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,8 +4,6 @@ require 'rubocop/rspec/support' RSpec.configure do |config| - config.include RuboCop::RSpec::ExpectOffense - config.disable_monkey_patching! config.raise_errors_for_deprecations! config.raise_on_warning = true