Class: Rails::Command::Base
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
AboutCommand, ApplicationCommand, BootCommand, ConsoleCommand, CredentialsCommand, DbconsoleCommand, DestroyCommand, DevCommand, DevcontainerCommand, EncryptedCommand, GemHelpCommand, GenerateCommand, HelpCommand, InitializersCommand, MiddlewareCommand, NewCommand, NotesCommand, PluginCommand, RakeCommand, RestartCommand, RoutesCommand, RunnerCommand, SecretCommand, ServerCommand, StatsCommand, TestCommand, UnusedRoutesCommand, VersionCommand, App::UpdateCommand, Db::System::ChangeCommand
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Thor
|
|
Instance Chain:
self,
Actions ,
Thor
|
|
Inherits: |
Thor
|
Defined in: | railties/lib/rails/command/base.rb |
Class Attribute Summary
- .bin rw
- .bin? ⇒ Boolean rw
-
.engine? ⇒ Boolean
readonly
Returns true when the app is a Rails engine.
- .exit_on_failure? ⇒ Boolean readonly Internal use only
Class Method Summary
- .banner(command = nil)
-
.base_name
Sets the base_name taking into account the current class namespace.
-
.command_name
Return command name without namespaces.
-
.default_command_root
Default file root to place extra files a command might need, placed one folder above the command file.
-
.desc(usage = nil, description = nil, options = {})
Tries to get the description from a USAGE file one folder above the command root.
- .executable(command_name = self.command_name)
-
.hide_command!
Convenience method to hide this command from the available ones when running rails command.
-
.namespace(name = nil)
Convenience method to get the namespace from the class name.
- .printing_commands
-
.usage_path
Path to lookup a USAGE description in a file.
-
.create_command(meth)
private
Allow the command method to be called perform.
- .namespaced_name(name) private
- .resolve_path(path) private
- .class_usage Internal use only
-
.help(shell)
Internal use only
Override Thor’s class-level help to also show the USAGE.
- .inherited(base) Internal use only
- .perform(command, args, config) Internal use only
Instance Method Summary
Actions
- Included
#boot_application!, #load_environment_config!, | |
#load_generators | See additional method definition at line 36. |
#load_tasks | See additional method definition at line 31. |
#require_application!, | |
#set_application_directory! | Change to the application’s path if there is no |
Class Attribute Details
.bin (rw)
[ GitHub ]# File 'railties/lib/rails/command/base.rb', line 20
class_attribute :bin, instance_accessor: false, default: "bin/rails"
.bin? ⇒ Boolean
(rw)
[ GitHub ]
# File 'railties/lib/rails/command/base.rb', line 20
class_attribute :bin, instance_accessor: false, default: "bin/rails"
.engine? ⇒ Boolean
(readonly)
Returns true when the app is a Rails engine.
# File 'railties/lib/rails/command/base.rb', line 28
def engine? defined?(ENGINE_ROOT) end
.exit_on_failure? ⇒ Boolean
(readonly)
# File 'railties/lib/rails/command/base.rb', line 23
def exit_on_failure? # :nodoc: false end
Class Method Details
.banner(command = nil)
[ GitHub ]# File 'railties/lib/rails/command/base.rb', line 86
def (command = nil, *) if command # Similar to Thor's banner, but show the namespace (minus the # "rails:" prefix), and show the command's declared bin instead of # the command runner. command.formatted_usage(self).gsub(/^#{namespace}:(\w+)/) { executable($1) } else executable end end
.base_name
Sets the base_name taking into account the current class namespace.
Rails::Command::TestCommand.base_name # => 'rails'
# File 'railties/lib/rails/command/base.rb', line 106
def base_name @base_name ||= if base = name.to_s.split("::").first base.underscore end end
.class_usage
# File 'railties/lib/rails/command/base.rb', line 122
def class_usage # :nodoc: if usage_path @class_usage ||= ERB.new(File.read(usage_path), trim_mode: "-").result(binding) end end
.command_name
Return command name without namespaces.
Rails::Command::TestCommand.command_name # => 'test'
# File 'railties/lib/rails/command/base.rb', line 115
def command_name @command_name ||= if command = name.to_s.split("::").last command.chomp!("Command") command.underscore end end
.create_command(meth) (private)
Allow the command method to be called perform.
# File 'railties/lib/rails/command/base.rb', line 146
def create_command(meth) if meth == "perform" alias_method command_name, meth else # Prevent exception about command without usage. # Some commands define their documentation differently. @usage ||= meth @desc ||= "" super end end
.default_command_root
Default file root to place extra files a command might need, placed one folder above the command file.
For a TestCommand
placed in rails/command/test_command.rb
would return rails/test
.
# File 'railties/lib/rails/command/base.rb', line 139
def default_command_root @default_command_root = resolve_path(".") unless defined?(@default_command_root) @default_command_root end
.desc(usage = nil, description = nil, options = {})
Tries to get the description from a USAGE file one folder above the command root.
# File 'railties/lib/rails/command/base.rb', line 34
def desc(usage = nil, description = nil, = {}) if usage super else class_usage end end
.executable(command_name = self.command_name)
[ GitHub ]# File 'railties/lib/rails/command/base.rb', line 82
def executable(command_name = self.command_name) "#{bin} #{namespaced_name(command_name)}" end
.help(shell)
Override Thor’s class-level help to also show the USAGE.
# File 'railties/lib/rails/command/base.rb', line 98
def help(shell, *) # :nodoc: super shell.say class_usage if class_usage end
.hide_command!
Convenience method to hide this command from the available ones when running rails command.
# File 'railties/lib/rails/command/base.rb', line 55
def hide_command! Rails::Command.hidden_commands << self end
.inherited(base)
.namespace(name = nil)
Convenience method to get the namespace from the class name. It’s the same as Thor default except that the ::Rails::Command
at the end of the class is removed.
# File 'railties/lib/rails/command/base.rb', line 45
def namespace(name = nil) if name super else @namespace ||= super.chomp("_command").sub(/:command:/, ":") end end
.namespaced_name(name) (private)
[ GitHub ]# File 'railties/lib/rails/command/base.rb', line 159
def namespaced_name(name) *prefix, basename = namespace.delete_prefix("rails:").split(":") prefix.concat([basename, name.to_s].uniq).join(":") end
.perform(command, args, config)
# File 'railties/lib/rails/command/base.rb', line 67
def perform(command, args, config) # :nodoc: if Rails::Command::HELP_MAPPINGS.include?(args.first) command, args = "help", [command] args.clear if instance_method(:help).arity.zero? end dispatch(command, args.dup, nil, config) end
.printing_commands
[ GitHub ]# File 'railties/lib/rails/command/base.rb', line 76
def printing_commands commands.filter_map do |name, command| [namespaced_name(name), command.description] unless command.hidden? end end
.resolve_path(path) (private)
[ GitHub ].usage_path
Path to lookup a USAGE description in a file.
# File 'railties/lib/rails/command/base.rb', line 129
def usage_path @usage_path = resolve_path("USAGE") unless defined?(@usage_path) @usage_path end