提交 58036402 编写于 作者: Y yuuji.yaginuma

Do not generate unused components contents in `app:update` task

Currently, `app:update` generates all contents regardless of the
component using in application.

For example, even if not using Action Cable, `app:update` will generate
a contents related to Action Cable. This is a little inconvenient.
This PR checks the existence of the component and does not generate
unnecessary contents.
Can not check all options in this way. However, it will be able to
prevent the generation of unnecessary files.
上级 5153c0c2
* Skip unused components when running `bin/rails app:update`.
If the initial app generation skipped Action Cable, Active Record etc.,
the update task honors those skips too.
*Yuji Yaginuma*
* Make Rails' test runner work better with minitest plugins. * Make Rails' test runner work better with minitest plugins.
By demoting the Rails test runner to just another minitest plugin — By demoting the Rails test runner to just another minitest plugin —
......
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
File.exist?(Rails.root.join("config", "application.rb")) ? gen.send(:app_const) : gen.send(:valid_const?)
gen
end
end
private
def generator_options
options = { api: !!Rails.application.config.api_only, update: true }
options[:skip_active_record] = !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)
options
end
end
end
end
...@@ -128,7 +128,7 @@ def config_when_updating ...@@ -128,7 +128,7 @@ def config_when_updating
gsub_file "config/initializers/cookies_serializer.rb", /json(?!,)/, "marshal" gsub_file "config/initializers/cookies_serializer.rb", /json(?!,)/, "marshal"
end end
unless action_cable_config_exist if !options[:skip_action_cable] && !action_cable_config_exist
template "config/cable.yml" template "config/cable.yml"
end end
......
...@@ -36,38 +36,21 @@ namespace :app do ...@@ -36,38 +36,21 @@ namespace :app do
end end
namespace :update do namespace :update do
class RailsUpdate require_relative "../app_updater"
def self.invoke_from_app_generator(method)
app_generator.send(method)
end
def self.app_generator
@app_generator ||= begin
require_relative "../generators"
require_relative "../generators/rails/app/app_generator"
gen = Rails::Generators::AppGenerator.new ["rails"],
{ api: !!Rails.application.config.api_only, update: true },
destination_root: Rails.root
File.exist?(Rails.root.join("config", "application.rb")) ?
gen.send(:app_const) : gen.send(:valid_const?)
gen
end
end
end
# desc "Update config/boot.rb from your current rails install" # desc "Update config/boot.rb from your current rails install"
task :configs do task :configs do
RailsUpdate.invoke_from_app_generator :create_boot_file Rails::AppUpdater.invoke_from_app_generator :create_boot_file
RailsUpdate.invoke_from_app_generator :update_config_files Rails::AppUpdater.invoke_from_app_generator :update_config_files
end end
# desc "Adds new executables to the application bin/ directory" # desc "Adds new executables to the application bin/ directory"
task :bin do task :bin do
RailsUpdate.invoke_from_app_generator :update_bin_files Rails::AppUpdater.invoke_from_app_generator :update_bin_files
end end
task :upgrade_guide_info do task :upgrade_guide_info do
RailsUpdate.invoke_from_app_generator :display_upgrade_guide_info Rails::AppUpdater.invoke_from_app_generator :display_upgrade_guide_info
end end
end end
end end
...@@ -278,6 +278,22 @@ def test_app_update_does_not_remove_rack_cors_if_already_present ...@@ -278,6 +278,22 @@ def test_app_update_does_not_remove_rack_cors_if_already_present
end end
end end
def test_app_update_does_not_generate_action_cable_contents_when_skip_action_cable_is_given
app_root = File.join(destination_root, "myapp")
run_generator [app_root, "--skip-action-cable"]
FileUtils.cd(app_root) do
# For avoid conflict file
FileUtils.rm("#{app_root}/config/secrets.yml")
quietly { system("bin/rails app:update") }
end
assert_no_file "#{app_root}/config/cable.yml"
assert_file "#{app_root}/config/environments/production.rb" do |content|
assert_no_match(/config\.action_cable/, content)
end
end
def test_application_names_are_not_singularized def test_application_names_are_not_singularized
run_generator [File.join(destination_root, "hats")] run_generator [File.join(destination_root, "hats")]
assert_file "hats/config/environment.rb", /Rails\.application\.initialize!/ assert_file "hats/config/environment.rb", /Rails\.application\.initialize!/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册