123456789_123456789_123456789_123456789_123456789_

Class: Rails::Generators::AppBase

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

Constant Summary

AppName - Included

RESERVED_NAMES

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

Instance Method Summary

AppName - Included

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

Constructor Details

.new(positional_argv, option_argv) ⇒ AppBase

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 141

def initialize(positional_argv, option_argv, *)
  @argv = [*positional_argv, *option_argv]
  @gem_filter = lambda { |gem| true }
  super
end

Class Method Details

.add_shared_options_for(name)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 32

def self.add_shared_options_for(name)
  class_option :name,                type: :string, aliases: "-n",
                                     desc: "Name of the app"

  class_option :template,            type: :string, aliases: "-m",
                                     desc: "Path to some #{name} template (can be a filesystem path or URL)"

  class_option :database,            type: :string, aliases: "-d", default: "sqlite3",
                                     enum: Database::DATABASES,
                                     desc: "Preconfigure for selected database"

  class_option :skip_git,            type: :boolean, aliases: "-G", default: nil,
                                     desc: "Skip git init, .gitignore and .gitattributes"

  class_option :skip_docker,         type: :boolean, default: nil,
                                     desc: "Skip Dockerfile, .dockerignore and bin/docker-entrypoint"

  class_option :skip_keeps,          type: :boolean, default: nil,
                                     desc: "Skip source control .keep files"

  class_option :skip_action_mailer,  type: :boolean, aliases: "-M",
                                     default: nil,
                                     desc: "Skip Action Mailer files"

  class_option :skip_action_mailbox, type: :boolean, default: nil,
                                     desc: "Skip Action Mailbox gem"

  class_option :skip_action_text,    type: :boolean, default: nil,
                                     desc: "Skip Action Text gem"

  class_option :skip_active_record,  type: :boolean, aliases: "-O", default: nil,
                                     desc: "Skip Active Record files"

  class_option :skip_active_job,     type: :boolean, default: nil,
                                     desc: "Skip Active Job"

  class_option :skip_active_storage, type: :boolean, default: nil,
                                     desc: "Skip Active Storage files"

  class_option :skip_action_cable,   type: :boolean, aliases: "-C", default: nil,
                                     desc: "Skip Action Cable files"

  class_option :skip_asset_pipeline, type: :boolean, aliases: "-A", default: nil

  class_option :skip_javascript,     type: :boolean, aliases: ["-J", "--skip-js"], default: (true if name == "plugin"),
                                     desc: "Skip JavaScript files"

  class_option :skip_hotwire,        type: :boolean, default: nil,
                                     desc: "Skip Hotwire integration"

  class_option :skip_jbuilder,       type: :boolean, default: nil,
                                     desc: "Skip jbuilder gem"

  class_option :skip_test,           type: :boolean, aliases: "-T", default: nil,
                                     desc: "Skip test files"

  class_option :skip_system_test,    type: :boolean, default: nil,
                                     desc: "Skip system test files"

  class_option :skip_bootsnap,       type: :boolean, default: nil,
                                     desc: "Skip bootsnap gem"

  class_option :skip_dev_gems,       type: :boolean, default: nil,
                                     desc: "Skip development gems (e.g., web-console)"

  class_option :skip_thruster,       type: :boolean, default: nil,
                                     desc: "Skip Thruster setup"

  class_option :skip_rubocop,        type: :boolean, default: nil,
                                     desc: "Skip RuboCop setup"

  class_option :skip_brakeman,       type: :boolean, default: nil,
                                     desc: "Skip brakeman setup"

  class_option :skip_ci,             type: :boolean, default: nil,
                                     desc: "Skip GitHub CI files"

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

  class_option :skip_solid,          type: :boolean, default: nil,
                                     desc: "Skip Solid Cache, Queue, and Cable 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: nil,
                                     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"

  class_option :main,                type: :boolean, default: nil, aliases: "--master",
                                     desc: "Set up the #{name} with Gemfile pointing to Rails repository main branch"

  class_option :rc,                  type: :string, default: nil,
                                     desc: "Path to file containing extra configuration options for rails command"

  class_option :no_rc,               type: :boolean, default: nil,
                                     desc: "Skip loading of extra configuration options from .railsrc file"

  class_option :help,                type: :boolean, aliases: "-h", group: :rails,
                                     desc: "Show this help message and quit"
