Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Merge #6455
Browse files Browse the repository at this point in the history
6455: [CLI::Gem] Add a --rubocop option r=deivid-rodriguez a=segiddins

Based upon #6451.

### What was the end-user problem that led to this PR?

The problem was I always have to remember how to add RuboCop to my Rakefile when I set up a new gem.

### What was your diagnosis of the problem?

My diagnosis was that RuboCop has become enough of a community standard that it makes sense to offer it in `bundle gem`.

### What is your fix for the problem, implemented in this PR?

My fix adds an option to `bundle gem` to add in RuboCop, in the same way options like `:coc`and `:mit` are handled.

### Why did you choose this fix out of the possible options?

I chose this fix because it does not require bundler have an opinion on _how_ rubocop is configured.

Co-authored-by: Samuel Giddins <[email protected]>
  • Loading branch information
bundlerbot and segiddins committed Nov 8, 2019
2 parents 6f05c53 + f821c75 commit 03e08a0
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/bundler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ def viz
method_option :ext, :type => :boolean, :default => false, :desc => "Generate the boilerplate for C extension code"
method_option :git, :type => :boolean, :default => true, :desc => "Initialize a git repo inside your library."
method_option :mit, :type => :boolean, :desc => "Generate an MIT license file. Set a default with `bundle config set gem.mit true`."
method_option :rubocop, :type => :boolean, :desc => "Add rubocop to the generated Rakefile and gemspec. Set a default with `bundle config set gem.rubocop true`."
method_option :test, :type => :string, :lazy_default => "rspec", :aliases => "-t", :banner => "rspec",
:desc => "Generate a test directory for your library, either rspec or minitest. Set a default with `bundle config set gem.test rspec`."
def gem(name)
Expand Down
11 changes: 10 additions & 1 deletion lib/bundler/cli/gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def run
end
end

config[:test_task] = config[:test] == "minitest" ? "test" : "spec"
config[:test_task] = config[:test] == "minitest" ? :test : :spec

