Class: Gel::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/gel/command.rb

Direct Known Subclasses

Config, Env, Exec, Help, Install, InstallGem, Lock, Ruby, ShellSetup, Stub, Update

Defined Under Namespace

Classes: Config, Env, Exec, Help, Install, InstallGem, Lock, Ruby, ShellSetup, Stub, Update

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#reraiseObject

If set to true, an error raised from #run will pass straight up to ruby instead of being treated as an internal Gel error



73
74
75
# File 'lib/gel/command.rb', line 73

def reraise
  @reraise
end

Class Method Details

.extract_word(arguments) ⇒ Object



65
66
67
68
69
# File 'lib/gel/command.rb', line 65

def self.extract_word(arguments)
  if idx = arguments.index { |w| w =~ /^[^-]/ }
    arguments.delete_at(idx)
  end
end

.handle_error(ex) ⇒ Object



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
# File 'lib/gel/command.rb', line 31

def self.handle_error(ex)
  case ex
  when Gel::ReportableError
    $stderr.puts "ERROR: #{ex.message}"
    if more = ex.details
      $stderr.puts more
    end

    exit ex.exit_code
  when Interrupt
    # Re-signal so our parent knows why we died
    Signal.trap(ex.signo, "SYSTEM_DEFAULT")
    Process.kill(ex.signo, Process.pid)

    # Shouldn't be reached
    raise ex
  when SystemExit, SignalException
    raise ex
  when StandardError, ScriptError, NoMemoryError, SystemStackError
    # We want basically everything here: we definitely care about
    # StandardError and ScriptError... but we also assume that whatever
    # caused NoMemoryError or SystemStackError was way down the call
    # stack, so we've now unwound enough to safely handle even those.

    $stderr.print "\n\n===== Gel Internal Error =====\n\n"

    # We'll improve this later, but for now after the header we'll leave
    # ruby to write the message & backtrace:
    raise ex
  else
    raise ex
  end
end

.run(command_line) ⇒ Object



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

def self.run(command_line)
  command_line = command_line.dup
  if command_name = extract_word(command_line)
    const = command_name.downcase.sub(/^./, &:upcase).gsub(/[-_]./) { |s| s[1].upcase }
    if Gel::Command.const_defined?(const, false)
      command = Gel::Command.const_get(const, false).new
      command.run(command_line)
    elsif Gel::Environment.activate_for_executable(["gel-#{command_name}", command_name])
      command_name = "gel-#{command_name}" if Gel::Environment.find_executable("gel-#{command_name}")
      command = Gel::Command::Exec.new
      command.run([command_name, *command_line])
    else
      raise Gel::Error::UnknownCommandError.new(command_name: command_name)
    end
  else
    puts <<~EOF
    Gel doesn't have a default command; please run `gel install`
    EOF
  end
rescue Exception => ex
  raise if $DEBUG || (command && command.reraise)
  handle_error(ex)
end