diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 250677233754c..181af80764f24 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,7 @@ +* Add Git pre committer to by default when rubocop is present + + *faqndo97* + * Add brakeman gem by default for static analysis of security vulnerabilities. Allow skipping with --skip-brakeman option. *vipulnsward* diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index 7c4d3d576ce71..1a02efd91a632 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -102,7 +102,7 @@ def app end def bin - exclude_pattern = Regexp.union([(/rubocop/ if skip_rubocop?), (/brakeman/ if skip_brakeman?)].compact) + exclude_pattern = Regexp.union([(/rubocop|hooks/ if skip_rubocop?), (/brakeman/ if skip_brakeman?)].compact) directory "bin", { exclude_pattern: exclude_pattern } do |content| "#{shebang}\n" + content end diff --git a/railties/lib/rails/generators/rails/app/templates/bin/hooks/pre-commit.tt b/railties/lib/rails/generators/rails/app/templates/bin/hooks/pre-commit.tt new file mode 100644 index 0000000000000..57c0ee258cf64 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/bin/hooks/pre-commit.tt @@ -0,0 +1 @@ +system("bin/rubocop") diff --git a/railties/lib/rails/generators/rails/app/templates/bin/setup.tt b/railties/lib/rails/generators/rails/app/templates/bin/setup.tt index 4ab9814459d17..fcb2c1240dbf4 100644 --- a/railties/lib/rails/generators/rails/app/templates/bin/setup.tt +++ b/railties/lib/rails/generators/rails/app/templates/bin/setup.tt @@ -3,6 +3,8 @@ require "fileutils" # path to your application root. APP_ROOT = File.expand_path("..", __dir__) +HOOKS_PATH = File.expand_path("../bin/hooks", __dir__) + def system!(*args) system(*args, exception: true) end @@ -12,7 +14,12 @@ FileUtils.chdir APP_ROOT do # This script is idempotent, so that you can run it at any time and get an expectable outcome. # Add necessary setup steps to this file. - puts "== Installing dependencies ==" +<% unless options.skip_rubocop? -%> + puts "== Installing Pre Committer ==" + system("git config core.hooksPath #{HOOKS_PATH}") +<% end -%> + + puts "\n== Installing dependencies ==" system! "gem install bundler --conservative" system("bundle check") || system!("bundle install") <% if using_node? -%> diff --git a/railties/test/application/bin_setup_test.rb b/railties/test/application/bin_setup_test.rb index 89ce2337cec04..962818f5af406 100644 --- a/railties/test/application/bin_setup_test.rb +++ b/railties/test/application/bin_setup_test.rb @@ -49,6 +49,8 @@ def test_bin_setup_output output.gsub!(/^.*warning:\s.*\n/, "") assert_equal(<<~OUTPUT, output) + == Installing Pre Commiter == + == Installing dependencies == The Gemfile's dependencies are satisfied diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 5967611b5095f..d021fe17d759d 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -628,7 +628,11 @@ def test_inclusion_of_a_debugger def test_inclusion_of_rubocop run_generator + assert_gem "rubocop-rails-omakase" + assert_file "bin/rubocop" + assert_directory "bin/hooks" + assert_file ".rubocop.yml" end def test_rubocop_is_skipped_if_required @@ -636,6 +640,7 @@ def test_rubocop_is_skipped_if_required assert_no_gem "rubocop" assert_no_file "bin/rubocop" + assert_no_directory "bin/hooks" assert_no_file ".rubocop.yml" end