123456789_123456789_123456789_123456789_123456789_

Module: Rails::Command

Relationships & Source Files
Namespace Children
Modules:
Classes:
Exceptions:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
self, Behavior
Defined in: railties/lib/rails/command.rb,
railties/lib/rails/command/actions.rb,
railties/lib/rails/command/base.rb,
railties/lib/rails/command/behavior.rb,
railties/lib/rails/command/environment_argument.rb,
railties/lib/rails/command/helpers/editor.rb,
railties/lib/rails/commands/about/about_command.rb,
railties/lib/rails/commands/app/update_command.rb,
railties/lib/rails/commands/application/application_command.rb,
railties/lib/rails/commands/boot/boot_command.rb,
railties/lib/rails/commands/console/console_command.rb,
railties/lib/rails/commands/credentials/credentials_command.rb,
railties/lib/rails/commands/db/system/change/change_command.rb,
railties/lib/rails/commands/dbconsole/dbconsole_command.rb,
railties/lib/rails/commands/destroy/destroy_command.rb,
railties/lib/rails/commands/dev/dev_command.rb,
railties/lib/rails/commands/devcontainer/devcontainer_command.rb,
railties/lib/rails/commands/encrypted/encrypted_command.rb,
railties/lib/rails/commands/gem_help/gem_help_command.rb,
railties/lib/rails/commands/generate/generate_command.rb,
railties/lib/rails/commands/help/help_command.rb,
railties/lib/rails/commands/initializers/initializers_command.rb,
railties/lib/rails/commands/middleware/middleware_command.rb,
railties/lib/rails/commands/new/new_command.rb,
railties/lib/rails/commands/notes/notes_command.rb,
railties/lib/rails/commands/plugin/plugin_command.rb,
railties/lib/rails/commands/rake/rake_command.rb,
railties/lib/rails/commands/restart/restart_command.rb,
railties/lib/rails/commands/routes/routes_command.rb,
railties/lib/rails/commands/runner/runner_command.rb,
railties/lib/rails/commands/secret/secret_command.rb,
railties/lib/rails/commands/server/server_command.rb,
railties/lib/rails/commands/stats/stats_command.rb,
railties/lib/rails/commands/test/test_command.rb,
railties/lib/rails/commands/unused_routes/unused_routes_command.rb,
railties/lib/rails/commands/version/version_command.rb

Constant Summary

Class Method Summary

::ActiveSupport::Autoload - Extended

Class Method Details

.application_root

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/command.rb', line 110

def application_root # :nodoc:
  Pathname.new(File.expand_path("../..", APP_PATH)) if defined?(APP_PATH)
end

.command_type (private)

[ GitHub ]

  
# File 'railties/lib/rails/command.rb', line 153

def command_type # :doc:
  @command_type ||= "command"
end

.environment

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/command.rb', line 51

def environment # :nodoc:
  ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence || "development"
end

.file_lookup_paths (private)

[ GitHub ]

  
# File 'railties/lib/rails/command.rb', line 161

def file_lookup_paths # :doc:
  @file_lookup_paths ||= [ "{#{lookup_paths.join(',')}}", "**", "*_command.rb" ]
end

.find_by_namespace(namespace, command_name = nil)

This method is for internal use only.

::Rails finds namespaces similar to Thor, it only adds one rule:

Command names must end with “_command.rb”. This is required because ::Rails looks in load paths and loads the command just before it’s going to be used.

find_by_namespace :webrat, :integration

Will search for the following commands:

"webrat", "webrat:integration", "rails:webrat", "rails:webrat:integration"
[ GitHub ]

  
# File 'railties/lib/rails/command.rb', line 90

def find_by_namespace(namespace, command_name = nil) # :nodoc:
  lookups = [ namespace ]
  lookups << "#{namespace}:#{command_name}" if command_name
  lookups.concat lookups.map { |lookup| "rails:#{lookup}" }

  lookup(lookups)

  namespaces = subclasses.index_by(&:namespace)
  namespaces[(lookups & namespaces.keys).first]
end

.hidden_commands

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/command.rb', line 47

def hidden_commands # :nodoc:
  @hidden_commands ||= []
end

.invoke(full_namespace, args = [], **config)

Receives a namespace, arguments, and the behavior to invoke the command.

[ GitHub ]

  
# File 'railties/lib/rails/command.rb', line 56

def invoke(full_namespace, args = [], **config)
  args = ["--help"] if rails_new_with_no_path?(args)

  full_namespace = full_namespace.to_s
  namespace, command_name = split_namespace(full_namespace)
  command = find_by_namespace(namespace, command_name)

  with_argv(args) do
    if command && command.all_commands[command_name]
      command.perform(command_name, args, config)
    else
      invoke_rake(full_namespace, args, config)
    end
  end
rescue UnrecognizedCommandError => error
  if error.name == full_namespace && command && command_name == full_namespace
    command.perform("help", [], config)
  else
    puts error.detailed_message
  end
  exit(1)
end

.invoke_rake(task, args, config) (private)

[ GitHub ]

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

def invoke_rake(task, args, config)
  args = ["--describe", task] if HELP_MAPPINGS.include?(args[0])
  find_by_namespace("rake").perform(task, args, config)
end

.lookup_paths (private)

[ GitHub ]

  
# File 'railties/lib/rails/command.rb', line 157

def lookup_paths # :doc:
  @lookup_paths ||= %w( rails/commands commands )
end

.printing_commands

This method is for internal use only.
[ GitHub ]

  
# File 'railties/lib/rails/command.rb', line 114

def printing_commands # :nodoc:
  lookup!

  (subclasses - hidden_commands).flat_map(&:printing_commands)
end

.rails_new_with_no_path?(args) ⇒ Boolean (private)

[ GitHub ]

  
# File 'railties/lib/rails/command.rb', line 121

def rails_new_with_no_path?(args)
  args == ["new"]
end

.root

Returns the root of the Rails engine or app running the command.

[ GitHub ]

  
# File 'railties/lib/rails/command.rb', line 102

def root
  if defined?(ENGINE_ROOT)
    Pathname.new(ENGINE_ROOT)
  else
    application_root
  end
end

.split_namespace(namespace) (private)

[ GitHub ]

  
# File 'railties/lib/rails/command.rb', line 125

def split_namespace(namespace)
  case namespace
  when /^(.):(\w)$/
    [$1, $2]
  when ""
    ["help", "help"]
  when HELP_MAPPINGS, "help"
    ["help", "help_extended"]
  when VERSION_MAPPINGS
    ["version", "version"]
  else
    [namespace, namespace]
  end
end

.with_argv(argv) (private)

[ GitHub ]

  
# File 'railties/lib/rails/command.rb', line 140

def with_argv(argv)
  original_argv = ARGV.dup
  ARGV.replace(argv)
  yield
ensure
  ARGV.replace(original_argv)
end