From 7bd143dafcc904d788619b829522a27233c83e9b Mon Sep 17 00:00:00 2001 From: Chuck Date: Tue, 7 Nov 2023 09:30:52 -0500 Subject: [PATCH 1/3] Update Rails version check if Rails 7 or newer is not being used. (#199) * Update Rails version check if Rails 7 or newer is not being used. * Add check for rails 6 edge case * Update template.rb * Update template.rb --------- Co-authored-by: Chris Oliver --- template.rb | 56 +++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/template.rb b/template.rb index c5caf22..e5ddc69 100644 --- a/template.rb +++ b/template.rb @@ -25,12 +25,24 @@ def add_template_repository_to_source_path end end +def read_gemfile? + File.open("Gemfile").each_line do |line| + return true if line.strip.start_with?("rails") && line.include?("6.") + end +end + def rails_version - @rails_version ||= Gem::Version.new(Rails::VERSION::STRING) + @rails_version ||= Gem::Version.new(Rails::VERSION::STRING) || read_gemfile? +end + +def rails_7_or_newer? + Gem::Requirement.new(">= 7.0.0.alpha").satisfied_by? rails_version end -def rails_6_or_newer? - Gem::Requirement.new(">= 6.0.0.alpha").satisfied_by? rails_version +unless rails_7_or_newer? + say "\nJumpstart requires Rails 7 or newer. You are using #{rails_version}.", :green + say "Please remove partially installed Jumpstart files #{original_app_name} and try again.", :green + exit 1 end def add_gems @@ -69,13 +81,11 @@ def add_users # Set admin default to false in_root do - migration = Dir.glob("db/migrate/*").max_by{ |f| File.mtime(f) } + migration = Dir.glob("db/migrate/*").max_by { |f| File.mtime(f) } gsub_file migration, /:admin/, ":admin, default: false" end - if Gem::Requirement.new("> 5.2").satisfied_by? rails_version - gsub_file "config/initializers/devise.rb", / # config.secret_key = .+/, " config.secret_key = Rails.application.credentials.secret_key_base" - end + gsub_file "config/initializers/devise.rb", / # config.secret_key = .+/, " config.secret_key = Rails.application.credentials.secret_key_base" inject_into_file("app/models/user.rb", "omniauthable, :", after: "devise :") end @@ -120,21 +130,21 @@ def add_sidekiq environment "config.active_job.queue_adapter = :sidekiq" insert_into_file "config/routes.rb", - "require 'sidekiq/web'\n\n", - before: "Rails.application.routes.draw do" + "require 'sidekiq/web'\n\n", + before: "Rails.application.routes.draw do" content = <<~RUBY - authenticate :user, lambda { |u| u.admin? } do - mount Sidekiq::Web => '/sidekiq' - - namespace :madmin do - resources :impersonates do - post :impersonate, on: :member - post :stop_impersonating, on: :collection - end - end - end - RUBY + authenticate :user, lambda { |u| u.admin? } do + mount Sidekiq::Web => '/sidekiq' + + namespace :madmin do + resources :impersonates do + post :impersonate, on: :member + post :stop_impersonating, on: :collection + end + end + end + RUBY insert_into_file "config/routes.rb", "#{content}\n", after: "Rails.application.routes.draw do\n" end @@ -170,7 +180,7 @@ def add_whenever def add_friendly_id generate "friendly_id" - insert_into_file( Dir["db/migrate/**/*friendly_id_slugs.rb"].first, "[5.2]", after: "ActiveRecord::Migration") + insert_into_file(Dir["db/migrate/**/*friendly_id_slugs.rb"].first, "[5.2]", after: "ActiveRecord::Migration") end def add_sitemap @@ -208,10 +218,6 @@ def gem_exists?(name) IO.read("Gemfile") =~ /^\s*gem ['"]#{name}['"]/ end -unless rails_6_or_newer? - puts "Please use Rails 6.0 or newer to create a Jumpstart application" -end - # Main setup add_template_repository_to_source_path default_to_esbuild From 80980bce233bbebec78a1b13fdf3113d9eda31bd Mon Sep 17 00:00:00 2001 From: Andy Waite <13400+andyw8@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:31:56 -0500 Subject: [PATCH 2/3] Run `yarn build` as part of template test (#173) Co-authored-by: Andy Waite --- test/template_test.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/template_test.rb b/test/template_test.rb index a71004a..234dd1b 100644 --- a/test/template_test.rb +++ b/test/template_test.rb @@ -10,10 +10,15 @@ def teardown end def test_generator_succeeds - output, err = capture_subprocess_io do + output, _err = capture_subprocess_io do system("DISABLE_SPRING=1 SKIP_GIT=1 rails new test_app -m template.rb") end assert_includes output, "Jumpstart app successfully created!" + + output, _err = capture_subprocess_io do + system("cd test_app && yarn build") + end + assert_includes output, "Done in " end # TODO: Fix these tests on CI so they don't fail on db:create From 74f6fa383385af866b0a1076e325e4ea90bd2635 Mon Sep 17 00:00:00 2001 From: Andy Waite <13400+andyw8@users.noreply.github.com> Date: Tue, 7 Nov 2023 09:52:38 -0500 Subject: [PATCH 3/3] Use `setup-rails` for CI (#185) Provides a default CI for new applications. --------- Co-authored-by: Andy Waite Co-authored-by: Chris Oliver --- github/workflows/verify.yml | 8 ++++++++ template.rb | 5 +++++ 2 files changed, 13 insertions(+) create mode 100644 github/workflows/verify.yml diff --git a/github/workflows/verify.yml b/github/workflows/verify.yml new file mode 100644 index 0000000..ee20a8f --- /dev/null +++ b/github/workflows/verify.yml @@ -0,0 +1,8 @@ +# See https://github.com/andyw8/setup-rails for more information + +name: Verify +on: [push] + +jobs: + verify: + uses: andyw8/setup-rails/.github/workflows/verify.yml@v1 \ No newline at end of file diff --git a/template.rb b/template.rb index e5ddc69..b20f8ca 100644 --- a/template.rb +++ b/template.rb @@ -210,6 +210,10 @@ def add_esbuild_script end end +def add_github_actions_ci + copy_file "github/workflows/verify.yml", ".github/workflows/verify.yml" +end + def add_gem(name, *options) gem(name, *options) unless gem_exists?(name) end @@ -237,6 +241,7 @@ def gem_exists?(name) add_whenever add_sitemap add_announcements_css + add_github_actions_ci rails_command "active_storage:install" # Make sure Linux is in the Gemfile.lock for deploying