require 'open3' module InvocaGems::ShellCommand def self.included(klass) klass.send(:extend, InvocaGems::ShellCommand) end # Returns the command output, stripped # Or nil if command failed Response = Struct.new(:stdout, :stderr, :code) def shell_command(command:, path: '.', presenter:, raise_errors: true) presenter.set_subtask "cd #{path}; #{command}" r = Response.new(*Open3.capture3("cd #{path}; #{command}")) if r.code && r.code.success? r.stdout.strip else if raise_errors presenter.stop_with_message "cd #{path}; #{command}: returned error #{r.code.exitstatus}: #{r.stdout}\n#{r.stderr}" end end end end