end

.edge_branch

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 137

def self.edge_branch # :nodoc:
  Rails.gem_version.prerelease? ? "main" : [*Rails.gem_version.segments.first(2), "stable"].join("-")
end

.strict_args_position

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 28

def self.strict_args_position
  false
end

Instance Attribute Details

#bundle_install?Boolean (readonly)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 666

def bundle_install?
  !(options[:skip_bundle] || options[:pretend])
end

#depend_on_bootsnap?Boolean (readonly)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 674

def depend_on_bootsnap?
  !options[:skip_bootsnap] && !options[:dev] && !defined?(JRUBY_VERSION)
end

#depends_on_system_test?Boolean (readonly)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 670

def depends_on_system_test?
  !(options[:skip_system_test] || options[:skip_test] || options[:api])
end

#devcontainer?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 408

def devcontainer?
  options[:devcontainer]
end

#include_all_railties?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 315

def include_all_railties? # :doc:
  required_railties.values.all?
end

#keeps?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 348

def keeps? # :doc:
  !options[:skip_keeps]
end

#rails_prerelease?Boolean (readonly)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 456

def rails_prerelease?
  options.dev? || options.edge? || options.main?
end

#rails_template (rw)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 23

attr_accessor :rails_template

#skip_action_cable?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 368

def skip_action_cable? # :doc:
  options[:skip_action_cable]
end

#skip_action_mailbox?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 376

def skip_action_mailbox? # :doc:
  options[:skip_action_mailbox]
end

#skip_action_mailer?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 372

def skip_action_mailer? # :doc:
  options[:skip_action_mailer]
end

#skip_action_text?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 380

def skip_action_text? # :doc:
  options[:skip_action_text]
end

#skip_active_record?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 356

def skip_active_record? # :doc:
  options[:skip_active_record]
end

#skip_active_storage?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 360

def skip_active_storage? # :doc:
  options[:skip_active_storage]
end

#skip_asset_pipeline?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 384

def skip_asset_pipeline? # :doc:
  options[:skip_asset_pipeline]
end

#skip_brakeman?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 396

def skip_brakeman?
  options[:skip_brakeman]
end

#skip_ci?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 400

def skip_ci?
  options[:skip_ci]
end

#skip_devcontainer?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 404

def skip_devcontainer?
  !options[:devcontainer]
end

#skip_kamal?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 412

def skip_kamal?
  options[:skip_kamal]
end

#skip_rubocop?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 392

def skip_rubocop?
  options[:skip_rubocop]
end

#skip_solid?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 416

def skip_solid?
  options[:skip_solid]
end

#skip_storage?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 364

def skip_storage? # :doc:
  skip_active_storage? && !sqlite3?
end

#skip_thruster?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 388

def skip_thruster?
  options[:skip_thruster]
end

#sqlite3?Boolean (readonly, private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 352

def sqlite3? # :doc:
  !skip_active_record? && options[:database] == "sqlite3"
end

#using_bun?Boolean (readonly)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 522

def using_bun?
  using_js_runtime? && %w[bun].include?(options[:javascript])
end

#using_js_runtime?Boolean (readonly)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 513

def using_js_runtime?
  (options[:javascript] && !%w[importmap].include?(options[:javascript])) ||
    (options[:css] && !%w[tailwind sass].include?(options[:css]))
end

#using_node?Boolean (readonly)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 518

def using_node?
  using_js_runtime? && !%w[bun].include?(options[:javascript])
end

Instance Method Details

#add_bundler_platforms

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 750

def add_bundler_platforms
  if bundle_install?
    # The vast majority of Rails apps will be deployed on `x86_64-linux`.
    bundle_command("lock --add-platform=x86_64-linux")

    # Users that develop on M1 mac may use docker and would need `aarch64-linux` as well.
    bundle_command("lock --add-platform=aarch64-linux") if RUBY_PLATFORM.start_with?("arm64")
  end
end

#apply_rails_template (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 264

def apply_rails_template # :doc:
  apply rails_template if rails_template
rescue Thor::Error, LoadError, Errno::ENOENT => e
  raise Error, "The template [#{rails_template}] could not be loaded. Error: #{e}"