if ask_and_set(:mit, "Do you want to license your code permissively under the MIT license?",
"This means that any other developer or company will be legally allowed to use your code " \
Expand All @@ -124,6 +124,15 @@ def run
templates.merge!("CODE_OF_CONDUCT.md.tt" => "CODE_OF_CONDUCT.md")
end

if ask_and_set(:rubocop, "Do you want to add rubocop as a dependency for gems you generate?",
"RuboCop is a static code analyzer that has out-of-the-box rules for many " \
"of the guidelines in the community style guide. " \
"For more information, see the RuboCop docs (https://docs.rubocop.org/en/stable/) " \
"and the Ruby Style Guides (https://github.com/rubocop-hq/ruby-style-guide).")
config[:rubocop] = true
Bundler.ui.info "RuboCop enabled in config"
end

templates.merge!("exe/newgem.tt" => "exe/#{name}") if config[:exe]

if options[:ext]
Expand Down
3 changes: 3 additions & 0 deletions lib/bundler/templates/newgem/Gemfile.tt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ gem "rake-compiler"
<%- if config[:test] -%>
gem "<%= config[:test] %>", "~> <%= config[:test_framework_version] %>"
<%- end -%>
<%- if config[:rubocop] -%>
gem "rubocop"
<%- end -%>
13 changes: 10 additions & 3 deletions lib/bundler/templates/newgem/Rakefile.tt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "bundler/gem_tasks"
<% default_task_names = [config[:test_task]] -%>
<% if config[:test] == "minitest" -%>
require "rake/testtask"

Expand All @@ -13,8 +14,16 @@ require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

<% end -%>
<% if config[:rubocop] -%>
<% default_task_names << :rubocop -%>
require "rubocop/rake_task"

RuboCop::RakeTask.new

<% end -%>
<% if config[:ext] -%>
<% default_task_names.unshift(:clobber, :compile) -%>
require "rake/extensiontask"

task :build => :compile
Expand All @@ -23,7 +32,5 @@ Rake::ExtensionTask.new("<%= config[:underscored_name] %>") do |ext|
ext.lib_dir = "lib/<%= config[:namespaced_path] %>"
end

task :default => [:clobber, :compile, :<%= config[:test_task] %>]
<% else -%>
task :default => :<%= config[:test_task] %>
<% end -%>
task :default => <%= default_task_names.size == 1 ? default_task_names.first.inspect : default_task_names.inspect %>
66 changes: 64 additions & 2 deletions spec/commands/newgem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,55 @@ def gem_skeleton_assertions
end
end

shared_examples_for "--rubocop flag" do
before do
bundle! "gem #{gem_name} --rubocop"
end

it "generates a gem skeleton with rubocop" do
gem_skeleton_assertions
expect(bundled_app("test-gem/Rakefile")).to read_as(
include('require "rubocop/rake_task"').
and(include("RuboCop::RakeTask.new").
and(match(/:default.+:rubocop/)))
)
end

it "includes rubocop in generated Gemfile" do
Dir.chdir(bundled_app(gem_name)) do
builder = Bundler::Dsl.new
builder.eval_gemfile(bundled_app("#{gem_name}/Gemfile"))
builder.dependencies
rubocop_dep = builder.dependencies.find {|d| d.name == "rubocop" }
expect(rubocop_dep).not_to be_nil
end
end
end

shared_examples_for "--no-rubocop flag" do
define_negated_matcher :exclude, :include

before do
bundle! "gem #{gem_name} --no-rubocop"
end

it "generates a gem skeleton without rubocop" do
gem_skeleton_assertions
expect(bundled_app("test-gem/Rakefile")).to read_as(exclude("rubocop"))
expect(bundled_app("test-gem/#{gem_name}.gemspec")).to read_as(exclude("rubocop"))
end

it "does not include rubocop in generated Gemfile" do
Dir.chdir(bundled_app(gem_name)) do
builder = Bundler::Dsl.new
builder.eval_gemfile(bundled_app("#{gem_name}/Gemfile"))
builder.dependencies
rubocop_dep = builder.dependencies.find {|d| d.name == "rubocop" }
expect(rubocop_dep).to be_nil
end
end
end

context "README.md" do
context "git config github.user present" do
before do
Expand Down Expand Up @@ -518,7 +567,7 @@ def create_temporary_dir(dir)

context "with mit option in bundle config settings set to true" do
before do
global_config "BUNDLE_GEM__MIT" => "true", "BUNDLE_GEM__TEST" => "false", "BUNDLE_GEM__COC" => "false"
global_config "BUNDLE_GEM__MIT" => "true", "BUNDLE_GEM__TEST" => "false", "BUNDLE_GEM__RUBOCOP" => "false", "BUNDLE_GEM__COC" => "false"
end
it_behaves_like "--mit flag"
it_behaves_like "--no-mit flag"
Expand All @@ -531,7 +580,7 @@ def create_temporary_dir(dir)

context "with coc option in bundle config settings set to true" do
before do
global_config "BUNDLE_GEM__MIT" => "false", "BUNDLE_GEM__TEST" => "false", "BUNDLE_GEM__COC" => "true"
global_config "BUNDLE_GEM__MIT" => "false", "BUNDLE_GEM__TEST" => "false", "BUNDLE_GEM__RUBOCOP" => "false", "BUNDLE_GEM__COC" => "true"
end
it_behaves_like "--coc flag"
it_behaves_like "--no-coc flag"
Expand All @@ -541,6 +590,19 @@ def create_temporary_dir(dir)
it_behaves_like "--coc flag"
it_behaves_like "--no-coc flag"
end

context "with rubocop option in bundle config settings set to true" do
before do
global_config "BUNDLE_GEM__MIT" => "false", "BUNDLE_GEM__TEST" => "false", "BUNDLE_GEM__COC" => "false", "BUNDLE_GEM__RUBOCOP" => "true"
end
it_behaves_like "--rubocop flag"
it_behaves_like "--no-rubocop flag"
end

context "with rubocop option in bundle config settings set to false" do
it_behaves_like "--rubocop flag"
it_behaves_like "--no-rubocop flag"
end
end

context "gem naming with underscore" do
Expand Down

0 comments on commit 03e08a0

Please sign in to comment.