Add config.generators.after_generate for processing to generated files#38870
Add config.generators.after_generate for processing to generated files#38870y-yagi merged 1 commit intorails:masterfrom
config.generators.after_generate for processing to generated files#38870Conversation
|
This covers a little difference use-case than I have seen benefit of in original #32996. I was looking for way to open (for example) migration file every-time I create it using generator, but do not enforce the same for all developers on same project. Also I would welcome to set this once in my "env" and have this enabled for all Rails apps having this capability (compatible version). If I understand it well currently there's no easy way to achieve this, since (AFAIK) the global file Thinking of my requirement, I think it should be possible to modify behaviour of rails generator commands when used in "non-interactive"( |
|
Thanks for sharing your use-case! It seems the requirement is different from my requirement.
This use-case can support by this PR. For example, only run config.generators.after_generate do |files|
if ENV["EDITOR_FOR_GENERATOR"]
system(ENV["EDITOR_FOR_GENERATOR"], *files, exception: true)
end
end $ EDITOR_FOR_GENERATOR=vim ./bin/rails g model user name:string Of course, this is a little ugly. That support is out of scope this PR, but being able to pass a list of files to stdout in some way is worth considering in other PR I think. |
Yeah, but that should be added to every app just because of me. I'll think about my use-case a little more and open separate PR if needed. Code changes in here would be beneficial for that as well. |
|
Not too fond of the stated use case as justification for adding it. Wouldn't you have to run Rubocop before pushing the files (e.g. after someone's edited it) or run a Rubocop check on CI anyway? But the possibilities that I think the |
railties/lib/rails/generators.rb
Outdated
There was a problem hiding this comment.
Omit the needless self. when calling generated_files.
There was a problem hiding this comment.
CreateMigration keeps an existing file when content is the same with an invoked file. In that case, we need to return an existing file name. If do not, after_generate callback will return a non-existing file name(invoked file name).
railties/lib/rails/generators.rb
Outdated
There was a problem hiding this comment.
Does start return a value that we need to pass on? E.g. should we do start(args, config).tap do?
There was a problem hiding this comment.
The start returns all result of all_command. all_command includes methods that do not relate with generating files(e.g. check_class_collision, hook_for). It is a little difficult to extract that only result of generating files.
railties/lib/rails/generators.rb
Outdated
There was a problem hiding this comment.
Hm, not liking exposing this here. Can we manage everything entirely within add_generated_file? Maybe via (@@generated_files ||= []) << file or something?
If not, I'd move the _reader above the _accessor since that's the style we follow, iirc.
There was a problem hiding this comment.
I fixed that manage everything entirely within add_generated_file.
fe2a4ef to
1d14399
Compare
railties/lib/rails/generators.rb
Outdated
There was a problem hiding this comment.
It feels a little strange to have this as global state. I'm not sure how it will play with the :inline option added in #37516. If there is no way for this to be instance state on the instantiated generator, what do you think about clearing @@generated_files after all callbacks are run?
There was a problem hiding this comment.
Thanks, your advice is correct and clearing @@generated_files was needed. I fixed.
Register a callback that will get called right after generators has
finished.
This is useful if users want to process generated files.
For example, can execute an autocorrect of RuboCop for generated files
as like following.
```ruby
config.generators.after_generate do |files|
system("bundle exec rubocop --auto-correct " + files.join(" "), exception: true)
end
```
|
Rad! ❤️💪 |
This PR adds a Rails feature introduced by rails/rails#38870 to docs.
This PR adds a Rails feature introduced by rails/rails#38870 to docs.
This PR adds a Rails feature introduced by rails/rails#38870 to docs.
This PR adds a Rails feature introduced by rails/rails#38870 to docs.
…rate` ## Motivation / Background RuboCop has now been included by default (rails#50456). By adding the following tip to the default configuration, user can apply RuboCop's autocorrection to code generated by `bin/rails generate` (e.g., migration file): https://github.com/rubocop/rubocop-rails#rails-configuration-tip This means that the generated files will be formatted according to user's .rubocop.yml custom configuration. ## Detail Since `bin/rails generate` and `bin/rubocop` are used only in the development environment, the target files are limited to only `config/environments/development.rb`. ## Additional information This feature was introduced in Rails 6.1 by rails#38870.
…rate` (#50506) * Apply autocorrection by RuboCop to files generated by `bin/rails generate` ## Motivation / Background RuboCop has now been included by default (#50456). By adding the following tip to the default configuration, user can apply RuboCop's autocorrection to code generated by `bin/rails generate` (e.g., migration file): https://github.com/rubocop/rubocop-rails#rails-configuration-tip This means that the generated files will be formatted according to user's .rubocop.yml custom configuration. ## Detail Since `bin/rails generate` and `bin/rubocop` are used only in the development environment, the target files are limited to only `config/environments/development.rb`. ## Additional information This feature was introduced in Rails 6.1 by #38870.
…rate` (rails#50506) * Apply autocorrection by RuboCop to files generated by `bin/rails generate` ## Motivation / Background RuboCop has now been included by default (rails#50456). By adding the following tip to the default configuration, user can apply RuboCop's autocorrection to code generated by `bin/rails generate` (e.g., migration file): https://github.com/rubocop/rubocop-rails#rails-configuration-tip This means that the generated files will be formatted according to user's .rubocop.yml custom configuration. ## Detail Since `bin/rails generate` and `bin/rubocop` are used only in the development environment, the target files are limited to only `config/environments/development.rb`. ## Additional information This feature was introduced in Rails 6.1 by rails#38870.
This is useful if users want to process generated files.
For example, can execute an autocorrect of RuboCop for generated files as like following.