class Rails::Command::Base
def banner(command = nil, *)
def banner(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
def base_name
Sets the base_name taking into account the current class namespace.
def base_name @base_name ||= if base = name.to_s.split("::").first base.underscore end end
def class_usage # :nodoc:
def class_usage # :nodoc: if usage_path @class_usage ||= ERB.new(File.read(usage_path), trim_mode: "-").result(binding) end end
def command_name
Return command name without namespaces.
def command_name @command_name ||= if command = name.to_s.split("::").last command.chomp!("Command") command.underscore end end
def create_command(meth)
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
def default_command_root
For a Rails::Command::TestCommand placed in rails/command/test_command.rb
one folder above the command file.
Default file root to place extra files a command might need, placed
def default_command_root @default_command_root = resolve_path(".") unless defined?(@default_command_root) @default_command_root end
def desc(usage = nil, description = nil, options = {})
Tries to get the description from a USAGE file one folder above the command
def desc(usage = nil, description = nil, options = {}) if usage super else class_usage end end
def engine?
def engine? defined?(ENGINE_ROOT) end
def executable(command_name = self.command_name)
def executable(command_name = self.command_name) "#{bin} #{namespaced_name(command_name)}" end
def exit_on_failure? # :nodoc:
def exit_on_failure? # :nodoc: false end
def help(shell, *) # :nodoc:
Override Thor's class-level help to also show the USAGE.
def help(shell, *) # :nodoc: super shell.say class_usage if class_usage end
def hide_command!
Convenience method to hide this command from the available ones when
def hide_command! Rails::Command.hidden_commands << self end
def inherited(base) # :nodoc:
def inherited(base) # :nodoc: super if base.name && !base.name.end_with?("Base") Rails::Command.subclasses << base end end
def invoke_command(command, *) # :nodoc:
def invoke_command(command, *) # :nodoc: @current_subcommand ||= nil original_subcommand, @current_subcommand = @current_subcommand, command.name super ensure @current_subcommand = original_subcommand end
def namespace(name = nil)
same as Thor default except that the Command at the end of the class
Convenience method to get the namespace from the class name. It's the
def namespace(name = nil) if name super else @namespace ||= super.chomp("_command").sub(/:command:/, ":") end end
def namespaced_name(name)
def namespaced_name(name) *prefix, basename = namespace.delete_prefix("rails:").split(":") prefix.concat([basename, name.to_s].uniq).join(":") end
def perform(command, args, config) # :nodoc:
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
def printing_commands
def printing_commands commands.filter_map do |name, command| [namespaced_name(name), command.description] unless command.hidden? end end
def resolve_path(path)
def resolve_path(path) path = File.join("../commands", *namespace.delete_prefix("rails:").split(":"), path) path = File.expand_path(path, __dir__) path if File.exist?(path) end
def usage_path
def usage_path @usage_path = resolve_path("USAGE") unless defined?(@usage_path) @usage_path end