Class: Nimbu::Command::Base

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/nimbu/command/base.rb

Direct Known Subclasses

Auth, Browse, Help, Init, Server, Sites, Themes, Version

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#action, #ask, #confirm, #confirm_billing, #confirm_command, #create_git_remote, #deprecate, disable_error_capture, #display, #display_header, #display_object, #display_row, #display_table, enable_error_capture, #error, error_with_failure, error_with_failure=, extended, extended_into, #fail, #format_bytes, #format_date, #format_error, #format_with_bang, #get_terminal_environment, #git, #has_git?, #home_directory, #hprint, #hputs, included, included_into, #json_decode, #json_encode, #launchy, #line_formatter, #longest, #output, #output_with_arrow, #output_with_bang, #quantify, #redisplay, #retry_on_exception, #run_command, #running_on_a_mac?, #running_on_windows?, #set_buffer, #shell, #spinner, #status, #string_distance, #styled_array, #styled_error, #styled_hash, #styled_header, #suggestion, #time_ago, #truncate, #with_tty

Methods included from Helpers::System

#browser_launcher, #command?, #osx?, #tmp_dir, #which, #windows?

Constructor Details

#initialize(args = [], options = {}) ⇒ Base

Returns a new instance of Base.



16
17
18
19
# File 'lib/nimbu/command/base.rb', line 16

def initialize(args=[], options={})
  @args = args
  @options = options
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



13
14
15
# File 'lib/nimbu/command/base.rb', line 13

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/nimbu/command/base.rb', line 14

def options
  @options
end

Class Method Details

.alias_command(new, old) ⇒ Object



61
62
63
64
# File 'lib/nimbu/command/base.rb', line 61

def self.alias_command(new, old)
  raise "no such command: #{old}" unless Nimbu::Command.commands[old]
  Nimbu::Command.command_aliases[new] = old
end

.extract_banner(help) ⇒ Object



97
98
99
# File 'lib/nimbu/command/base.rb', line 97

def self.extract_banner(help)
  help.first
end

.extract_description(help) ⇒ Object



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

def self.extract_description(help)
  help.reject do |line|
    line =~ /^\s+-(.+)#(.+)/
  end.join("\n")
end

.extract_help(file, line_number) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/nimbu/command/base.rb', line 79

def self.extract_help(file, line_number)
  buffer = []
  lines = Nimbu::Command.files[file]

  (line_number.to_i-2).downto(0) do |i|
    line = lines[i]
    case line[0..0]
      when ""
      when "#"
        buffer.unshift(line[1..-1])
      else
        break
    end
  end

  buffer
end

.extract_help_from_caller(line) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/nimbu/command/base.rb', line 71

def self.extract_help_from_caller(line)
  # pull out of the caller the information for the file path and line number
  if line =~ /^(.+?):(\d+)/
    return extract_help($1, $2)
  end
  raise "unable to extract help from caller: #{line}"
end

.extract_options(help) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/nimbu/command/base.rb', line 111

def self.extract_options(help)
  help.select do |line|
    line =~ /^\s+-(.+)#(.+)/
  end.inject([]) do |options, line|
    args = line.split('#', 2).first
    args = args.split(/,\s*/).map {|arg| arg.strip}.sort.reverse
    name = args.last.split(' ', 2).first[2..-1]
    options << { :name => name, :args => args }
  end
end

.extract_summary(help) ⇒ Object



101
102
103
# File 'lib/nimbu/command/base.rb', line 101

def self.extract_summary(help)
  extract_description(help).split("\n")[2].to_s.split("\n").first
end

.inherited(klass) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/nimbu/command/base.rb', line 27

def self.inherited(klass)
  unless klass == Nimbu::Command::Base
    help = extract_help_from_caller(caller.first)

    Nimbu::Command.register_namespace(
      :name => klass.namespace,
      :description => help.first
    )
  end
end

.method_added(method) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/nimbu/command/base.rb', line 38

def self.method_added(method)
  return if self == Nimbu::Command::Base
  return if private_method_defined?(method)
  return if protected_method_defined?(method)

  help = extract_help_from_caller(caller.first)
  resolved_method = (method.to_s == "index") ? nil : method.to_s
  command = [ self.namespace, resolved_method ].compact.join(":")
  banner = extract_banner(help) || command

  Nimbu::Command.register_command(
    :klass       => self,
    :method      => method,
    :namespace   => self.namespace,
    :command     => command,
    :banner      => banner.strip,
    :help        => help.join("\n"),
    :summary     => extract_summary(help),
    :description => extract_description(help),
    :options     => extract_options(help)
  )
end

.namespaceObject



9
10
11
# File 'lib/nimbu/command/base.rb', line 9

def self.namespace
  self.to_s.split("::").last.downcase
end

Instance Method Details

#nimbuObject



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

def nimbu
  Nimbu::Auth.client
end