Module: Rails::Command

Extended by:
ActiveSupport::Autoload
Includes:
Behavior
Defined in:
railties/lib/rails/command.rb,
railties/lib/rails/command/base.rb,
railties/lib/rails/command/actions.rb,
railties/lib/rails/command/behavior.rb,
railties/lib/rails/command/spellchecker.rb,
railties/lib/rails/command/helpers/editor.rb,
railties/lib/rails/commands/dev/dev_command.rb,
railties/lib/rails/commands/new/new_command.rb,
railties/lib/rails/commands/help/help_command.rb,
railties/lib/rails/commands/rake/rake_command.rb,
railties/lib/rails/commands/test/test_command.rb,
railties/lib/rails/command/environment_argument.rb,
railties/lib/rails/commands/notes/notes_command.rb,
railties/lib/rails/commands/plugin/plugin_command.rb,
railties/lib/rails/commands/routes/routes_command.rb,
railties/lib/rails/commands/runner/runner_command.rb,
railties/lib/rails/commands/server/server_command.rb,
railties/lib/rails/commands/console/console_command.rb,
railties/lib/rails/commands/destroy/destroy_command.rb,
railties/lib/rails/commands/secrets/secrets_command.rb,
railties/lib/rails/commands/version/version_command.rb,
railties/lib/rails/commands/generate/generate_command.rb,
railties/lib/rails/commands/dbconsole/dbconsole_command.rb,
railties/lib/rails/commands/encrypted/encrypted_command.rb,
railties/lib/rails/commands/application/application_command.rb,
railties/lib/rails/commands/credentials/credentials_command.rb,
railties/lib/rails/commands/db/system/change/change_command.rb,
railties/lib/rails/commands/initializers/initializers_command.rb

Defined Under Namespace

Modules: Actions, Behavior, Db, EnvironmentArgument, Helpers, Spellchecker Classes: ApplicationCommand, Base, ConsoleCommand, CredentialsCommand, DbconsoleCommand, DestroyCommand, DevCommand, EncryptedCommand, GenerateCommand, HelpCommand, InitializersCommand, NewCommand, NotesCommand, PluginCommand, RakeCommand, RoutesCommand, RunnerCommand, SecretsCommand, ServerCommand, TestCommand, VersionCommand

Constant Summary collapse

HELP_MAPPINGS =
%w(-h -? --help)

Class Method Summary collapse

Methods included from ActiveSupport::Autoload

autoload, autoload_at, autoload_under, autoloads, eager_autoload, eager_load!, extended

Methods included from ActiveSupport::Concern

#append_features, #class_methods, extended, #included, #prepend_features, #prepended

Class Method Details

.environmentObject

:nodoc:


26
27
28
# File 'railties/lib/rails/command.rb', line 26

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

.find_by_namespace(namespace, command_name = nil) ⇒ Object

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, :rails, :integration

Will search for the following commands:

"rails:webrat", "webrat:integration", "webrat"

Notice that “rails:commands:webrat” could be loaded as well, what Rails looks for is the first and last parts of the namespace.


71
72
73
74
75
76
77
78
79
80
# File 'railties/lib/rails/command.rb', line 71

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_commandsObject

:nodoc:


22
23
24
# File 'railties/lib/rails/command.rb', line 22

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

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

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


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'railties/lib/rails/command.rb', line 31

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

  if char = namespace =~ /:(\w+)$/
    command_name, namespace = $1, namespace.slice(0, char)
  else
    command_name = namespace
  end

  command_name, namespace = "help", "help" if command_name.blank? || HELP_MAPPINGS.include?(command_name)
  command_name, namespace = "version", "version" if %w( -v --version ).include?(command_name)

  # isolate ARGV to ensure that commands depend only on the args they are given
  args = args.dup # args might *be* ARGV so dup before clearing
  old_argv = ARGV.dup
  ARGV.clear

  command = find_by_namespace(namespace, command_name)
  if command && command.all_commands[command_name]
    command.perform(command_name, args, config)
  else
    find_by_namespace("rake").perform(full_namespace, args, config)
  end
ensure
  ARGV.replace(old_argv)
end

:nodoc:


91
92
93
# File 'railties/lib/rails/command.rb', line 91

def print_commands # :nodoc:
  commands.each { |command| puts("  #{command}") }
end

.rootObject

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


83
84
85
86
87
88
89
# File 'railties/lib/rails/command.rb', line 83

def root
  if defined?(ENGINE_ROOT)
    Pathname.new(ENGINE_ROOT)
  elsif defined?(APP_PATH)
    Pathname.new(File.expand_path("../..", APP_PATH))
  end
end