lib/repertoire/compat.rb in repertoire-0.1.0 vs lib/repertoire/compat.rb in repertoire-0.1.1

- old
+ new

@@ -1,5 +1,8 @@ +require 'repertoire/exceptions/program_not_found' +require 'repertoire/exceptions/command_failed' + module Repertoire module Compat # # Returns the native _platform_. # @@ -40,8 +43,34 @@ return full_path if File.file?(full_path) end return nil + end + + # + # Runs the command specified by _program_ and the given _args_. + # If _program_ cannot be found on the system, a ProgramNotFound + # exception will be raised. + # + # Compat.sh('ls','-la','/') + # + def Compat.sh(program,*args) + program = program.to_s + + program_path = Compat.find_program(program) + unless program_path + raise(ProgramNotFound,"the program #{program.dump} was not found",caller) + end + + # stringify the args + args = args.map { |arg| arg.to_s } + + unless system(program_path,*args) + command = program_path + command += ' ' + args.join(' ') unless args.empty? + + raise(CommandFailed,"the command #{command.dump} failed",caller) + end end end end