Class: Rails::Command::Base

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

Defined Under Namespace

Classes: CorrectableError, 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.


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

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'

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

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

.command_nameObject

Return command name without namespaces.

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

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

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.


134
135
136
137
# File 'railties/lib/rails/command/base.rb', line 134

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.


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

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

.engine?Boolean

Returns true when the app is a Rails engine.

Returns:

  • (Boolean)

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

def engine?
  defined?(ENGINE_ROOT)
end

.executableObject


93
94
95
# File 'railties/lib/rails/command/base.rb', line 93

def executable
  "rails #{command_name}"
end

.exit_on_failure?Boolean

:nodoc:

Returns:

  • (Boolean)

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

def exit_on_failure? # :nodoc:
  false
end

.hide_command!Object

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


69
70
71
# File 'railties/lib/rails/command/base.rb', line 69

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

.inherited(base) ⇒ Object

:nodoc:


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

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.


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

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

.perform(command, args, config) ⇒ Object

:nodoc:


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

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


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

def printing_commands
  namespaced_commands
end

.usage_pathObject

Path to lookup a USAGE description in a file.


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

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


173
174
175
176
177
178
179
# File 'railties/lib/rails/command/base.rb', line 173

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