Class: Exercism::Rb::CommandRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/exercism/rb/command_runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(ui: UI.new) ⇒ CommandRunner

Returns a new instance of CommandRunner.



8
9
10
# File 'lib/exercism/rb/command_runner.rb', line 8

def initialize(ui: UI.new)
  @ui = ui
end

Instance Method Details

#run(*args, chdir: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/exercism/rb/command_runner.rb', line 12

def run(*args, chdir: nil)
  printable = Shellwords.join(args)
  printable = "cd #{Shellwords.escape(chdir)} && #{printable}" if chdir
  @ui.command("$ #{printable}")

  ok = if chdir
    Dir.chdir(chdir) { system(*args) }
  else
    system(*args)
  end

  case ok
  when true
    true
  when nil
    raise Error, "Command not found: #{args.first}. Install it or ensure it is on PATH."
  else
    raise Error, "Command failed: #{Shellwords.join(args)}"
  end

  true
end