Skip to content

Commit d3b055e

Browse files
authored
Merge pull request rails#51518 from andrewn617/devcontainer-dev-mode
Make local rails available to devcontainer generated with `--dev`
2 parents 6ec4975 + ab17a6c commit d3b055e

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

railties/lib/rails/generators/devcontainer.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ def devcontainer_volumes
3939
@devcontainer_volumes
4040
end
4141

42+
def devcontainer_mounts
43+
return @devcontainer_mounts if @devcontainer_mounts
44+
45+
@devcontainer_mounts = []
46+
47+
@devcontainer_mounts << local_rails_mount if options.dev?
48+
49+
@devcontainer_mounts
50+
end
51+
4252
def devcontainer_needs_redis?
4353
!(options.skip_action_cable? && options.skip_active_job?)
4454
end
@@ -127,6 +137,14 @@ def mariadb_service
127137
def db_service_names
128138
["mysql", "mariadb", "postgres"]
129139
end
140+
141+
def local_rails_mount
142+
{
143+
type: "bind",
144+
source: Rails::Generators::RAILS_DEV_PATH,
145+
target: Rails::Generators::RAILS_DEV_PATH
146+
}
147+
end
130148
end
131149
end
132150
end

railties/lib/rails/generators/rails/app/templates/.devcontainer/devcontainer.json.tt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
2727
// "remoteUser": "root",
2828

29+
<%- if !devcontainer_mounts.empty? -%>
30+
"mounts": [
31+
<%= devcontainer_mounts.map { |mount| "{\n " + mount.map { |key, value| "\"#{key}\": \"#{value}\"" }.join(",\n ") + "\n }" }.join(",\n ") %>
32+
],
33+
<%- end -%>
34+
2935
// Use 'postCreateCommand' to run commands after the container is created.
3036
"postCreateCommand": "bin/setup"
3137
}

railties/test/generators/app_generator_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,18 @@ def test_devcontainer_no_depends_on_when_no_dependencies
14151415
end
14161416
end
14171417

1418+
def test_devcontainer_dev_flag_mounts_local_rails_repo
1419+
run_generator_using_prerelease [ destination_root, "--dev" ]
1420+
1421+
assert_devcontainer_json_file do |devcontainer_config|
1422+
rails_mount = devcontainer_config["mounts"].sole
1423+
1424+
assert_equal "bind", rails_mount["type"]
1425+
assert_equal Rails::Generators::RAILS_DEV_PATH, rails_mount["source"]
1426+
assert_equal Rails::Generators::RAILS_DEV_PATH, rails_mount["target"]
1427+
end
1428+
end
1429+
14181430
def test_skip_devcontainer
14191431
run_generator [ destination_root, "--skip-devcontainer" ]
14201432

railties/test/generators/generators_test_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ def assert_compose_file
122122
end
123123
end
124124

125+
def assert_devcontainer_json_file
126+
assert_file ".devcontainer/devcontainer.json" do |content|
127+
yield JSON.load(content)
128+
end
129+
end
130+
125131
private
126132
def gemfile_locals
127133
{

0 commit comments

Comments
 (0)