Skip to content

Commit 793ff00

Browse files
authored
Merge pull request rails#51690 from etiennebarrie/force-app-update
Turn app:update into a command to add --force
2 parents 113ca9e + d379fae commit 793ff00

File tree

6 files changed

+127
-85
lines changed

6 files changed

+127
-85
lines changed

railties/CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
* Add options to bin/rails app:update.
2+
3+
`bin/rails app:update` now supports the same generic options that generators do:
4+
5+
* `--force`: Accept all changes to existing files
6+
* `--skip`: Refuse all changes to existing files
7+
* `--pretend`: Don't make any changes
8+
* `--quiet`: Don't output all changes made
9+
10+
*Étienne Barrié*
11+
112
* Implement Rails console commands and helpers with IRB v1.13's extension APIs
213

314
Rails console users will now see `helper`, `controller`, `new_session`, and `app` under

railties/lib/rails/app_updater.rb

-52
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# frozen_string_literal: true
2+
3+
require "rails/generators"
4+
require "rails/generators/rails/app/app_generator"
5+
6+
module Rails
7+
module Command
8+
module App
9+
class UpdateCommand < Base # :nodoc:
10+
include Thor::Actions
11+
add_runtime_options!
12+
13+
desc "update", "Update configs and some other initially generated files (or use just update:configs or update:bin)"
14+
def perform
15+
configs
16+
bin
17+
active_storage
18+
display_upgrade_guide_info
19+
end
20+
21+
desc "configs", "Update config files in the application config/ directory", hide: true
22+
def configs
23+
require_application!
24+
app_generator.create_boot_file
25+
app_generator.update_config_files
26+
end
27+
28+
desc "bin", "Add or update executables in the application bin/ directory", hide: true
29+
def bin
30+
require_application!
31+
app_generator.update_bin_files
32+
end
33+
34+
desc "active_storage", "Run the active_storage:update command", hide: true
35+
def active_storage
36+
require_application!
37+
app_generator.update_active_storage
38+
end
39+
40+
private
41+
def display_upgrade_guide_info
42+
say "\nAfter this, check Rails upgrade guide at https://guides.rubyonrails.org/upgrading_ruby_on_rails.html for more details about upgrading your app."
43+
end
44+
45+
def app_generator
46+
@app_generator ||= begin
47+
gen = Rails::Generators::AppGenerator.new(["rails"], generator_options, destination_root: Rails.root)
48+
gen.send(:valid_const?) unless File.exist?(Rails.root.join("config", "application.rb"))
49+
gen
50+
end
51+
end
52+
53+
def generator_options
54+
{
55+
api: !!Rails.application.config.api_only,
56+
update: true,
57+
name: Rails.application.class.name.chomp("::Application").underscore,
58+
skip_active_job: !defined?(ActiveJob::Railtie),
59+
skip_active_record: !defined?(ActiveRecord::Railtie),
60+
skip_active_storage: !defined?(ActiveStorage::Engine),
61+
skip_action_mailer: !defined?(ActionMailer::Railtie),
62+
skip_action_mailbox: !defined?(ActionMailbox::Engine),
63+
skip_action_text: !defined?(ActionText::Engine),
64+
skip_action_cable: !defined?(ActionCable::Engine),
65+
skip_test: !defined?(Rails::TestUnitRailtie),
66+
skip_system_test: Rails.application.config.generators.system_tests.nil?,
67+
asset_pipeline: asset_pipeline,
68+
skip_asset_pipeline: asset_pipeline.nil?,
69+
skip_bootsnap: !defined?(Bootsnap),
70+
}.merge(options)
71+
end
72+
73+
def asset_pipeline
74+
case
75+
when defined?(Sprockets::Railtie)
76+
"sprockets"
77+
when defined?(Propshaft::Railtie)
78+
"propshaft"
79+
else
80+
nil
81+
end
82+
end
83+
end
84+
end
85+
end
86+
end

railties/lib/rails/generators/rails/app/app_generator.rb

