123456789_123456789_123456789_123456789_123456789_

Class: Rails::Generators::DevcontainerGenerator

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Base, Thor::Group
Instance Chain:
self, Base, Actions, Thor::Actions, Thor::Group
Inherits: Rails::Generators::Base
Defined in: railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb

Class Attribute Summary

Base - Inherited

Class Method Summary

Base - Inherited

.base_root

Returns the base root for a common set of generators.

.default_source_root

Returns the default source root for a given generator.

.desc

Tries to get the description from a USAGE file one folder above the source root otherwise uses a default description.

.hide!

Convenience method to hide this generator from the available ones when running rails generator command.

.hook_for

Invoke a generator based on the value supplied by the user to the given option named “name”.

.namespace

Convenience method to get the namespace from the class name.

.remove_hook_for

Remove a previously added hook.

.source_root

Returns the source root for this generator using default_source_root as default.

.add_shebang_option!

Small macro to add ruby as an option to the generator with proper default value plus an instance helper method called shebang.

.banner

Use Rails default banner.

.base_name

Sets the base_name taking into account the current class namespace.

.default_aliases_for_option

Returns default aliases for the option name given doing a lookup in aliases.

.default_for_option

Returns default for the option name given doing a lookup in config.

.default_generator_root,
.default_value_for_option

Returns the default value for the option name given doing a lookup in options.

.generator_name

Removes the namespaces and get the generator name.

.usage_path,
.class_option

Make class option aware of options and aliases.

.inherited

Cache source root and add lib/generators/base/generator/templates to source paths.

.hooks

Keep hooks configuration that are used on prepare_for_invocation.

.prepare_for_invocation

Prepare class invocation to search on ::Rails namespace if a previous added hook is being used.

Instance Attribute Summary

Base - Inherited

Instance Method Summary

Base - Inherited

#class_collisions

Check whether the given class names are already taken by user application or Ruby on ::Rails.

#extract_last_module

Takes in an array of nested modules and extracts the last module.

#indent,
#module_namespacing

Wrap block with namespace of current application if namespace exists and is not skipped.

#namespace, #namespace_dirs, #namespaced_path, #wrap_with_namespace

Actions - Included

#add_source

Add the given source to Gemfile

#application
#environment

Adds configuration code to a Rails runtime environment.

#gem

Adds a gem declaration to the Gemfile for the specified gem.

#gem_group

Wraps gem entries inside a group.

#generate

Runs another generator.

#git

Runs one or more git commands.

#github,
#initializer

Creates an initializer file in config/initializers/.

#lib

Creates a file in lib/.

#rails_command

Runs the specified Rails command.

#rake

Runs the specified Rake task.

#rakefile

Creates a Rake tasks file in lib/tasks/.

#readme

Reads the given file at the source root and prints it in the console.

#route

Make an entry in Rails routing file config/routes.rb.

#vendor

Creates a file in vendor/.

#append_file_with_newline

Append string to a file with a newline if necessary.

#execute_command

Runs the supplied command using either “rake …” or “rails …” based on the executor parameter provided.

#indentation

Indent the Gemfile to the depth of @indentation.

#log

Define log for backwards compatibility.

#match_file,
#optimize_indentation

Returns optimized string with indentation.

#quote

Always returns value in double quotes.

#rebase_indentation
#route_namespace_pattern,
#with_indentation

Manage Gemfile indentation for a DSL action block.

#initialize

Instance Attribute Details

#devcontainer?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb', line 62

def devcontainer?
  true
end

Instance Method Details

#app_name (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb', line 66

def app_name
  options[:app_name]
end

#container_env (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb', line 81

def container_env
  return @container_env if @container_env

  @container_env = {}

  @container_env["CAPYBARA_SERVER_PORT"] = "45678" if options[:system_test]
  @container_env["SELENIUM_HOST"] = "selenium" if options[:system_test]
  @container_env["REDIS_URL"] = "redis://redis:6379/1" if options[:redis]
  @container_env["KAMAL_REGISTRY_PASSWORD"] = "$KAMAL_REGISTRY_PASSWORD" if options[:kamal]
  @container_env["DB_HOST"] = database.name if database.service

  @container_env
end

#create_devcontainer

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb', line 37

def create_devcontainer
  empty_directory ".devcontainer"

  template "devcontainer/devcontainer.json", ".devcontainer/devcontainer.json"
  template "devcontainer/Dockerfile", ".devcontainer/Dockerfile"
  template "devcontainer/compose.yaml", ".devcontainer/compose.yaml"
end

#database (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb', line 142

def database
  @database ||= Database.build(options[:database])
end

#dependencies (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb', line 70

def dependencies
  return @dependencies if @dependencies

  @dependencies = []

  @dependencies << "selenium" if options[:system_test]
  @dependencies << "redis" if options[:redis]
  @dependencies << database.name if database.service
  @dependencies
end

#devcontainer_db_service_yaml(**options) (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb', line 146

def devcontainer_db_service_yaml(**options)
  return unless service = database.service

  { database.name => service }.to_yaml(**options)[4..-1]
end

#features (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb', line 106

def features
  return @features if @features

  @features = {
    "ghcr.io/devcontainers/features/github-cli:1" => {}
  }

  @features["ghcr.io/rails/devcontainer/features/activestorage"] = {} if options[:active_storage]
  @features["ghcr.io/devcontainers/features/node:1"] = {} if options[:node]
  @features["ghcr.io/devcontainers/features/docker-outside-of-docker:1"] = {} if options[:kamal]

  @features.merge!(database.feature) if database.feature

  @features
end

#forward_ports (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb', line 132

def forward_ports
  return @forward_ports if @forward_ports

  @forward_ports = [3000]
  @forward_ports << database.port if database.port
  @forward_ports << 6379 if options[:redis]

  @forward_ports
end

#local_rails_mount (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb', line 152

def local_rails_mount
  {
    type: "bind",
    source: Rails::Generators::RAILS_DEV_PATH,
    target: Rails::Generators::RAILS_DEV_PATH
  }
end

#mounts (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb', line 122

def mounts
  return @mounts if @mounts

  @mounts = []

  @mounts << local_rails_mount if options[:dev]

  @mounts
end

#system_test_configuration (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb', line 160

def system_test_configuration
  optimize_indentation(<<-'RUBY', 2).chomp
    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
  RUBY
end

#update_application_system_test_case

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb', line 45

def update_application_system_test_case
  return unless options[:system_test]

  system_test_case_path = File.expand_path "test/application_system_test_case.rb", destination_root
  return unless File.exist? system_test_case_path

  gsub_file(system_test_case_path, /^\s*driven_by\b.*/, system_test_configuration)
end

#update_database_yml

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb', line 54

def update_database_yml
  # Only postgresql has devcontainer specific configuration, so only update database.yml if we are using postgres
  return unless options[:database] == "postgresql"

  template("config/databases/#{options[:database]}.yml", "config/database.yml")
end

#volumes (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb', line 95

def volumes
  return @volumes if @volumes

  @volumes = []

  @volumes << "redis-data" if options[:redis]
  @volumes << database.volume if database.volume

  @volumes
end