end

#asset_pipeline_gemfile_entry (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 293

def asset_pipeline_gemfile_entry
  unless skip_asset_pipeline?
    GemfileEntry.floats "propshaft", "The modern asset pipeline for Rails [https://github.com/rails/propshaft]"
  end
end

#build(meth, *args) (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 170

def build(meth, *args) # :doc:
  builder.public_send(meth, *args) if builder.respond_to?(meth)
end

#builder (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 162

def builder # :doc:
  @builder ||= begin
    builder_class = get_builder_class
    builder_class.include(ActionMethods)
    builder_class.new(self)
  end
end

#bundle_command(command, env = {})

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 640

def bundle_command(command, env = {})
  say_status :run, "bundle #{command}"

  # We are going to shell out rather than invoking Bundler::CLI.new(command)
  # because `rails new` loads the Thor gem and on the other hand bundler uses
  # its own vendored Thor, which could be a different version. Running both
  # things in the same process is a recipe for a night with paracetamol.
  #
  # Thanks to James Tucker for the Gem tricks involved in this call.
  _bundle_command = Gem.bin_path("bundler", "bundle")

  require "bundler"
  Bundler.with_original_env do
    exec_bundle_command(_bundle_command, command, env)
  end
end

#cable_gemfile_entry

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 633

def cable_gemfile_entry
  if !options[:skip_action_cable] && options[:skip_solid]
    comment = "Use Redis adapter to run Action Cable in production"
    GemfileEntry.new("redis", ">= 4.0.1", comment, {}, true)
  end
end

#ci_packages

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 612

def ci_packages
  if depends_on_system_test?
    dockerfile_build_packages << "google-chrome-stable"
  else
    dockerfile_build_packages
  end
end

#comment_if(value) (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 335

def comment_if(value) # :doc:
  question = "#{value}?"

  comment =
    if respond_to?(question, true)
      send(question)
    else
      options[value]
    end

  comment ? "# " : ""
end

#create_root (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 257

def create_root # :doc:
  valid_const?

  empty_directory "."
  FileUtils.cd(destination_root) unless options[:pretend]
end

#css_gemfile_entry

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 620

def css_gemfile_entry
  return if options[:api]
  return unless options[:css]

  if !using_js_runtime? && options[:css] == "tailwind"
    GemfileEntry.floats "tailwindcss-rails", "Use Tailwind CSS [https://github.com/rails/tailwindcss-rails]"
  elsif !using_js_runtime? && options[:css] == "sass"
    GemfileEntry.floats "dartsass-rails", "Use Dart SASS [https://github.com/rails/dartsass-rails]"
  else
    GemfileEntry.floats "cssbundling-rails", "Bundle and process CSS [https://github.com/rails/cssbundling-rails]"
  end
end

#database

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 804

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

#database_gemfile_entry (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 281

def database_gemfile_entry # :doc:
  return if options[:skip_active_record]

  gem_name, gem_version = database.gem
  GemfileEntry.version gem_name, gem_version,
    "Use #{options[:database]} as the database for Active Record"
end

#deduce_implied_options(options, option_reasons, meta_options) (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 174

def deduce_implied_options(options, option_reasons, meta_options)
  active = options.transform_values { |value| [] if value }.compact
  irrevocable = (active.keys - meta_options).to_set

  deduction_order = TSort.tsort(
    ->(&block) { option_reasons.each_key(&block) },
    ->(key, &block) { option_reasons[key]&.each(&block) }
  )

  deduction_order.each do |name|
    active_reasons = option_reasons[name].to_a.select(&active)
    active[name] ||= active_reasons if active_reasons.any?
    irrevocable << name if active_reasons.any?(irrevocable)
  end

  revoked = options.select { |name, value| value == false }.keys.to_set - irrevocable
  deduction_order.reverse_each do |name|
    revoked += option_reasons[name].to_a if revoked.include?(name)
  end
  revoked -= meta_options

  active.filter_map do |name, reasons|
    unless revoked.include?(name) || reasons.all?(revoked)
      [name, reasons - revoked.to_a]
    end
  end.to_h
end

#dockerfile_base_packages

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 577

