Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rubocop-rails-omakase to new Rails applications #50486

Merged
merged 15 commits into from
Dec 30, 2023
13 changes: 13 additions & 0 deletions railties/lib/rails/generators/app_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ def self.add_shared_options_for(name)
class_option :skip_dev_gems, type: :boolean, default: nil,
desc: "Skip development gems (e.g., web-console)"

class_option :skip_rubocop, type: :boolean, default: nil,
desc: "Skip rubocop-rails-omakase gem"

class_option :dev, type: :boolean, default: nil,
desc: "Set up the #{name} with Gemfile pointing to your Rails checkout"

Expand Down Expand Up @@ -379,6 +382,10 @@ def skip_propshaft?
skip_asset_pipeline? || options[:asset_pipeline] != "propshaft"
end

def skip_rubocop?
options[:skip_rubocop]
end


class GemfileEntry < Struct.new(:name, :version, :comment, :options, :commented_out)
def initialize(name, version, comment, options = {}, commented_out = false)
Expand Down Expand Up @@ -710,6 +717,12 @@ def run_css
end
end

def run_rubocop
return if skip_rubocop?

run "bin/rubocop"
end

def add_bundler_platforms
if bundle_install?
# The vast majority of Rails apps will be deployed on `x86_64-linux`.
Expand Down
13 changes: 12 additions & 1 deletion railties/lib/rails/generators/rails/app/app_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def dockerfiles
chmod "bin/docker-entrypoint", 0755 & ~File.umask, verbose: false
end

def rubocop
template ".rubocop.yml"
dhh marked this conversation as resolved.
Show resolved Hide resolved
end

def version_control
if !options[:skip_git] && !options[:pretend]
run git_init_command, capture: options[:quiet], abort_on_failure: false
Expand All @@ -98,7 +102,8 @@ def app
end

def bin
directory "bin" do |content|
options = skip_rubocop? ? { exclude_pattern: /rubocop/ } : {}
directory "bin", **options do |content|
"#{shebang}\n" + content
end
chmod "bin", 0755 & ~File.umask, verbose: false
Expand Down Expand Up @@ -367,6 +372,11 @@ def create_dockerfiles
build(:dockerfiles)
end

def create_rubocop_file
return if skip_rubocop?
build(:rubocop)
end

def create_config_files
build(:config)
end
Expand Down Expand Up @@ -544,6 +554,7 @@ def finish_template
public_task :run_javascript
public_task :run_hotwire
public_task :run_css
public_task :run_rubocop

def run_after_bundle_callbacks
@after_bundle_callbacks.each(&:call)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Omakase Ruby styling for Rails
inherit_gem: { rubocop-rails-omakase: rubocop.yml }
dhh marked this conversation as resolved.
Show resolved Hide resolved
dhh marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions railties/lib/rails/generators/rails/app/templates/Gemfile.tt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ group :development do
# Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
# gem "rack-mini-profiler"

<%- end -%>
<%- unless options.skip_rubocop? -%>
# Omakase Ruby styling for Rails applications [https://github.com/rails/rubocop-rails-omakase/tree/main]
dhh marked this conversation as resolved.
Show resolved Hide resolved
gem "rubocop-rails-omakase", require: false

<%- end -%>
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "rubygems"
require "bundler/setup"

# explicit rubocop config increases performance slightly while avoiding config confusion.
ARGV.unshift("--config", File.expand_path("../.rubocop.yml", __dir__))

load Gem.bin_path("rubocop", "rubocop")
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module <%= app_const_base %>
# Please, add to the `ignore` list any other `lib` subdirectories that do
# not contain `.rb` files, or that should not be reloaded or eager loaded.
# Common ones are `templates`, `generators`, or `middleware`, for example.
config.autoload_lib(ignore: %w(assets tasks))
config.autoload_lib(ignore: %w[assets tasks])

# Configuration for the application, engines, and railties goes here.
#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "test_helper"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome, screen_size: [1400, 1400]
driven_by :selenium, using: :chrome, screen_size: [ 1400, 1400 ]
end
15 changes: 15 additions & 0 deletions railties/test/generators/app_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
.gitattributes
.gitignore
.dockerignore
.rubocop.yml
.ruby-version
README.md
Gemfile
Expand Down Expand Up @@ -39,6 +40,7 @@
bin/docker-entrypoint
bin/rails
bin/rake
bin/rubocop
bin/setup
config/application.rb
config/boot.rb
Expand Down Expand Up @@ -623,6 +625,19 @@ def test_inclusion_of_a_debugger
end
end

def test_inclusion_of_rubocop
run_generator
assert_gem "rubocop-rails-omakase"
end

def test_rubocop_is_skipped_if_required
run_generator [destination_root, "--skip-rubocop"]

assert_no_gem "rubocop"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be rubocop-rails-omakase, instead of rubocop?

-assert_no_gem "rubocop"
+assert_no_gem "rubocop-rails-omakase"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@koic You're right. Raising a PR.

assert_no_file "bin/rubocop"
assert_no_file ".rubocop.yml"
end

def test_usage_read_from_file
assert_called(File, :read, returns: "USAGE FROM FILE") do
assert_equal "USAGE FROM FILE", Rails::Generators::AppGenerator.desc
Expand Down