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 4ca56aa..49b568e 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_6_or_newer? - Gem::Requirement.new(">= 6.0.0.alpha").satisfied_by? rails_version +def rails_7_or_newer? + Gem::Requirement.new(">= 7.0.0.alpha").satisfied_by? rails_version +end + +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 @@ -171,7 +181,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 @@ -201,6 +211,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 @@ -209,10 +223,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 @@ -232,6 +242,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 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