def dockerfile_base_packages
  # Add curl to work with the default health check strategy in Kamal
  packages = ["curl"]

  # ActiveRecord databases
  packages << database.base_package unless skip_active_record?

  # ActiveStorage preview support
  packages << "libvips" unless skip_active_storage?

  # jemalloc for memory optimization
  packages << "libjemalloc2"

  packages.compact.sort
end

#dockerfile_binfile_fixups

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 548

def dockerfile_binfile_fixups
  # binfiles may have OS specific paths to ruby.  Normalize them.
  shebangs = Dir["bin/*"].map { |file| IO.read(file).lines.first }.join
  rubies = shebangs.scan(%r{#!/usr/bin/env (ruby.*)}).flatten.uniq

  binfixups = (rubies - %w(ruby)).map do |ruby|
    "sed -i 's/#{Regexp.quote(ruby)}$/ruby/' bin/*"
  end

  # Windows line endings will cause scripts to fail.  If any
  # or found OR this generation is run on a windows platform
  # and there are other binfixups required, then convert
  # line endings.  This avoids adding unnecessary fixups if
  # none are required, but prepares for the need to do the
  # fix line endings if other fixups are required.
  has_cr = Dir["bin/*"].any? { |file| IO.read(file).include? "\r" }
  if has_cr || (Gem.win_platform? && !binfixups.empty?)
    binfixups.unshift 'sed -i "s/\r$//g" bin/*'
  end

  # Windows file systems may not have the concept of executable.
  # In such cases, fix up during the build.
  unless Dir["bin/*"].all? { |file| File.executable? file }
    binfixups.unshift "chmod +x bin/*"
  end

  binfixups
end

#dockerfile_build_packages

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 593

def dockerfile_build_packages
  # start with the essentials
  packages = %w(build-essential git pkg-config)

  # add database support
  packages << database.build_package unless skip_active_record?

  packages << "unzip" if using_bun?

  # node support, including support for building native modules
  if using_node?
    packages << "node-gyp" # pkg-config already listed above

    packages << "python-is-python3"
  end

  packages.compact.sort
end

#dockerfile_bun_version

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 542

def dockerfile_bun_version
  using_bun? and `bun --version`[/\d\.\d\.\d+/]
rescue
  BUN_VERSION
end

#dockerfile_chown_directories

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 795

def dockerfile_chown_directories
  directories = %w(log tmp)

  directories << "storage" unless skip_storage?
  directories << "db" unless skip_active_record?

  directories.sort
end

#dockerfile_yarn_version

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 536

def dockerfile_yarn_version
  using_node? and `yarn --version`[/\d\.\d\.\d+/]
rescue
  "latest"
end

#edge_branch

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 791

def edge_branch
  self.class.edge_branch
end

#empty_directory_with_keep_file(destination, config = {})

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 766

def empty_directory_with_keep_file(destination, config = {})
  empty_directory(destination, config)
  keep_file(destination)
end

#exec_bundle_command(bundle_command, command, env)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 657

def exec_bundle_command(bundle_command, command, env)
  full_command = %Q["#{Gem.ruby}" "#{bundle_command}" #{command}]
  if options[:quiet]
    system(env, full_command, out: File::NULL)
  else
    system(env, full_command)
  end
end

#gemfile_entries (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 148

def gemfile_entries # :doc:
  [
    rails_gemfile_entry,
    asset_pipeline_gemfile_entry,
    database_gemfile_entry,
    web_server_gemfile_entry,
    javascript_gemfile_entry,
    hotwire_gemfile_entry,
    css_gemfile_entry,
    jbuilder_gemfile_entry,
    cable_gemfile_entry,
  ].flatten.compact.select(&@gem_filter)
end

#generate_bundler_binstub

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 760

def generate_bundler_binstub
  if bundle_install?
    bundle_command("binstubs bundler")
  end
end

#git_init_command

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 779

def git_init_command
  return "git init" if user_default_branch.strip.present?

  git_version = `git --version`[/\d\.\d\.\d+/]

  if Gem::Version.new(git_version) >= Gem::Version.new("2.28.0")
    "git init -b main"
  else
    "git init && git symbolic-ref HEAD refs/heads/main"
  end
end

#hotwire_gemfile_entry

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 501

def hotwire_gemfile_entry
  return if options[:skip_hotwire]

  turbo_rails_entry =
    GemfileEntry.floats "turbo-rails", "Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]"

  stimulus_rails_entry =
    GemfileEntry.floats "stimulus-rails", "Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]"

  [ turbo_rails_entry, stimulus_rails_entry ]
end

#imply_options(option_implications = OPTION_IMPLICATIONS, meta_options: []) (private)

Options

:meta_options

A list of generator options which only serve to trigger other options. These options should have no other effects, and will be treated transparently when revoking other options.

For example: –minimal implies both –skip-active-job and –skip-active-storage. Also, –skip-active-job by itself implies –skip-active-storage. If –skip-active-job is explicitly specified, –no-skip-active-storage should raise an error. But, if only –minimal is specified, –no-skip-active-storage should “undo” the implied –skip-active-job. This can be accomplished by passing meta_options: [:minimal].

In contrast, –api is not a meta option because it does other things besides implying options such as –skip-asset-pipeline. (And so –api with –no-skip-asset-pipeline should raise an error.)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 227

def imply_options(option_implications = OPTION_IMPLICATIONS, meta_options: [])
  option_reasons = {}
  option_implications.each do |reason, implications|
    implications.each do |implication|
      (option_reasons[implication.to_s] ||= []) << reason.to_s
    end
  end

  @implied_options = deduce_implied_options(options, option_reasons, meta_options.map(&:to_s))
  @implied_options_conflicts = @implied_options.keys.select { |name| options[name] == false }
  self.options = options.merge(@implied_options.transform_values { true }).freeze
end

#javascript_gemfile_entry

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 491

def javascript_gemfile_entry
  return if options[:skip_javascript]

  if options[:javascript] == "importmap"
    GemfileEntry.floats "importmap-rails", "Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]"
  else
    GemfileEntry.floats "jsbundling-rails", "Bundle and transpile JavaScript [https://github.com/rails/jsbundling-rails]"
  end
end

#jbuilder_gemfile_entry

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 486

def jbuilder_gemfile_entry
  return if options[:skip_jbuilder]
  GemfileEntry.new "jbuilder", nil, "Build JSON APIs with ease [https://github.com/rails/jbuilder]", {}, options[:api]
end

#keep_file(destination)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 771

def keep_file(destination)
  create_file("#{destination}/.keep") if keeps?
end

#node_version

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 526

def node_version
  if using_node?
    ENV.fetch("NODE_VERSION") do
      `node --version`[/\d\.\d\.\d+/]
    rescue
      NODE_LTS_VERSION
    end
  end
end

#rails_gemfile_entry

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 460

def rails_gemfile_entry
  if options.dev?
    GemfileEntry.path("rails", Rails::Generators::RAILS_DEV_PATH, "Use local checkout of Rails")
  elsif options.edge?
    GemfileEntry.github("rails", "rails/rails", edge_branch, "Use specific branch of Rails")
  elsif options.main?
    GemfileEntry.github("rails", "rails/rails", "main", "Use main development branch of Rails")
  else
    GemfileEntry.version("rails", rails_version_specifier,
      %(Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"))
  end
end

#rails_require_statement (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 319

def rails_require_statement
  if include_all_railties?
    %(require "rails/all")
  else
    require_statements = required_railties.map do |railtie, required|
      %(#{"# " if !required}require "#{railtie}")
    end

    <<~RUBY.strip
      require "rails"
      # Pick the frameworks you want:
      #{require_statements.join("\n")}
    RUBY
  end
end

#rails_version_specifier(gem_version = Rails.gem_version)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 473

def rails_version_specifier(gem_version = Rails.gem_version)
  if gem_version.segments.size == 3 || gem_version.release.segments.size == 3
    # ~> 1.2.3
    # ~> 1.2.3.pre4
    "~> #{gem_version}"
  else
    # ~> 1.2.3, >= 1.2.3.4
    # ~> 1.2.3, >= 1.2.3.4.pre5
    patch = gem_version.segments[0, 3].join(".")
    ["~> #{patch}", ">= #{gem_version}"]
  end
end

#report_implied_options (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 240

def report_implied_options
  return if @implied_options.blank?

  say "Based on the specified options, the following options will also be activated:"
  say ""
  @implied_options.each do |name, reasons|
    due_to = reasons.map { |reason| "--#{reason.dasherize}" }.join(", ")
    say "  --#{name.dasherize} [due to #{due_to}]"
    if @implied_options_conflicts.include?(name)
      say "    ERROR: Conflicts with --no-#{name.dasherize}", :red
    end
  end
  say ""

  raise "Cannot proceed due to conflicting options" if @implied_options_conflicts.any?
end

#required_railties (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 299

def required_railties
  @required_railties ||= {
    "active_model/railtie"      => true,
    "active_job/railtie"        => !options[:skip_active_job],
    "active_record/railtie"     => !options[:skip_active_record],
    "active_storage/engine"     => !options[:skip_active_storage],
    "action_controller/railtie" => true,
    "action_mailer/railtie"     => !options[:skip_action_mailer],
    "action_mailbox/engine"     => !options[:skip_action_mailbox],
    "action_text/engine"        => !options[:skip_action_text],
    "action_view/railtie"       => true,
    "action_cable/engine"       => !options[:skip_action_cable],
    "rails/test_unit/railtie"   => !options[:skip_test],
  }
end

#run_bundle

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 700

def run_bundle
  bundle_command("install --quiet", "BUNDLE_IGNORE_MESSAGES" => "1") if bundle_install?
end

#run_css

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 719

def run_css
  return if !options[:css] || !bundle_install?

  if !using_js_runtime? && options[:css] == "tailwind"
    rails_command "tailwindcss:install"
  elsif !using_js_runtime? && options[:css] == "sass"
    rails_command "dartsass:install"
  else
    rails_command "css:install:#{options[:css]}"
  end
end

#run_hotwire

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 713

def run_hotwire
  return if options[:skip_hotwire] || !bundle_install?

  rails_command "turbo:install stimulus:install"
end

#run_javascript

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 704

def run_javascript
  return if options[:skip_javascript] || !bundle_install?

  case options[:javascript]
  when "importmap"                           then rails_command "importmap:install"
  when "webpack", "bun", "esbuild", "rollup" then rails_command "javascript:install:#{options[:javascript]}"
  end
end

#run_kamal

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 731

def run_kamal
  return if options[:skip_kamal] || !bundle_install?

  bundle_command "binstubs kamal"
  bundle_command "exec kamal init"

  template "kamal-secrets.tt", ".kamal/secrets", force: true
  template "config/deploy.yml", force: true
end

#run_solid

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 741

def run_solid
  return if skip_solid? || !bundle_install?

  commands = "solid_cache:install solid_queue:install"
  commands += " solid_cable:install" unless skip_action_cable?

  rails_command commands
end

#set_default_accessors! (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 270

def set_default_accessors! # :doc:
  self.destination_root = File.expand_path(app_path, destination_root)

  if options[:template].is_a?(String) && !options[:template].match?(/^https?:\/\//)
    interpolated = options[:template].gsub(/\$(\w+)|\$\{\g<1>\}|%\g<1>%/) { |m| ENV[$1] || m }
    self.rails_template = File.expand_path(interpolated)
  else
    self.rails_template = options[:template]
  end
end

#target_rails_prerelease(self_command = "new")

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 678

def target_rails_prerelease(self_command = "new")
  return unless rails_prerelease? && bundle_install?

  if !File.exist?(File.expand_path("Gemfile", destination_root))
    create_file("Gemfile", <<~GEMFILE)
      source "https://rubygems.org"
      #{rails_gemfile_entry}
    GEMFILE

    run_bundle

    @argv.delete_at(@argv.index(app_path))
    @argv.unshift(destination_root)
    require "shellwords"
    bundle_command("exec rails #{self_command} #{Shellwords.join(@argv)}")
    exit
  else
    remove_file("Gemfile")
    remove_file("Gemfile.lock")
  end
end

#user_default_branch

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 775

def user_default_branch
  @user_default_branch ||= `git config init.defaultbranch`
end

#web_server_gemfile_entry (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/app_base.rb', line 289

def web_server_gemfile_entry # :doc:
  GemfileEntry.new "puma", ">= 5.0", "Use the Puma web server [https://github.com/puma/puma]"
end