Module: Rails::Command
Constant Summary
-
HELP_MAPPINGS =
# File 'railties/lib/rails/command.rb', line 43%w(-h -? --help).to_set
-
VERSION_MAPPINGS =
# File 'railties/lib/rails/command.rb', line 44%w(-v --version).to_set
Class Method Summary
-
.invoke(full_namespace, args = [], **config)
Receives a namespace, arguments, and the behavior to invoke the command.
-
.root
Returns the root of the Rails engine or app running the command.
- .command_type private
- .file_lookup_paths private
- .invoke_rake(task, args, config) private
- .lookup_paths private
- .rails_new_with_no_path?(args) ⇒ Boolean private
- .split_namespace(namespace) private
- .with_argv(argv) private
- .application_root Internal use only
- .environment Internal use only
-
.find_by_namespace(namespace, command_name = nil)
Internal use only
::Rails
finds namespaces similar to Thor, it only adds one rule: - .hidden_commands Internal use only
- .printing_commands Internal use only
::ActiveSupport::Autoload
- Extended
Class Method Details
.application_root
This method is for internal use only.
[ GitHub ]
.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"
# 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
.invoke(full_namespace, args = [], **config)
Receives a namespace, arguments, and the behavior to invoke the command.
# 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. 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)
# 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.
# 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