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 running Rails generators from the UI #2257

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

vinistock
Copy link
Member

Motivation

This PR adds a Rails class to our VS Code extension capable of running generators from the UI. This is useful on its own, but it is also necessary for other functionality that may want to invoke generators programatically.

Implementation

I split the work into two commits:

  1. Add the Rails class with a generate and destroy command and associate both with commands
  2. Expose a Ruby file operations command, where we can aggregate all of these under the same menu item. Then expose it in the explorer title and menu context

Automated Tests

Added tests.

Manual Tests

  1. Start the extension on this branch
  2. Open a Rails app
  3. Click on the new button in the explorer title view
  4. Use the generator
  5. Verify that it executes the Rails generator properly
Screen.Recording.2024-07-04.at.4.01.24.PM.mov

@vinistock vinistock added enhancement New feature or request vscode This pull request should be included in the VS Code extension's release notes labels Jul 4, 2024
@vinistock vinistock self-assigned this Jul 4, 2024
@vinistock vinistock requested a review from a team as a code owner July 4, 2024 20:34
@vinistock vinistock requested review from andyw8 and st0012 July 4, 2024 20:34
Copy link
Member

@st0012 st0012 left a comment

Choose a reason for hiding this comment

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

I think it works well in Rails projects. But I wouldn't expect the dropdown to show in IRB, which won't be able to run either commands anyway.
Since we now also have addon information, can we raise a warning when the Rails generate/destroy commands got triggered, but the addons list doesn't include Ruby LSP Rails?

Screenshot 2024-07-04 at 21 59 22

@andyw8
Copy link
Contributor

andyw8 commented Jul 5, 2024

Similar to what @st0012 mentioned, it is a bit awkward that we'll be showing these commands for non-Rails codebases, especially if we add more Rails commands in future.

I'm looking at the available things we can do in the when of the command to conditionally display it, maybe we can come up with a creative solution:

https://code.visualstudio.com/api/references/when-clause-contexts

vscode/src/rails.ts Outdated Show resolved Hide resolved
@andyw8
Copy link
Contributor

andyw8 commented Jul 5, 2024

When generate a resource, it fails at the file opening stage for one file, I suspect the output parsing needs little adjustment:

cannot open file:///Users/andyw8/src/github.com/Shopify/ruby-lsp-rails/app/views/widgets. Detail: Unable to read file '/Users/andyw8/src/github.com/Shopify/ruby-lsp-rails/app/views/widgets' (Error: Unable to read file '/Users/andyw8/src/github.com/Shopify/ruby-lsp-rails/app/views/widgets' that is actually a directory)

@andyw8
Copy link
Contributor

andyw8 commented Jul 5, 2024

I thought that if you try run a generator where some files already exist, Rails would interactively prompt to ask whether to overwrite each file, but I can't seem to trigger it. Maybe the behaviour changed in a more recent Rails version.

@vinistock
Copy link
Member Author

vinistock commented Jul 5, 2024

When generate a resource, it fails at the file opening stage for one file, I suspect the output parsing needs little adjustment:

Fixed it! Thanks for pointing that out.

Regarding not showing the button, that will become pretty complex given that you could have one workspace that is a Rails app and another one that isn't. There's no when clause that will tell you which workspace explorer view you are hovering over as far as I know.

So I propose we do something different. We always show the button, but then hide the Rails commands if the workspace is not a Rails app (which is significantly more straight forward).

To start out, so that we don't show an empty quick pick to users, I can follow up with a PR to add a New Ruby class/module file action, which essentially creates a new file with the entity selected.

This is common in IDEs, so it would be a nice feature to have and then we can move forward with a much simpler approach that won't require trying to figure out which workspace folder is being hovered over.

Thoughts?

@vinistock vinistock requested review from st0012 and andyw8 July 5, 2024 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request vscode This pull request should be included in the VS Code extension's release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants