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

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], true)
  
  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(<<-XML),nil,'-').result(binding)
        <profile>
        <% attrs.each do |attr| -%>
          <<%= attr %>><%= r.send(attr) %></<%= attr %>>
        <% end -%>
        </profile>
      XML
    else
      puts r.send(attr)
    end
    
  else
    runner.send(self.arguments[:command], self.arguments[:params])
  end
end