forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate a .devcontainer folder and its contents when creating a new …
…app. The .devcontainer folder includes everything needed to boot the app and do development in a remote container. The container setup includes: - A redis container for Kredis, ActionCable etc. - A database (SQLite, Postgres, MySQL or MariaDB) - A Headless chrome container for system tests - Active Storage configured to use the local disk and with preview features working If any of these options are skipped in the app setup they will not be included in the container configuration. These files can be skipped using the `--no-devcontainer` option. Co-authored-by: Rafael Mendonça França <rafael@franca.dev>
- Loading branch information
1 parent
30506d2
commit c90a870
Showing
22 changed files
with
761 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rails | ||
module Generators | ||
module Devcontainer | ||
private | ||
def devcontainer_ruby_version | ||
gem_ruby_version.to_s.match(/^\d+\.\d+/).to_s | ||
end | ||
|
||
def devcontainer_dependencies | ||
return @devcontainer_dependencies if @devcontainer_dependencies | ||
|
||
@devcontainer_dependencies = [] | ||
|
||
@devcontainer_dependencies << "selenium" if depends_on_system_test? | ||
@devcontainer_dependencies << "redis" if devcontainer_needs_redis? | ||
@devcontainer_dependencies << db_name_for_devcontainer if db_name_for_devcontainer | ||
@devcontainer_dependencies | ||
end | ||
|
||
def devcontainer_variables | ||
return @devcontainer_variables if @devcontainer_variables | ||
|
||
@devcontainer_variables = {} | ||
|
||
@devcontainer_variables["CAPYBARA_SERVER_PORT"] = "45678" if depends_on_system_test? | ||
@devcontainer_variables["SELENIUM_HOST"] = "selenium" if depends_on_system_test? | ||
@devcontainer_variables["REDIS_URL"] = "redis://redis:6379/1" if devcontainer_needs_redis? | ||
@devcontainer_variables["DB_HOST"] = db_name_for_devcontainer if db_name_for_devcontainer | ||
|
||
@devcontainer_variables | ||
end | ||
|
||
def devcontainer_volumes | ||
return @devcontainer_volumes if @devcontainer_volumes | ||
|
||
@devcontainer_volumes = [] | ||
|
||
@devcontainer_volumes << "redis-data" if devcontainer_needs_redis? | ||
@devcontainer_volumes << db_volume_name_for_devcontainer if db_volume_name_for_devcontainer | ||
|
||
@devcontainer_volumes | ||
end | ||
|
||
def devcontainer_needs_redis? | ||
!(options.skip_action_cable? && options.skip_active_job?) | ||
end | ||
|
||
def db_name_for_devcontainer(database = options[:database]) | ||
case database | ||
when "mysql" then "mysql" | ||
when "trilogy" then "mariadb" | ||
when "postgresql" then "postgres" | ||
end | ||
end | ||
|
||
def db_volume_name_for_devcontainer(database = options[:database]) | ||
case database | ||
when "mysql" then "mysql-data" | ||
when "trilogy" then "mariadb-data" | ||
when "postgresql" then "postgres-data" | ||
end | ||
end | ||
|
||
def devcontainer_db_service_yaml(**options) | ||
return unless service = db_service_for_devcontainer | ||
|
||
service.to_yaml(**options)[4..-1] | ||
end | ||
|
||
def db_service_for_devcontainer(database = options[:database]) | ||
case database | ||
when "mysql" then mysql_service | ||
when "trilogy" then mariadb_service | ||
when "postgresql" then postgres_service | ||
end | ||
end | ||
|
||
def postgres_service | ||
{ | ||
"postgres" => { | ||
"image" => "postgres:16.1", | ||
"restart" => "unless-stopped", | ||
"networks" => ["default"], | ||
"volumes" => ["postgres-data:/var/lib/postgresql/data"], | ||
"environment" => { | ||
"POSTGRES_USER" => "postgres", | ||
"POSTGRES_PASSWORD" => "postgres" | ||
} | ||
} | ||
} | ||
end | ||
|
||
def mysql_service | ||
{ | ||
"mysql" => { | ||
"image" => "mysql/mysql-server:8.0", | ||
"restart" => "unless-stopped", | ||
"environment" => { | ||
"MYSQL_ALLOW_EMPTY_PASSWORD" => true, | ||
"MYSQL_ROOT_HOST" => "%" | ||
}, | ||
"volumes" => ["mysql-data:/var/lib/mysql"], | ||
"networks" => ["default"], | ||
} | ||
} | ||
end | ||
|
||
def mariadb_service | ||
{ | ||
"mariadb" => { | ||
"image" => "mariadb:10.5", | ||
"restart" => "unless-stopped", | ||
"networks" => ["dqefault"], | ||
"volumes" => ["mariadb-data:/var/lib/mysql"], | ||
"environment" => { | ||
"MARIADB_ALLOW_EMPTY_ROOT_PASSWORD" => true, | ||
}, | ||
} | ||
} | ||
end | ||
|
||
def db_service_names | ||
["mysql", "mariadb", "postgres"] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
railties/lib/rails/generators/rails/app/templates/.devcontainer/Dockerfile.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version | ||
ARG RUBY_VERSION=<%= devcontainer_ruby_version %> | ||
FROM mcr.microsoft.com/devcontainers/ruby:1-$RUBY_VERSION-bookworm | ||
|
||
<%- unless options.skip_active_storage -%> | ||
# Install packages needed to build gems | ||
RUN apt-get update -qq && \ | ||
apt-get install --no-install-recommends -y \ | ||
libvips \ | ||
# For video thumbnails | ||
ffmpeg \ | ||
# For pdf thumbnails. If you want to use mupdf instead of poppler, | ||
# you can install the following packages instead: | ||
# mupdf mupdf-tools | ||
poppler-utils | ||
<%- end -%> |
55 changes: 55 additions & 0 deletions
55
railties/lib/rails/generators/rails/app/templates/.devcontainer/compose.yaml.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
services: | ||
rails-app: | ||
build: | ||
context: .. | ||
dockerfile: .devcontainer/Dockerfile | ||
|
||
volumes: | ||
- ../..:/workspaces:cached | ||
|
||
# Overrides default command so things don't shut down after the process ends. | ||
command: sleep infinity | ||
|
||
networks: | ||
- default | ||
|
||
# Uncomment the next line to use a non-root user for all processes. | ||
# user: vscode | ||
|
||
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally. | ||
# (Adding the "ports" property to this file will not forward from a Codespace.) | ||
ports: | ||
- 45678:45678 | ||
<%- if !devcontainer_dependencies.empty? -%> | ||
depends_on: | ||
<%- devcontainer_dependencies.each do |dependency| -%> | ||
- <%= dependency %> | ||
<%- end -%> | ||
<%- end -%> | ||
|
||
<%- if depends_on_system_test? -%> | ||
selenium: | ||
image: seleniarm/standalone-chromium | ||
restart: unless-stopped | ||
networks: | ||
- default | ||
<%- end -%> | ||
|
||
<%- if devcontainer_needs_redis? -%> | ||
redis: | ||
image: redis:7.2 | ||
restart: unless-stopped | ||
networks: | ||
- default | ||
volumes: | ||
- redis-data:/data | ||
<%- end -%> | ||
|
||
<%= devcontainer_db_service_yaml(indentation: 4) %> | ||
|
||
<%- if !devcontainer_volumes.empty? -%> | ||
volumes: | ||
<%- devcontainer_volumes.each do |volume| -%> | ||
<%= volume %>: | ||
<%- end -%> | ||
<%- end -%> |
34 changes: 34 additions & 0 deletions
34
railties/lib/rails/generators/rails/app/templates/.devcontainer/devcontainer.json.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// For format details, see https://aka.ms/devcontainer.json. For config options, see the | ||
// README at: https://github.com/devcontainers/templates/tree/main/src/ruby | ||
{ | ||
"name": "<%= app_name %>", | ||
"dockerComposeFile": "compose.yaml", | ||
"service": "rails-app", | ||
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}", | ||
|
||
// Features to add to the dev container. More info: https://containers.dev/features. | ||
"features": { | ||
"ghcr.io/devcontainers/features/github-cli:1": {} | ||
}, | ||
|
||
<%- if !devcontainer_variables.empty? -%> | ||
"containerEnv": { | ||
<%= devcontainer_variables.map { |key, value| "\"#{key}\": \"#{value}\"" }.join(",\n ") %> | ||
}, | ||
<%- end -%> | ||
|
||
// Features to add to the dev container. More info: https://containers.dev/features. | ||
// "features": {}, | ||
|
||
// Use 'forwardPorts' to make a list of ports inside the container available locally. | ||
// "forwardPorts": [], | ||
|
||
// Configure tool-specific properties. | ||
// "customizations": {}, | ||
|
||
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. | ||
// "remoteUser": "root", | ||
|
||
// Use 'postCreateCommand' to run commands after the container is created. | ||
"postCreateCommand": "bin/setup" | ||
} |
2 changes: 1 addition & 1 deletion
2
railties/lib/rails/generators/rails/app/templates/Dockerfile.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.