2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-25 16:46:12 -04:00
|
|
|
require "rails/generators"
|
|
|
|
require "rails/generators/rails/app/app_generator"
|
|
|
|
|
|
|
|
module Rails
|
|
|
|
class AppUpdater # :nodoc:
|
|
|
|
class << self
|
|
|
|
def invoke_from_app_generator(method)
|
|
|
|
app_generator.send(method)
|
|
|
|
end
|
|
|
|
|
|
|
|
def app_generator
|
|
|
|
@app_generator ||= begin
|
|
|
|
gen = Rails::Generators::AppGenerator.new ["rails"], generator_options, destination_root: Rails.root
|
Avoid footguns in `rails app:update`
While upgrading a Rails 6 app to Rails 6.1, I noticed that `rails app:update` asks you to review some file changes that you'd basically never want to accept. In this PR, I propose we make the update task do a bit less, by not offering to overwrite files where the developer has almost certainly changed them from the default. Specific changes:
Don't replace the following files, as they change very rarely at the framework level, so if the user has changes they almost certainly intend to keep them:
- [config/boot.rb](https://github.com/rails/rails/commits/master/railties/lib/rails/generators/rails/app/templates/config/boot.rb.tt)
- [config/environment.rb](https://github.com/rails/rails/commits/master/railties/lib/rails/generators/rails/app/templates/config/environment.rb.tt)
- [config/storage.yml](https://github.com/rails/rails/commits/master/railties/lib/rails/generators/rails/app/templates/config/storage.yml.tt)
- [config/spring.rb](https://github.com/rails/rails/commits/master/railties/lib/rails/generators/rails/app/templates/config/spring.rb.tt)
- [config/cable.yml](https://github.com/rails/rails/commits/master/railties/lib/rails/generators/rails/app/templates/config/cable.yml.tt)
- [config/puma.rb](https://github.com/rails/rails/commits/master/railties/lib/rails/generators/rails/app/templates/config/puma.rb.tt)
- [config.ru](https://github.com/rails/rails/commits/master/railties/lib/rails/generators/rails/app/templates/config.ru.tt)
Don't overwrite the config/locales directory. There's basically no chance you'll want to replace your current `config/locales/en.yml` with a [hello world](https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/app/templates/config/locales/en.yml).
Don't replace config/routes.rb. It's so unlikely that the user will want to replace their routes file with [an empty file](https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/app/templates/config/routes.rb.tt).
With these changes, you will still be prompted to accept/decline changes to these files when doing an update:
- config/application.rb
- config/environments/{development|test|production}.rb
- All the default [initializers](https://github.com/rails/rails/tree/master/railties/lib/rails/generators/rails/app/templates/config/initializers) that you already have a copy of. I go back and forth on also opting some of these out, specifically `assets.rb`, `filter_parameter_logging.rb`, and `inflections.rb` which seem pretty likely to have been changed.
- All the default [binstubs](https://github.com/rails/rails/tree/master/railties/lib/rails/generators/rails/app/templates/bin) that you already have a copy of. I decided to still make the user review these, as bugs here are going to be annoying to debug, but it is good to use the latest versions if possible.
2021-02-09 13:10:19 -05:00
|
|
|
gen.send(:valid_const?) unless File.exist?(Rails.root.join("config", "application.rb"))
|
2017-06-25 16:46:12 -04:00
|
|
|
gen
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def generator_options
|
|
|
|
options = { api: !!Rails.application.config.api_only, update: true }
|
2018-10-21 16:08:43 -04:00
|
|
|
options[:skip_javascript] = !File.exist?(Rails.root.join("bin", "yarn"))
|
2017-08-06 16:41:31 -04:00
|
|
|
options[:skip_active_record] = !defined?(ActiveRecord::Railtie)
|
|
|
|
options[:skip_active_storage] = !defined?(ActiveStorage::Engine) || !defined?(ActiveRecord::Railtie)
|
|
|
|
options[:skip_action_mailer] = !defined?(ActionMailer::Railtie)
|
|
|
|
options[:skip_action_cable] = !defined?(ActionCable::Engine)
|
|
|
|
options[:skip_sprockets] = !defined?(Sprockets::Railtie)
|
|
|
|
options[:skip_puma] = !defined?(Puma)
|
2018-05-06 18:56:00 -04:00
|
|
|
options[:skip_bootsnap] = !defined?(Bootsnap)
|
2018-05-07 04:25:01 -04:00
|
|
|
options[:skip_spring] = !defined?(Spring)
|
Avoid footguns in `rails app:update`
While upgrading a Rails 6 app to Rails 6.1, I noticed that `rails app:update` asks you to review some file changes that you'd basically never want to accept. In this PR, I propose we make the update task do a bit less, by not offering to overwrite files where the developer has almost certainly changed them from the default. Specific changes:
Don't replace the following files, as they change very rarely at the framework level, so if the user has changes they almost certainly intend to keep them:
- [config/boot.rb](https://github.com/rails/rails/commits/master/railties/lib/rails/generators/rails/app/templates/config/boot.rb.tt)
- [config/environment.rb](https://github.com/rails/rails/commits/master/railties/lib/rails/generators/rails/app/templates/config/environment.rb.tt)
- [config/storage.yml](https://github.com/rails/rails/commits/master/railties/lib/rails/generators/rails/app/templates/config/storage.yml.tt)
- [config/spring.rb](https://github.com/rails/rails/commits/master/railties/lib/rails/generators/rails/app/templates/config/spring.rb.tt)
- [config/cable.yml](https://github.com/rails/rails/commits/master/railties/lib/rails/generators/rails/app/templates/config/cable.yml.tt)
- [config/puma.rb](https://github.com/rails/rails/commits/master/railties/lib/rails/generators/rails/app/templates/config/puma.rb.tt)
- [config.ru](https://github.com/rails/rails/commits/master/railties/lib/rails/generators/rails/app/templates/config.ru.tt)
Don't overwrite the config/locales directory. There's basically no chance you'll want to replace your current `config/locales/en.yml` with a [hello world](https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/app/templates/config/locales/en.yml).
Don't replace config/routes.rb. It's so unlikely that the user will want to replace their routes file with [an empty file](https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/app/templates/config/routes.rb.tt).
With these changes, you will still be prompted to accept/decline changes to these files when doing an update:
- config/application.rb
- config/environments/{development|test|production}.rb
- All the default [initializers](https://github.com/rails/rails/tree/master/railties/lib/rails/generators/rails/app/templates/config/initializers) that you already have a copy of. I go back and forth on also opting some of these out, specifically `assets.rb`, `filter_parameter_logging.rb`, and `inflections.rb` which seem pretty likely to have been changed.
- All the default [binstubs](https://github.com/rails/rails/tree/master/railties/lib/rails/generators/rails/app/templates/bin) that you already have a copy of. I decided to still make the user review these, as bugs here are going to be annoying to debug, but it is good to use the latest versions if possible.
2021-02-09 13:10:19 -05:00
|
|
|
options[:updating] = true
|
2017-06-25 16:46:12 -04:00
|
|
|
options
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|