Skip to content

Commit

Permalink
Merge pull request #51880 from Shopify/devcontainer-generator
Browse files Browse the repository at this point in the history
Make devcontainers opt in and create a devcontainer command
  • Loading branch information
rafaelfranca committed May 23, 2024
2 parents a8fdfff + df203b2 commit 62501c7
Show file tree
Hide file tree
Showing 18 changed files with 290 additions and 165 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/devcontainer-smoke-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
bundler-cache: true

- name: Generate rails app sqlite3
run: bundle exec railties/exe/rails new myapp_sqlite --database="sqlite3" --dev
run: bundle exec railties/exe/rails new myapp_sqlite --database="sqlite3" --dev --devcontainer

- name: Remove old deprecated docker-compose
run: |
Expand All @@ -52,7 +52,7 @@ jobs:
run: docker ps -q | xargs docker stop

- name: Generate rails app postgresql
run: bundle exec railties/exe/rails new myapp_postgresql --database="postgresql" --dev
run: bundle exec railties/exe/rails new myapp_postgresql --database="postgresql" --dev --devcontainer

- name: Test devcontainer postgresql
uses: devcontainers/ci@v0.3
Expand All @@ -69,7 +69,7 @@ jobs:
run: docker ps -q | xargs docker stop

- name: Generate rails app mysql
run: bundle exec railties/exe/rails new myapp_mysql --database="mysql" --dev
run: bundle exec railties/exe/rails new myapp_mysql --database="mysql" --dev --devcontainer

- name: Test devcontainer mysql
uses: devcontainers/ci@v0.3
Expand All @@ -86,7 +86,7 @@ jobs:
run: docker ps -q | xargs docker stop

- name: Generate rails app trilogy
run: bundle exec railties/exe/rails new myapp_trilogy --database="trilogy" --dev
run: bundle exec railties/exe/rails new myapp_trilogy --database="trilogy" --dev --devcontainer

- name: Test devcontainer trilogy
uses: devcontainers/ci@v0.3
Expand Down
1 change: 0 additions & 1 deletion guides/source/command_line.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ If you wish to skip some files from being generated or skip some libraries, you
| `--skip-bootsnap` | Skip bootsnap gem |
| `--skip-dev-gems` | Skip adding development gems |
| `--skip-rubocop` | Skip RuboCop setup |
| `--skip-devcontainer` | Skip Dev Container setup |

These are just some of the options that `rails new` accepts. For a full list of options, type `rails new --help`.

Expand Down
1 change: 0 additions & 1 deletion guides/source/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ of the files and folders that Rails creates by default:
|.gitignore|This file tells git which files (or patterns) it should ignore. See [GitHub - Ignoring files](https://help.github.com/articles/ignoring-files) for more information about ignoring files.|
|.rubocop.yml|This file contains the configuration for RuboCop.|
|.ruby-version|This file contains the default Ruby version.|
|.devcontainer/|This folder contains the Dev Container configuration|

Hello, Rails!
-------------
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
17 changes: 8 additions & 9 deletions railties/lib/rails/generators/app_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
module Rails
module Generators
class AppBase < Base # :nodoc:
include Devcontainer
include AppName

NODE_LTS_VERSION = "20.11.1"
Expand Down Expand Up @@ -109,15 +108,15 @@ def self.add_shared_options_for(name)
class_option :skip_ci, type: :boolean, default: nil,
desc: "Skip GitHub CI files"

class_option :skip_devcontainer, type: :boolean, default: false,
desc: "Skip devcontainer files"

class_option :skip_kamal, type: :boolean, default: false,
desc: "Skip Kamal setup"

class_option :dev, type: :boolean, default: nil,
desc: "Set up the #{name} with Gemfile pointing to your Rails checkout"

class_option :devcontainer, type: :boolean, default: false,
desc: "Generate devcontainer files"

class_option :edge, type: :boolean, default: nil,
desc: "Set up the #{name} with a Gemfile pointing to the #{edge_branch} branch on the Rails repository"

Expand Down Expand Up @@ -411,7 +410,11 @@ def skip_ci?
end

def skip_devcontainer?
options[:skip_devcontainer]
!options[:devcontainer]
end

def devcontainer?
options[:devcontainer]
end

def skip_kamal?
Expand Down Expand Up @@ -454,10 +457,6 @@ def to_s
end
end

def gem_ruby_version
Gem::Version.new(Gem::VERSION) >= Gem::Version.new("3.3.13") ? Gem.ruby_version : RUBY_VERSION
end

def rails_prerelease?
options.dev? || options.edge? || options.main?
end
Expand Down
4 changes: 4 additions & 0 deletions railties/lib/rails/generators/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ def self.default_generator_root # :doc:
path = File.expand_path(File.join(base_name, generator_name), base_root)
path if File.exist?(path)
end

def gem_ruby_version
Gem::Version.new(Gem::VERSION) >= Gem::Version.new("3.3.13") ? Gem.ruby_version : RUBY_VERSION
end
end
end
end
96 changes: 0 additions & 96 deletions railties/lib/rails/generators/devcontainer.rb

This file was deleted.

17 changes: 12 additions & 5 deletions railties/lib/rails/generators/rails/app/app_generator.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

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

module Rails
module ActionMethods # :nodoc:
Expand Down Expand Up @@ -268,11 +269,17 @@ def config_target_version
end

def devcontainer
empty_directory ".devcontainer"

template ".devcontainer/devcontainer.json"
template ".devcontainer/Dockerfile"
template ".devcontainer/compose.yaml"
devcontainer_options = {
database: options[:database],
redis: !(options[:skip_action_cable] && options[:skip_active_job]),
system_test: depends_on_system_test?,
active_storage: !options[:skip_active_storage],
dev: options[:dev],
node: using_node?,
app_name: app_name
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ default: &default
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
<% unless options[:skip_devcontainer] -%>
<% if devcontainer? -%>
<%% if ENV["DB_HOST"] %>
host: <%%= ENV["DB_HOST"] %>
username: postgres
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
require "test_helper"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
<% if skip_devcontainer? -%>
driven_by :selenium, using: :headless_chrome, screen_size: [ 1400, 1400 ]
<% else -%>
if ENV["CAPYBARA_SERVER_PORT"]
served_by host: "rails-app", port: ENV["CAPYBARA_SERVER_PORT"]

driven_by :selenium, using: :headless_chrome, screen_size: [ 1400, 1400 ], options: {
browser: :remote,
url: "http://#{ENV["SELENIUM_HOST"]}:4444"
}
else
driven_by :selenium, using: :headless_chrome, screen_size: [ 1400, 1400 ]
end
<% end -%>
end
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ def edit_dockerfile
end

def edit_devcontainer_files
devcontainer_path = File.expand_path(".devcontainer", destination_root)
return unless File.exist?(devcontainer_path)
return unless devcontainer?

edit_devcontainer_json
edit_compose_yaml
Expand Down Expand Up @@ -194,6 +193,12 @@ def devcontainer_json_path
def database
@database ||= Database.build(options[:database])
end

def devcontainer?
return @devcontainer if defined?(@devcontainer)

@devcontainer = File.exist?(File.expand_path(".devcontainer", destination_root))
end
end
end
end
Expand Down
Loading

0 comments on commit 62501c7

Please sign in to comment.