Abort early if generator command fails (#34420)

* No need to go through ruby

* Abort early if a generator command fails

* Reuse `rails_command` method

* Bump thor minimum dependency to 0.20.3

* Add some minimal docs

* Add a changelog entry

* Restore original logging
This commit is contained in:
David Rodríguez 2018-12-07 07:01:32 +01:00 committed by Yuji Yaginuma
parent b86f65a816
commit f173ec77fc
6 changed files with 33 additions and 6 deletions

@ -93,7 +93,7 @@ PATH
activesupport (= 6.0.0.alpha)
method_source
rake (>= 0.8.7)
thor (>= 0.19.0, < 2.0)
thor (>= 0.20.3, < 2.0)
GEM
remote: https://rubygems.org/
@ -304,7 +304,7 @@ GEM
marcel (0.3.3)
mimemagic (~> 0.3.2)
memoist (0.16.0)
method_source (0.9.1)
method_source (0.9.2)
mime-types (3.2.2)
mime-types-data (~> 3.2015)
mime-types-data (3.2018.0812)

@ -195,6 +195,12 @@ You can also run commands as a super-user:
rails_command "log:clear", sudo: true
```
You can also run commands that should abort application generation if they fail:
```ruby
rails_command "db:migrate", abort_on_failure: true
```
### route(routing_code)
Adds a routing entry to the `config/routes.rb` file. In the steps above, we generated a person scaffold and also removed `README.rdoc`. Now, to make `PeopleController#index` the default page for the application:

@ -1,3 +1,9 @@
* Add an `abort_on_failure` boolean option to the generator method that shell
out (`generate`, `rake`, `rails_command`) to abort the generator if the
command fails.
*David Rodríguez*
* Remove `app/assets` and `app/javascript` from `eager_load_paths` and `autoload_paths`.
*Gannon McGibbon*

@ -221,9 +221,11 @@ def initializer(filename, data = nil)
# generate(:authenticated, "user session")
def generate(what, *args)
log :generate, what
options = args.extract_options!
argument = args.flat_map(&:to_s).join(" ")
in_root { run_ruby_script("bin/rails generate #{what} #{argument}", verbose: false) }
execute_command :rails, "generate #{what} #{argument}", options
end
# Runs the supplied rake task (invoked with 'rake ...')
@ -307,6 +309,7 @@ def execute_command(executor, command, options = {}) # :doc:
config = { verbose: false }
config[:capture] = options[:capture] if options[:capture]
config[:abort_on_failure] = options[:abort_on_failure] if options[:abort_on_failure]
in_root { run("#{sudo}#{extify(executor)} #{command} RAILS_ENV=#{env}", config) }
end

@ -37,7 +37,7 @@
s.add_dependency "actionpack", version
s.add_dependency "rake", ">= 0.8.7"
s.add_dependency "thor", ">= 0.19.0", "< 2.0"
s.add_dependency "thor", ">= 0.20.3", "< 2.0"
s.add_dependency "method_source"
s.add_development_dependency "actionview", version

@ -303,9 +303,21 @@ def test_initializer_should_write_date_to_file_with_block_in_config_initializers
end
def test_generate_should_run_script_generate_with_argument_and_options
assert_called_with(generator, :run_ruby_script, ["bin/rails generate model MyModel", verbose: false]) do
action :generate, "model", "MyModel"
run_generator
action :generate, "model", "MyModel"
assert_file "app/models/my_model.rb", /MyModel/
end
def test_generate_aborts_when_subprocess_fails_if_requested
run_generator
content = capture(:stderr) do
assert_raises SystemExit do
action :generate, "model", "MyModel:ADsad", abort_on_failure: true
action :generate, "model", "MyModel"
end
end
assert_match(/wrong constant name MyModel:aDsad/, content)
assert_no_file "app/models/my_model.rb"
end
def test_rake_should_run_rake_command_with_default_env