Class: Hub::Runner
- Inherits:
-
Object
- Object
- Hub::Runner
- 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
-
#args ⇒ Object
readonly
Returns the value of attribute args.
Class Method Summary collapse
-
.execute(*args) ⇒ Object
Shortcut.
Instance Method Summary collapse
-
#command ⇒ Object
A string representation of the command that would run.
-
#commands ⇒ Object
An array of all commands as strings.
-
#exec(*args) ⇒ Object
Special-case ‘echo` for Windows.
-
#execute ⇒ Object
Runs the target git command with an optional callback.
-
#execute_command_chain(commands) ⇒ Object
Runs multiple commands in succession; exits at first failure.
-
#initialize(*args) ⇒ Runner
constructor
A new instance of Runner.
Constructor Details
Instance Attribute Details
#args ⇒ Object (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
#command ⇒ Object
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 |
#commands ⇒ Object
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
70 71 72 73 74 75 76 |
# File 'lib/hub/runner.rb', line 70 def exec *args if args.first == 'echo' && Context::windows? puts args[1..-1].join(' ') else super end end |
#execute ⇒ Object
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 67 |
# 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 STDOUT.flush; STDERR.flush exec(*cmd) else exit($?.exitstatus) unless system(*cmd) end end end |