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

Add support for Rails generators #605

Merged
merged 1 commit into from
Dec 13, 2021
Merged

Add support for Rails generators #605

merged 1 commit into from
Dec 13, 2021

Conversation

michaelherold
Copy link
Contributor

@michaelherold michaelherold commented Nov 10, 2021

Motivation

Rails generators are application code, though not part of your main Rails application. The files are not automatically loaded when starting your app because Railties loads them when you run the generate command.

However, since it's part of your application, it's helpful to be able to enable strong typing within those files. Without knowing about the Thor-based DSL, you're left writing shims yourself.

Having support within Tapioca means that you won't have to manage this manually any more.

Implementation

I load the two most likely candidate files. There are others that we could load, for example:

  • rails/generators/active_model
  • rails/generators/erb
  • rails/generators/generated_attribute

I didn't want to get into the rabbit hole of trying to load all of them because that would be brittle between Rails versions. Do you think that's the correct choice?

Also, in order to avoid generating RBIs for built-in classes, I opted for pattern matching on the class name. That doesn't feel great, but I wasn't sure what would be a better approach. There are two main base classes, Rails::Generators::NamedBase and Rails::Generators::Base, but there are also some in gems other than railties. Do you have suggestions on a better solution?

I had to add some RBIs for railties for type-checking since I use them in the tests. It appears that a prior version of Tapioca (or maybe Parlour?) wrote the existing file and that it used to include comments. Instead of including the big, messy diff, I included only the pieces that I needed. This leaves the file looking inconsistent because there are chunks in the new style and the rest is in the old. Is that okay, or should I rewrite the whole file? I rewrote the whole file.

Tests

Yep, there are tests for corner cases that I could think of.

@michaelherold michaelherold added the enhancement New feature or request label Nov 10, 2021
@KaanOzkan KaanOzkan requested a review from a team November 10, 2021 15:20
lib/tapioca/compilers/dsl/rails_generators.rb Show resolved Hide resolved
lib/tapioca/compilers/dsl/rails_generators.rb Outdated Show resolved Hide resolved
lib/tapioca/compilers/dsl/rails_generators.rb Show resolved Hide resolved
lib/tapioca/compilers/dsl/rails_generators.rb Outdated Show resolved Hide resolved
lib/tapioca/compilers/dsl/rails_generators.rb Outdated Show resolved Hide resolved
Copy link
Contributor Author

@michaelherold michaelherold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I resolved the style nits and left the other two threads open with comments. Thanks!

Copy link
Contributor

@KaanOzkan KaanOzkan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of including the big, messy diff, I included only the pieces that I needed

Feel free to include the big messy diff for railtie.rbi, since that's what tapioca outputted.

However, since this gem RBI update PR hasn't touched railtie I'm thinking that you didn't supply the --doc option? If that's the case please supply that and commit the RBI as a whole.

Rails generators are application code, though not part of your main
Rails application. The files are not automatically loaded when starting
your app because Railties loads them when you run the generate command.

However, since it's part of your application, it's helpful to be able to
enable strong typing within those files. Without knowing about the
Thor-based DSL, you're left writing shims yourself.

Having support within Tapioca means that you won't have to manage this
manually any more.
Copy link
Member

@paracycle paracycle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not in love with the BUILT_IN_MATCHER gymnastics, but I guess this is the best way of excluding internal classes, for now.

Thanks for working on this and putting this together. Much appreciated.

@paracycle paracycle merged commit 4e0254d into main Dec 13, 2021
@paracycle paracycle deleted the rails-generators branch December 13, 2021 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants