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

Instance Method Summary collapse

Methods included from Actions

#load_generators, #load_tasks, #require_application!, #require_application_and_environment!, #require_environment!, #set_application_directory!

Class Method Details

Use Rails’ default banner.



81
82
83
# File 'railties/lib/rails/command/base.rb', line 81

def banner(*)
  "#{executable} #{arguments.map(&:usage).join(' ')} [options]".squish
end

.base_nameObject

Sets the base_name taking into account the current class namespace.

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


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

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

.command_nameObject

Return command name without namespaces.

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


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

def command_name
  @command_name ||= begin
    if command = name.to_s.split("::").last
      command.chomp!("Command")
      command.underscore
    end
  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.



121
122
123
124
# File 'railties/lib/rails/command/base.rb', line 121

def default_command_root
  path = File.expand_path(relative_command_path, __dir__)
  path if File.exist?(path)
end

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

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



31
32
33
34
35
36
37
# File 'railties/lib/rails/command/base.rb', line 31

def desc(usage = nil, description = nil, options = {})
  if usage
    super
  else
    @desc ||= ERB.new(File.read(usage_path)).result(binding) if usage_path
  end
end

.engine?Boolean

Returns true when the app is a Rails engine.

Returns:

  • (Boolean)


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

def engine?
  defined?(ENGINE_ROOT)
end

.executableObject



76
77
78
# File 'railties/lib/rails/command/base.rb', line 76

def executable
  "rails #{command_name}"
end

.exit_on_failure?Boolean

:nodoc:

Returns:

  • (Boolean)


20
21
22
# File 'railties/lib/rails/command/base.rb', line 20

def exit_on_failure? # :nodoc:
  false
end

.hide_command!Object

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



52
53
54
# File 'railties/lib/rails/command/base.rb', line 52

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

.inherited(base) ⇒ Object

:nodoc:



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

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.



42
43
44
45
46
47
48
# File 'railties/lib/rails/command/base.rb', line 42

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

.perform(command, args, config) ⇒ Object

:nodoc:



64
65
66
67
68
69
70
# File 'railties/lib/rails/command/base.rb', line 64

def perform(command, args, config) # :nodoc:
  if Rails::Command::HELP_MAPPINGS.include?(args.first)
    command, args = "help", []
  end

  dispatch(command, args.dup, nil, config)
end

.printing_commandsObject



72
73
74
# File 'railties/lib/rails/command/base.rb', line 72

def printing_commands
  namespaced_commands
end

.usage_pathObject

Path to lookup a USAGE description in a file.



109
110
111
112
113
114
# File 'railties/lib/rails/command/base.rb', line 109

def usage_path
  if default_command_root
    path = File.join(default_command_root, "USAGE")
    path if File.exist?(path)
  end
end

Instance Method Details

#helpObject



160
161
162
163
164
165
166
# File 'railties/lib/rails/command/base.rb', line 160

def help
  if command_name = self.class.command_name
    self.class.command_help(shell, command_name)
  else
    super
  end
end