From 5b62b80fae3cc5f2def9b623f05e4d2d9b2bedb0 Mon Sep 17 00:00:00 2001 From: Facundo Espinosa Date: Sun, 31 Dec 2023 18:44:30 -0300 Subject: [PATCH] Default to creating git pre-commit --- railties/CHANGELOG.md | 4 ++++ railties/lib/rails/generators/rails/app/app_generator.rb | 2 +- .../rails/app/templates/bin/hooks/pre-commit.tt | 1 + .../rails/generators/rails/app/templates/bin/setup.tt | 9 ++++++++- railties/test/generators/app_generator_test.rb | 5 +++++ 5 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 railties/lib/rails/generators/rails/app/templates/bin/hooks/pre-commit.tt diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 250677233754c..dbba452295543 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,7 @@ +* Add Git pre commiter 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..4292795341f3b 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 Commiter ==" + 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/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