Class: Rails::Command::Base

Inherits:
Thor
  • Object
show all
Includes:
Actions
Defined in:
railties/lib/rails/command/base.rb

Defined Under Namespace

Classes: Error

Class Method Summary collapse

Methods included from Actions

#boot_application!, #load_environment_config!, #load_generators, #load_tasks, #require_application!, #set_application_directory!

Class Method Details



87
88
89
90
91
92
93
94
95
96
# File 'railties/lib/rails/command/base.rb', line 87

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

.base_nameObject

Sets the base_name taking into account the current class namespace.

Rails::Command::TestCommand.base_name # => 'rails'


107
108
109
110
111
# File 'railties/lib/rails/command/base.rb', line 107

def base_name
  @base_name ||= if base = name.to_s.split("::").first
    base.underscore
  end
end

.class_usageObject

:nodoc:



123
124
125
126
127
# File 'railties/lib/rails/command/base.rb', line 123

def class_usage # :nodoc:
  if usage_path
    @class_usage ||= ERB.new(File.read(usage_path), trim_mode: "-").result(binding)
  end
end

.command_nameObject

Return command name without namespaces.

Rails::Command::TestCommand.command_name # => 'test'


116
117
118
119
120
121
# File 'railties/lib/rails/command/base.rb', line 116

def command_name
  @command_name ||= if command = name.to_s.split("::").last
    command.chomp!("Command")
    command.underscore
  end
end

.default_command_rootObject

Default file root to place extra files a command might need, placed one folder above the command file.

For a Rails::Command::TestCommand placed in rails/command/test_command.rb would return rails/test.



140
141
142
143
# File 'railties/lib/rails/command/base.rb', line 140

def default_command_root
  @default_command_root = resolve_path(".") unless defined?(@default_command_root)
  @default_command_root
end

.desc(usage = nil, description = nil, options = {}) ⇒ Object

Tries to get the description from a USAGE file one folder above the command root.



35
36
37
38
39
40
41
# File 'railties/lib/rails/command/base.rb', line 35

def desc(usage = nil, description = nil, options = {})
  if usage
    super
  else
    class_usage
  end
end

.engine?Boolean

Returns true when the app is a Rails engine.

Returns:

  • (Boolean)


29
30
31
# File 'railties/lib/rails/command/base.rb', line 29

def engine?
  defined?(ENGINE_ROOT)
end

.executable(command_name = self.command_name) ⇒ Object



83
84
85
# File 'railties/lib/rails/command/base.rb', line 83

def executable(command_name = self.command_name)
  "#{bin} #{namespaced_name(command_name)}"
end

.exit_on_failure?Boolean

:nodoc:

Returns:

  • (Boolean)


24
25
26
# File 'railties/lib/rails/command/base.rb', line 24

def exit_on_failure? # :nodoc:
  false
end

.help(shell) ⇒ Object

Override Thor’s class-level help to also show the USAGE.



99
100
101
102
# File 'railties/lib/rails/command/base.rb', line 99

def help(shell, *) # :nodoc:
  super
  shell.say class_usage if class_usage
end

.hide_command!Object

Convenience method to hide this command from the available ones when running rails command.



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

def hide_command!
  Rails::Command.hidden_commands << self
end

.inherited(base) ⇒ Object

:nodoc:



60
61
62
63
64
65
66
# File 'railties/lib/rails/command/base.rb', line 60

def inherited(base) # :nodoc:
  super

  if base.name && !base.name.end_with?("Base")
    Rails::Command.subclasses << base
  end
end

.namespace(name = nil) ⇒ Object

Convenience method to get the namespace from the class name. It’s the same as Thor default except that the Command at the end of the class is removed.



46
47
48
49
50
51
52
# File 'railties/lib/rails/command/base.rb', line 46

def namespace(name = nil)
  if name
    super
  else
    @namespace ||= super.chomp("_command").sub(/:command:/, ":")
  end
end

.perform(command, args, config) ⇒ Object

:nodoc:



68
69
70
71
72
73
74
75
# File 'railties/lib/rails/command/base.rb', line 68

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_commandsObject



77
78
79
80
81
# File 'railties/lib/rails/command/base.rb', line 77

def printing_commands
  commands.filter_map do |name, command|
    [namespaced_name(name), command.description] unless command.hidden?
  end
end

.usage_pathObject

Path to lookup a USAGE description in a file.



130
131
132
133
# File 'railties/lib/rails/command/base.rb', line 130

def usage_path
  @usage_path = resolve_path("USAGE") unless defined?(@usage_path)
  @usage_path
end