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
# File 'lib/hub/runner.rb', line 10

def initialize(*args)
  @args = Args.new(args)
  Commands.run(@args)
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



16
17
18
# File 'lib/hub/runner.rb', line 16

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

Instance Method Details

#commandObject

A string representation of the command that would run.



21
22
23
24
25
26
27
# File 'lib/hub/runner.rb', line 21

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

#commandsObject

An array of all commands as strings.



30
31
32
33
34
35
36
37
38
39
# File 'lib/hub/runner.rb', line 30

def commands
  args.commands.map do |cmd|
    if cmd.respond_to?(:join)
      # a simplified `Shellwords.join` but it's OK since this is only used to inspect
      cmd.map { |arg| arg = arg.to_s; (arg.index(' ') || arg.empty?) ? "'#{arg}'" : arg }.join(' ')
    else
      cmd.to_s
    end
  end
end

#exec(*args) ⇒ Object

Special-case ‘echo` for Windows



69
70
71
72
73
74
75
# File 'lib/hub/runner.rb', line 69

def exec *args
  if args.first == 'echo' && Context::windows?
    puts args[1..-1].join(' ')
  else
    super
  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.



47
48
49
50
51
52
53
# File 'lib/hub/runner.rb', line 47

def execute
  if args.noop?
    puts commands
  elsif not args.skip?
    execute_command_chain args.commands
  end
end

#execute_command_chain(commands) ⇒ Object

Runs multiple commands in succession; exits at first failure.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hub/runner.rb', line 56

def execute_command_chain commands
  commands.each_with_index do |cmd, i|
    if cmd.respond_to?(:call) then cmd.call
    elsif i == commands.length - 1
      # last command in chain
      exec(*cmd)
    else
      exit($?.exitstatus) unless system(*cmd)
    end
  end
end