Skip to content

Commit

Permalink
Create a devcontainer command
Browse files Browse the repository at this point in the history
This command boots the application, and generates a Dev Container setup based on the current configuration of the application.
  • Loading branch information
andrewn617 committed May 22, 2024
1 parent 1f4bedc commit 67d55c2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
* Create a Dev Container Generator that generates a Dev Container setup based on the current configuration
of the application. Usage:

`bin/rails devcontainer`

*Andrew Novoselac*

* Use Kamal for deployment by default, which includes generating a Rails-specific config/deploy.yml.
This can be skipped using --skip-kamal. See more: https://kamal-deploy.org/

Expand Down
34 changes: 34 additions & 0 deletions railties/lib/rails/commands/devcontainer/devcontainer_command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require "rails/generators"
require "rails/generators/rails/devcontainer/devcontainer_generator"

module Rails
module Command
class DevcontainerCommand < Base # :nodoc:
desc "devcontainer", "Generate a Dev Container setup based on current application configuration"
def perform(*)
boot_application!

say "Generating Dev Container with the following options:"
devcontainer_options.each do |option, value|
say "#{option}: #{value}"
end

Rails::Generators::DevcontainerGenerator.new([], devcontainer_options).invoke_all
end

private
def devcontainer_options
@devcontainer_options ||= {
app_name: Rails.application.railtie_name.chomp("_application"),
database: !!defined?(ActiveRecord) && ActiveRecord::Base.connection_db_config.adapter,
active_storage: !!defined?(ActiveStorage),
redis: !!(defined?(ActionCable) || defined?(ActiveJob)),
system_test: File.exist?("test/application_system_test_case.rb"),
node: File.exist?(".node-version"),
}
end
end
end
end
23 changes: 23 additions & 0 deletions railties/test/commands/devcontainer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "isolation/abstract_unit"
require "rails/command"

class Rails::Command::DevcontainerTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation

teardown { teardown_app }

test "generates devcontainer for default app" do
build_app

output = rails "devcontainer"

assert_match "app_name: app_template", output
assert_match "database: sqlite3", output
assert_match "active_storage: true", output
assert_match "redis: true", output
assert_match "system_test: true", output
assert_match "node: false", output
end
end

0 comments on commit 67d55c2

Please sign in to comment.