Class: GGem::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/ggem/cli.rb,
lib/ggem/cli/clirb.rb,
lib/ggem/cli/commands.rb

Defined Under Namespace

Modules: GemspecCommand, GitRepoCommand, NotifyCmdCommand, ValidCommand Classes: BuildCommand, CLIRB, CommandSet, GenerateCommand, InstallCommand, InvalidCommand, PushCommand, ReleaseCommand, TagCommand

Constant Summary collapse

COMMANDS =
CommandSet.new{ |unknown| InvalidCommand.new(unknown) }.tap do |c|
  c.add(GenerateCommand, 'generate', 'g')
  c.add(BuildCommand,    'build',    'b')
  c.add(InstallCommand,  'install',  'i')
  c.add(PushCommand,     'push',     'p')
  c.add(TagCommand,      'tag',      't')
  c.add(ReleaseCommand,  'release',  'r')
end
InvalidCommandError =
Class.new(ArgumentError)
CommandExitError =
Class.new(RuntimeError)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kernel = nil, stdout = nil, stderr = nil) ⇒ CLI

Returns a new instance of CLI.



22
23
24
25
26
# File 'lib/ggem/cli.rb', line 22

def initialize(kernel = nil, stdout = nil, stderr = nil)
  @kernel = kernel || Kernel
  @stdout = stdout || $stdout
  @stderr = stderr || $stderr
end

Class Method Details

.run(args) ⇒ Object



18
19
20
# File 'lib/ggem/cli.rb', line 18

def self.run(args)
  self.new.run(args)
end

Instance Method Details

#run(args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ggem/cli.rb', line 28

def run(args)
  begin
    cmd_name = args.shift
    cmd = COMMANDS[cmd_name]
    cmd.run(args)
  rescue CLIRB::HelpExit
    @stdout.puts cmd.help
  rescue CLIRB::VersionExit
    @stdout.puts GGem::VERSION
  rescue CLIRB::Error, ArgumentError, InvalidCommandError => exception
    display_debug(exception)
    @stderr.puts "#{exception.message}\n\n"
    @stdout.puts cmd.help
    @kernel.exit 1
  rescue CommandExitError
    @kernel.exit 1
  rescue StandardError => exception
    @stderr.puts "#{exception.class}: #{exception.message}"
    @stderr.puts exception.backtrace.join("\n")
    @kernel.exit 1
  end
  @kernel.exit 0
end