Skip to content

Commit 0fb5f67

Browse files
authored
Add rubocop-rails-omakase to new Rails applications (rails#50486)
* Add rubocop to new rails app generator This setups a basic rubocop config for new rails apps using the `rubocop-rails-omakase` gem: https://github.com/rails/rubocop-rails-omakase It can be skipped with the `--skip-rubocop` flag.
1 parent 7b9e9ee commit 0fb5f67

File tree

9 files changed

+59
-3
lines changed

9 files changed

+59
-3
lines changed

railties/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Add RuboCop with rules from rubocop-rails-omakase by default. Skip with --skip-rubocop.
2+
3+
*DHH* and *zzak*
4+
15
* Use `bin/rails runner --skip-executor` option to not wrap the runner script
26
with an Executor.
37

railties/lib/rails/generators/app_base.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ def self.add_shared_options_for(name)
100100
class_option :skip_dev_gems, type: :boolean, default: nil,
101101
desc: "Skip development gems (e.g., web-console)"
102102

103+
class_option :skip_rubocop, type: :boolean, default: nil,
104+
desc: "Skip RuboCop setup"
105+
103106
class_option :dev, type: :boolean, default: nil,
104107
desc: "Set up the #{name} with Gemfile pointing to your Rails checkout"
105108

@@ -379,6 +382,10 @@ def skip_propshaft?
379382
skip_asset_pipeline? || options[:asset_pipeline] != "propshaft"
380383
end
381384

385+
def skip_rubocop?
386+
options[:skip_rubocop]
387+
end
388+
382389

383390
class GemfileEntry < Struct.new(:name, :version, :comment, :options, :commented_out)
384391
def initialize(name, version, comment, options = {}, commented_out = false)

railties/lib/rails/generators/rails/app/app_generator.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ def dockerfiles
8282
chmod "bin/docker-entrypoint", 0755 & ~File.umask, verbose: false
8383
end
8484

85+
def rubocop
86+
template "rubocop.yml", ".rubocop.yml"
87+
end
88+
8589
def version_control
8690
if !options[:skip_git] && !options[:pretend]
8791
run git_init_command, capture: options[:quiet], abort_on_failure: false
@@ -98,7 +102,8 @@ def app
98102
end
99103

100104
def bin
101-
directory "bin" do |content|
105+
options = skip_rubocop? ? { exclude_pattern: /rubocop/ } : {}
106+
directory "bin", **options do |content|
102107
"#{shebang}\n" + content
103108
end
104109
chmod "bin", 0755 & ~File.umask, verbose: false
@@ -367,6 +372,11 @@ def create_dockerfiles
367372
build(:dockerfiles)
368373
end
369374

375+
def create_rubocop_file
376+
return if skip_rubocop?
377+
build(:rubocop)
378+
end
379+
370380
def create_config_files
371381
build(:config)
372382
end

railties/lib/rails/generators/rails/app/templates/Gemfile.tt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ end
4040
<% end -%>
4141

4242
group :development do
43+
<%- unless options.skip_rubocop? -%>
44+
# Omakase Ruby styling [https://github.com/rails/rubocop-rails-omakase/]
45+
gem "rubocop-rails-omakase", require: false
46+
47+
<%- end -%>
4348
<%- unless options.api? || options.skip_dev_gems? -%>
4449
# Use console on exceptions pages [https://github.com/rails/web-console]
4550
gem "web-console"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "rubygems"
2+
require "bundler/setup"
3+
4+
# explicit rubocop config increases performance slightly while avoiding config confusion.
5+
ARGV.unshift("--config", File.expand_path("../.rubocop.yml", __dir__))
6+
7+
load Gem.bin_path("rubocop", "rubocop")

railties/lib/rails/generators/rails/app/templates/config/application.rb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module <%= app_const_base %>
1818
# Please, add to the `ignore` list any other `lib` subdirectories that do
1919
# not contain `.rb` files, or that should not be reloaded or eager loaded.
2020
# Common ones are `templates`, `generators`, or `middleware`, for example.
21-
config.autoload_lib(ignore: %w(assets tasks))
21+
config.autoload_lib(ignore: %w[assets tasks])
2222

2323
# Configuration for the application, engines, and railties goes here.
2424
#
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Omakase Ruby styling for Rails
2+
inherit_gem: { rubocop-rails-omakase: rubocop.yml }
3+
4+
# Overwrite or add rules to create your own house style
5+
#
6+
# # Use `[a, [b, c]]` not `[ a, [ b, c ] ]`
7+
# Layout/SpaceInsideArrayLiteralBrackets:
8+
# Enabled: false
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require "test_helper"
22

33
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
4-
driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
4+
driven_by :selenium, using: :chrome, screen_size: [ 1400, 1400 ]
55
end

railties/test/generators/app_generator_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
.gitattributes
99
.gitignore
1010
.dockerignore
11+
.rubocop.yml
1112
.ruby-version
1213
README.md
1314
Gemfile
@@ -39,6 +40,7 @@
3940
bin/docker-entrypoint
4041
bin/rails
4142
bin/rake
43+
bin/rubocop
4244
bin/setup
4345
config/application.rb
4446
config/boot.rb
@@ -623,6 +625,19 @@ def test_inclusion_of_a_debugger
623625
end
624626
end
625627

628+
def test_inclusion_of_rubocop
629+
run_generator
630+
assert_gem "rubocop-rails-omakase"
631+
end
632+
633+
def test_rubocop_is_skipped_if_required
634+
run_generator [destination_root, "--skip-rubocop"]
635+
636+
assert_no_gem "rubocop"
637+
assert_no_file "bin/rubocop"
638+
assert_no_file ".rubocop.yml"
639+
end
640+
626641
def test_usage_read_from_file
627642
assert_called(File, :read, returns: "USAGE FROM FILE") do
628643
assert_equal "USAGE FROM FILE", Rails::Generators::AppGenerator.desc

0 commit comments

Comments
 (0)