Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run after_bundle blocks with app:template command #48269

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 1 addition & 5 deletions guides/source/rails_application_templates.md
Expand Up @@ -60,11 +60,7 @@ gem "bj"
gem "nokogiri"
```

Please note that this will NOT install the gems for you and you will have to run `bundle install` to do that.

```bash
$ bundle install
```
Note that this method only adds the gem to the `Gemfile`; it does not install the gem.

### gem_group(*names, &block)

Expand Down
5 changes: 5 additions & 0 deletions railties/CHANGELOG.md
@@ -1,3 +1,8 @@
* `bin/rails app:template` now runs `bundle install` and any `after_bundle`
blocks after the template is executed.

*Jonathan Hefner* and *Gerry Caulfield*

* Enable passing column size to migration generator

Previously you could pass a limit to the migration generator:
Expand Down
11 changes: 10 additions & 1 deletion railties/lib/rails/generators/rails/app/app_generator.rb
Expand Up @@ -309,6 +309,14 @@ class AppGenerator < AppBase

META_OPTIONS = [:minimal] # :nodoc:

def self.apply_rails_template(template, destination) # :nodoc:
generator = new([destination], { template: template }, { destination_root: destination })
generator.set_default_accessors!
generator.apply_rails_template
generator.run_bundle
generator.run_after_bundle_callbacks
end

def initialize(*args)
super

Expand Down Expand Up @@ -537,7 +545,8 @@ def finish_template
build(:leftovers)
end

public_task :apply_rails_template, :run_bundle
public_task :apply_rails_template
public_task :run_bundle
public_task :generate_bundler_binstub
public_task :run_javascript
public_task :run_hotwire
Expand Down
4 changes: 1 addition & 3 deletions railties/lib/rails/tasks/framework.rake
Expand Up @@ -8,11 +8,9 @@ namespace :app do
task template: :environment do
template = ENV["LOCATION"]
raise "No LOCATION value given. Please set LOCATION either as path to a file or a URL" if template.blank?
template = File.expand_path(template) unless %r{\A[A-Za-z][A-Za-z0-9+\-.]*://}.match?(template)
require "rails/generators"
require "rails/generators/rails/app/app_generator"
generator = Rails::Generators::AppGenerator.new [Rails.root], {}, { destination_root: Rails.root }
generator.apply template, verbose: false
Rails::Generators::AppGenerator.apply_rails_template(template, Rails.root)
end

namespace :templates do
Expand Down
17 changes: 17 additions & 0 deletions railties/test/generators/app_generator_test.rb
Expand Up @@ -1012,6 +1012,23 @@ def test_after_bundle_callback
assert_equal @bundle_commands_before_callback, @bundle_commands
end

def test_apply_rails_template_class_method_runs_bundle_and_after_bundle_callbacks
run_generator

FileUtils.cd(destination_root) do
template = "lib/template.rb"
File.write(template, "after_bundle { create_file 'after_bundle_callback_ran' }")

generator_class.no_commands do
assert_called_on_instance_of(generator_class, :run_bundle) do
quietly { generator_class.apply_rails_template(template, destination_root) }
end
end

assert_file "after_bundle_callback_ran"
end
end

def test_gitignore
run_generator

Expand Down