/
base.rb
130 lines (106 loc) · 3.1 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
require 'yaml'
require 'gabba'
require 'boxcar/helpers'
class Boxcar::Command::Base
attr_reader :args
attr_reader :options
def initialize(args=[], options={})
@args = args
@options = options
analytics = YAML.load_file("#{Boxcar::App.settings.root}/analytics.yml")
@gabba = ::Gabba::Gabba.new(analytics['tracking_id'], analytics['domain'])
end
def self.namespace
self.to_s.split("::").last.downcase
end
def track_event(category, action, label)
@gabba.event(category, action, label)
end
def shift_argument
Boxcar::Command.shift_argument
end
protected
def validate_arguments!
Boxcar::Command.validate_arguments!
end
def self.alias_command(new, old)
raise "no such command: #{old}" unless Boxcar::Command.commands[old]
Boxcar::Command.command_aliases[new] = old
end
def self.inherited(klass)
unless klass == Boxcar::Command::Base
help = extract_help_from_caller(caller.first)
Boxcar::Command.register_namespace(
:name => klass.namespace,
:description => help.first
)
end
end
def self.method_added(method)
return if self == Boxcar::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
Boxcar::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
def self.extract_help_from_caller(line)
# pull out of the caller the information for the file path and line number
if line =~ /^(.+?):(\d+)/
extract_help($1, $2)
else
raise("unable to extract help from caller: #{line}")
end
end
def self.extract_help(file, line_number)
buffer = []
lines = Boxcar::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
def self.extract_banner(help)
help.first
end
def self.extract_summary(help)
extract_description(help).split("\n")[2].to_s.split("\n").first
end
def self.extract_description(help)
help.reject do |line|
line =~ /^\s+-(.+)#(.+)/
end.join("\n")
end
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
def current_command
Boxcar::Command.current_command
end
end