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

Make devcontainers opt in and create a devcontainer command #51880

Merged
merged 5 commits into from
May 23, 2024

Conversation

andrewn617
Copy link
Contributor

@andrewn617 andrewn617 commented May 21, 2024

Motivation / Background

This Pull Request has been created because we have decided for Rails 7.2 dev containers will be an opt in feature. So, new apps will only get a devcontainer if you pass the --devcontainer flag to rails new. Additionally, you will be able to generate a devcontainer for an existing app with bin/rails devcontainer.

Detail

There are a few commits here to make this work. First, I refactored the application generator to make dev container opt in. Then I extracted all the devcontainer logic from the application generator into a dedicated devcontainer generator. That allows the logic to be shared by the new command and the devcontainer command.

For the devcontainer command, I boot the app and check on the state of the application to decide what options are needed for the devcontainer, similar to the app update command.

Additional information

Previously, we had devcontainer specific logic in application_system_test_case.rb.tt. I removed that logic, and instead, now the devcontainer generator updates application_system_test_case.rb in place when generating a dev container. That way, we do not need to overwrite the entire file when generating a devcontainer for an existing app.

Similarily, there is devcontainer specific logic in postgresql.yml.tt. The devcontainer will update the database.yml for an existing app to make sure the database.yml is up-to-date. I also fixed this for the db:system:change generator, which previously was not updating the database.yml to include the devcontainer specific config.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@andrewn617 andrewn617 force-pushed the devcontainer-generator branch 2 times, most recently from d9a666b to 019bc15 Compare May 22, 2024 11:52

module Rails
module Command
class DevcontainerCommand < Base # :nodoc:
Copy link
Member

Choose a reason for hiding this comment

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

Do we need a command? I think rails g devcontainer should be enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

rails g devcontainer would work if you pass options to it, or get the default options. But the command is what knows about booting the application and looking at it to figure out what options are needed.

I am thinking about how I could move that into the generator. I think we would need another option for the generator to tell it to generate based on the current application, rather than the passed options (or just the defaults). So I guess we could make that option true by default (otherwise user has to do rails g devcontainer --infer-options), and then we can set it to false when we call it in the application generator.

What do you think about that?

Copy link
Member

Choose a reason for hiding this comment

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

Let's keep the command then

@rafaelfranca rafaelfranca added this to the 7.2.0 milestone May 22, 2024
@andrewn617 andrewn617 force-pushed the devcontainer-generator branch 2 times, most recently from 67d55c2 to 7759c1d Compare May 23, 2024 00:35
For Rails 7.2 we will make devcontainer and opt-in feature for new applications. When creating a new app, you can generate a devcontainer by passing the --devcontainer flag.
This refactoring prepares the way to implement a devcontainer command to add an devcontainer to an existing app.
Instead of relying on the template to be dev container aware and generate the correct configuration, let's just update the configuration in place when running the DevcontainerGenerator. This will allow the devcontainer command to update this file with the configuration needed for devcontainers without overwriting the entire file.
Postgresql's database.yml has devcontainer specific logic that should only appear when the app has a devcontainer. We need the DevcontainerGenerator to update database.yml, so when it is called by the devcontainer command the database.yml will have the right configuration.

This commit also fixes db:system:change to make sure it updates the database.yml with the correct devcontainer configuration.
This command boots the application, and generates a Dev Container setup based on the current configuration of the application.
@rafaelfranca rafaelfranca merged commit 62501c7 into rails:main May 23, 2024
2 of 3 checks passed
@rafaelfranca rafaelfranca deleted the devcontainer-generator branch May 23, 2024 19:01
rafaelfranca added a commit that referenced this pull request May 23, 2024
Make devcontainers opt in and create a devcontainer command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants