Skip to content

Commit

Permalink
When generating an app with the --dev flag, mount the local rails rep…
Browse files Browse the repository at this point in the history
…o in the devcontainer

This is useful for testing changes to how the devcontainer is generated, and will be necessary for creating a CI workflow for testing the devcontainer setup.
  • Loading branch information
andrewn617 committed Apr 8, 2024
1 parent 84c4598 commit ce23994
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
18 changes: 18 additions & 0 deletions railties/lib/rails/generators/devcontainer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ def devcontainer_volumes
@devcontainer_volumes
end

def devcontainer_mounts
return @devcontainer_mounts if @devcontainer_mounts

@devcontainer_mounts = []

@devcontainer_mounts << local_rails_mount if options.dev?

@devcontainer_mounts
end

def devcontainer_needs_redis?
!(options.skip_action_cable? && options.skip_active_job?)
end
Expand Down Expand Up @@ -127,6 +137,14 @@ def mariadb_service
def db_service_names
["mysql", "mariadb", "postgres"]
end

def local_rails_mount
{
type: "bind",
source: Rails::Generators::RAILS_DEV_PATH,
target: Rails::Generators::RAILS_DEV_PATH
}
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root",

<%- if !devcontainer_mounts.empty? -%>
"mounts": [
<%= devcontainer_mounts.map { |mount| "{\n " + mount.map { |key, value| "\"#{key}\": \"#{value}\"" }.join(",\n ") + "\n }" }.join(",\n ") %>
],
<%- end -%>

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "bin/setup"
}
12 changes: 12 additions & 0 deletions railties/test/generators/app_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,18 @@ def test_devcontainer_no_depends_on_when_no_dependencies
end
end

def test_devcontainer_dev_flag_mounts_local_rails_repo
run_generator_using_prerelease [ destination_root, "--dev" ]

assert_devcontainer_json_file do |devcontainer_config|
rails_mount = devcontainer_config["mounts"].sole

assert_equal "bind", rails_mount["type"]
assert_equal Rails::Generators::RAILS_DEV_PATH, rails_mount["source"]
assert_equal Rails::Generators::RAILS_DEV_PATH, rails_mount["target"]
end
end

def test_skip_devcontainer
run_generator [ destination_root, "--skip-devcontainer" ]

Expand Down
6 changes: 6 additions & 0 deletions railties/test/generators/generators_test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ def assert_compose_file
end
end

def assert_devcontainer_json_file
assert_file ".devcontainer/devcontainer.json" do |content|
yield JSON.load(content)
end
end

private
def gemfile_locals
{
Expand Down

0 comments on commit ce23994

Please sign in to comment.