Class: Hub::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/hub/runner.rb

Overview

The Hub runner expects to be initialized with ‘ARGV` and primarily exists to run a git command.

The actual functionality, that is, the code it runs when it needs to augment a git command, is kept in the ‘Hub::Commands` module.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Runner

Returns a new instance of Runner.



10
11
12
13
14
15
16
17
18
19
# File 'lib/hub/runner.rb', line 10

def initialize(*args)
  @args = Args.new(args)

  # Hack to emulate git-style
  @args.unshift 'help' if @args.grep(/^[^-]|version/).empty?

  # git commands can have dashes
  cmd = @args[0].sub(/(\w)-/, '\1_')
  Commands.send(cmd, @args) if Commands.respond_to?(cmd)
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



8
9
10
# File 'lib/hub/runner.rb', line 8

def args
  @args
end

Class Method Details

.execute(*args) ⇒ Object

Shortcut



22
23
24
# File 'lib/hub/runner.rb', line 22

def self.execute(*args)
  new(*args).execute
end

Instance Method Details

#afterObject

Returns the current after callback, which (if set) is run after the target git command.

See the ‘Hub::Args` class for more information on the `after` callback.



31
32
33
# File 'lib/hub/runner.rb', line 31

def after
  args.after.to_s
end

#commandObject

A string representation of the git command we would run if #execute were called.



37
38
39
40
41
42
43
# File 'lib/hub/runner.rb', line 37

def command
  if args.skip?
    ''
  else
    args.to_exec.join(' ')
  end
end

#executeObject

Runs the target git command with an optional callback. Replaces the current process.

If ‘args` is empty, this will skip calling the git command. This allows commands to print an error message and cancel their own execution if they don’t make sense.



51
52
53
54
55
56
57
58
59
# File 'lib/hub/runner.rb', line 51

def execute
  unless args.skip?
    if args.after?
      execute_with_after_callback
    else
      exec(*args.to_exec)
    end
  end
end

#execute_with_after_callbackObject

Runs the target git command then executes the ‘after` callback.

See the ‘Hub::Args` class for more information on the `after` callback.



65
66
67
68
69
70
71
72
73
# File 'lib/hub/runner.rb', line 65

def execute_with_after_callback
  after = args.after
  if system(*args.to_exec)
    after.respond_to?(:call) ? after.call : exec(after)
    exit
  else
    exit 1
  end
end