Class: Quails::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_and_environment!, #set_application_directory!

Class Method Details

Use Quails’ default banner.



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

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

.base_nameObject

Sets the base_name taking into account the current class namespace.

Quails::Command::TestCommand.base_name # => 'quails'


84
85
86
87
88
89
90
# File 'railties/lib/rails/command/base.rb', line 84

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.

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


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

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 Quails::Command::TestCommand placed in quails/command/test_command.rb would return quails/test.



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

def default_command_root
  path = File.expand_path(File.join("../commands", command_root_namespace), __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.



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

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 Quails engine.

Returns:

  • (Boolean)


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

def engine?
  defined?(ENGINE_ROOT)
end

.executableObject



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

def executable
  "bin/quails #{command_name}"
end

.hide_command!Object

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



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

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

.inherited(base) ⇒ Object

:nodoc:



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

def inherited(base) #:nodoc:
  super

  if base.name && base.name !~ /Base$/
    Quails::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.



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

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

.perform(command, args, config) ⇒ Object

:nodoc:



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

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

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

.printing_commandsObject



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

def printing_commands
  namespaced_commands
end

.usage_pathObject

Path to lookup a USAGE description in a file.



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

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



148
149
150
151
152
153
154
# File 'railties/lib/rails/command/base.rb', line 148

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