-5
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,6 @@ def create_credentials
412412
build(:credentials_diff_enroll)
413413
end
414414

415-
def display_upgrade_guide_info
416-
say "\nAfter this, check Rails upgrade guide at https://guides.rubyonrails.org/upgrading_ruby_on_rails.html for more details about upgrading your app."
417-
end
418-
remove_task :display_upgrade_guide_info
419-
420415
def create_boot_file
421416
template "config/boot.rb"
422417
end
-26
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# frozen_string_literal: true
22

33
namespace :app do
4-
desc "Update configs and some other initially generated files (or use just update:configs or update:bin)"
5-
task update: [ "update:configs", "update:bin", "update:active_storage", "update:upgrade_guide_info" ]
6-
74
desc "Apply the template supplied by LOCATION=(/path/to/template) or URL"
85
task template: :environment do
96
template = ENV["LOCATION"]
@@ -34,27 +31,4 @@ namespace :app do
3431
end
3532
end
3633
end
37-
38-
namespace :update do
39-
require "rails/app_updater"
40-
41-
# desc "Update config files from your current rails install"
42-
task :configs do
43-
Rails::AppUpdater.invoke_from_app_generator :create_boot_file
44-
Rails::AppUpdater.invoke_from_app_generator :update_config_files
45-
end
46-
47-
# desc "Add new executables to the application bin/ directory"
48-
task :bin do
49-
Rails::AppUpdater.invoke_from_app_generator :update_bin_files
50-
end
51-
52-
task :active_storage do
53-
Rails::AppUpdater.invoke_from_app_generator :update_active_storage
54-
end
55-
56-
task :upgrade_guide_info do
57-
Rails::AppUpdater.invoke_from_app_generator :display_upgrade_guide_info
58-
end
59-
end
6034
end

railties/test/generators/app_generator_test.rb

+30-2
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,34 @@ def test_app_update_create_new_framework_defaults
188188
assert_file defaults_path
189189
end
190190

191+
def test_app_update_supports_skip
192+
run_generator
193+
FileUtils.cd(destination_root) do
194+
config = "config/application.rb"
195+
File.open(config, "a") do |file|
196+
file.puts "# some configuration"
197+
end
198+
assert_no_changes -> { File.readlines(config) } do
199+
run_app_update(flags: "--skip")
200+
end
201+
end
202+
end
203+
204+
def test_app_update_supports_pretend
205+
run_generator
206+
FileUtils.cd(destination_root) do
207+
config = "config/application.rb"
208+
File.open(config, "a") do |file|
209+
file.puts "# some configuration"
210+
end
211+
assert_no_changes -> { File.readlines(config) } do
212+
run_app_update(flags: "--pretend --force")
213+
end
214+
defaults_path = "config/initializers/new_framework_defaults_#{Rails::VERSION::MAJOR}_#{Rails::VERSION::MINOR}.rb"
215+
assert_no_file defaults_path
216+
end
217+
end
218+
191219
def test_app_update_does_not_create_rack_cors
192220
run_generator
193221
run_app_update
@@ -1500,13 +1528,13 @@ def run_generator_and_bundler(args)
15001528
end
15011529
end
15021530

1503-
def run_app_update(app_root = destination_root)
1531+
def run_app_update(app_root = destination_root, flags: "--force")
15041532
Dir.chdir(app_root) do
15051533
gemfile_contents = File.read("Gemfile")
15061534
gemfile_contents.sub!(/^(gem "rails").*/, "\\1, path: #{File.expand_path("../../..", __dir__).inspect}")
15071535
File.write("Gemfile", gemfile_contents)
15081536

1509-
quietly { system({ "BUNDLE_GEMFILE" => "Gemfile" }, "yes | bin/rails app:update", exception: true) }
1537+
quietly { system({ "BUNDLE_GEMFILE" => "Gemfile" }, "bin/rails app:update #{flags}", exception: true) }
15101538
end
15111539
end
15121540

0 commit comments

Comments
 (0)