mirror of https://github.com/rails/rails
Merge pull request #23317 from rfmanuel/after-bundle-in-rails-plugin-template
Add an after_bundle callback in Rails plugin templates
This commit is contained in:
commit
6162c49e40
|
@ -1,3 +1,8 @@
|
|||
* Add `after_bundle` callbacks in Rails plugin templates. Useful for allowing
|
||||
templates to perform actions that are dependent upon `bundle install`.
|
||||
|
||||
*Ryan Manuel*
|
||||
|
||||
* Bring back `TEST=` env for `rake test` task.
|
||||
|
||||
*Yves Senn*
|
||||
|
|
|
@ -259,6 +259,12 @@ task default: :test
|
|||
|
||||
public_task :apply_rails_template, :run_bundle
|
||||
|
||||
def run_after_bundle_callbacks
|
||||
@after_bundle_callbacks.each do |callback|
|
||||
callback.call
|
||||
end
|
||||
end
|
||||
|
||||
def name
|
||||
@name ||= begin
|
||||
# same as ActiveSupport::Inflector#underscore except not replacing '-'
|
||||
|
|
|
@ -703,9 +703,8 @@ class AppGeneratorTest < Rails::Generators::TestCase
|
|||
end
|
||||
|
||||
sequence = ['install', 'exec spring binstub --all', 'echo ran after_bundle']
|
||||
ensure_bundler_first = -> command do
|
||||
@sequence_step ||= 0
|
||||
|
||||
ensure_bundler_first = -> command do
|
||||
assert_equal sequence[@sequence_step], command, "commands should be called in sequence #{sequence}"
|
||||
@sequence_step += 1
|
||||
end
|
||||
|
@ -717,6 +716,8 @@ class AppGeneratorTest < Rails::Generators::TestCase
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_equal 3, @sequence_step
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
|
@ -634,6 +634,34 @@ class PluginGeneratorTest < Rails::Generators::TestCase
|
|||
assert_file "app/models/bukkits/article.rb", /class Article < ApplicationRecord/
|
||||
end
|
||||
|
||||
def test_after_bundle_callback
|
||||
path = 'http://example.org/rails_template'
|
||||
template = %{ after_bundle { run 'echo ran after_bundle' } }
|
||||
template.instance_eval "def read; self; end" # Make the string respond to read
|
||||
|
||||
check_open = -> *args do
|
||||
assert_equal [ path, 'Accept' => 'application/x-thor-template' ], args
|
||||
template
|
||||
end
|
||||
|
||||
sequence = ['install', 'echo ran after_bundle']
|
||||
@sequence_step ||= 0
|
||||
ensure_bundler_first = -> command do
|
||||
assert_equal sequence[@sequence_step], command, "commands should be called in sequence #{sequence}"
|
||||
@sequence_step += 1
|
||||
end
|
||||
|
||||
generator([destination_root], template: path).stub(:open, check_open, template) do
|
||||
generator.stub(:bundle_command, ensure_bundler_first) do
|
||||
generator.stub(:run, ensure_bundler_first) do
|
||||
quietly { generator.invoke_all }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
assert_equal 2, @sequence_step
|
||||
end
|
||||
|
||||
protected
|
||||
def action(*args, &block)
|
||||
silence(:stdout){ generator.send(*args, &block) }
|
||||
|
|
Loading…
Reference in New Issue