Class: Firebrew::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/firebrew/command_line.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ CommandLine

Returns a new instance of CommandLine.



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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/firebrew/command_line.rb', line 30

def initialize(args=[])
  @arguments = {
    command: nil,
    params: {},
    config: {}
  }
  global_parser = self.option_parser do |parser|
    parser.banner = self.desc("      Usage: firebrew [--help] [--version]\n             [--base-dir=<path>] [--profile=<name>] [--firefox=<path>]\n             <command> [<args>]\n    DESC\n    \n    parser.separator ''\n    parser.separator 'commands:'\n    begin\n      pos = 11\n      \n      self.summary(parser, :install, <<-DESC, pos)\n        Install the Firefox extension\n      DESC\n      \n      self.summary(parser, :uninstall, <<-DESC, pos)\n        Uninstall the Firefox extension\n      DESC\n      \n      self.summary(parser, :info, <<-DESC, pos)\n        Show detail information of the Firefox extension\n      DESC\n      \n      self.summary(parser, :search, <<-DESC, pos)\n        Search Firefox extensions\n      DESC\n      \n      self.summary(parser, :list, <<-DESC, pos)\n        Enumerate the installed Firefox extensions\n      DESC\n      \n      self.summary(parser, :profile, <<-DESC, pos)\n        Show the profile information\n      DESC\n    end\n  end\n  \n  global_parser.order!(args)\n  command = args.shift.to_s.intern\n  \n  case command\n  when :install, :uninstall, :info then\n    subcommand_parser = self.option_parser do |parser|\n      parser.banner = self.desc(<<-DESC)\n        Usage: firebrew [--help] [--version]\n               [--base-dir=<path>] [--profile=<name>] [--firefox=<path>]\n               \#{command} <extension-name>\n      DESC\n    end\n    \n    subcommand_parser.permute!(args)\n    self.arguments[:command] = command\n    self.arguments[:params][:term] = args[0]\n    \n  when :search then\n    subcommand_parser = self.option_parser do |parser|\n      parser.banner = self.desc(<<-DESC)\n        Usage: firebrew [--help] [--version]\n               [--base-dir=<path>] [--profile=<name>] [--firefox=<path>]\n               \#{command} <term>\n      DESC\n    end\n    \n    subcommand_parser.permute!(args)\n    self.arguments[:command] = command\n    self.arguments[:params][:term] = args[0]\n    \n  when :list then\n    subcommand_parser = self.option_parser do |parser|\n      parser.banner = self.desc(<<-DESC)\n        Usage: firebrew [--help] [--version]\n               [--base-dir=<path>] [--profile=<name>] [--firefox=<path>]\n               \#{command}\n      DESC\n    end\n    \n    subcommand_parser.permute!(args)\n    self.arguments[:command] = command\n    \n  when :profile then\n    subcommand_parser = self.option_parser do |parser|\n      parser.summary_width = 30\n      parser.banner = self.desc(<<-DESC)\n        Usage: firebrew [--help] [--version]\n               [--base-dir=<path>] [--profile=<name>] [--firefox=<path>]\n               \#{command} [--attribute=<attr-name>]\n      DESC\n      \n      parser.separator ''\n      parser.separator 'options:'\n      begin\n        chooses = %r[\#{Firebrew::Firefox::Profile.attributes.join('|')}]\n        parser.on('-a <attr-name>','--attribute=<attr-name>', chooses, self.desc(<<-DESC)) do |v|\n          The name of the attribute which want to display\n        DESC\n          self.arguments[:params][:attribute] = v\n        end\n      end\n    end\n    \n    subcommand_parser.permute!(args)\n    self.arguments[:command] = command\n    \n  when :'' then\n    global_parser.permute(['--help'])\n    \n  else\n    raise Firebrew::CommandLineError, \"Invalid command: \#{command}\"\n  end\n  \nrescue OptionParser::ParseError => e\n  m = e.message\n  m[0] = m[0].upcase\n  raise Firebrew::CommandLineError, m\nend\n")

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



7
8
9
# File 'lib/firebrew/command_line.rb', line 7

def arguments
  @arguments
end

Class Method Details

.executeObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/firebrew/command_line.rb', line 9

def self.execute
  begin
    if block_given? then
      yield
    else
      self.new(ARGV).execute
    end
  rescue Firebrew::Error => e
    $stderr.puts e.message
    exit e.status
  rescue SystemExit => e
    exit 0
  rescue Exception => e
    $stderr.puts e.inspect
    $stderr.puts e.backtrace
    exit 1
  else
    exit 0
  end
end

Instance Method Details

#executeObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/firebrew/command_line.rb', line 153

def execute
  runner = Runner.new(self.arguments[:config])
  
  case self.arguments[:command]
  when :search, :list then
    results = runner.send(self.arguments[:command], self.arguments[:params])
    results.each do |result|
      puts result.name
    end
    
  when :info then
    result = runner.send(self.arguments[:command], self.arguments[:params])
    puts result.data
    
  when :profile then
    r = runner.profile
    attr = self.arguments[:params][:attribute]
    if attr.nil? then
      attrs = r.class.attributes
      puts ERB.new(self.desc("        <profile>\n        <% attrs.each do |attr| -%>\n          <<%= attr %>><%= r.send(attr) %></<%= attr %>>\n        <% end -%>\n        </profile>\n      XML\n    else\n      puts r.send(attr)\n    end\n    \n  else\n    runner.send(self.arguments[:command], self.arguments[:params])\n  end\nend\n"),nil,'-').result(binding)