require 'repertoire/exceptions/program_not_found' require 'repertoire/exceptions/command_failed' module Repertoire module Compat # # @return [String] # The native platform. # # @example # Compat.arch #=> "linux" # def self.platform RUBY_PLATFORM.split('-').last end # # @return [Array] # +PATH+ environment variable. # # @example # Compat.paths # #=> ["/bin", "/usr/bin"] # def self.paths # return an empty array in case # the PATH variable does not exist return [] unless ENV['PATH'] if self.platform =~ /mswin32/ return ENV['PATH'].split(';') else return ENV['PATH'].split(':') end end # # Finds the program matching name. # # @return [String, nil] # The full path of the program, or +nil+ if the program could not be # found. # # @example # Compat.find_program('as') # #=> "/usr/bin/as" # def self.find_program(name) self.paths.each do |dir| full_path = File.expand_path(File.join(dir,name)) return full_path if File.file?(full_path) end return nil end # # Runs the command. # # @param [String] program # The name of the program to run. # # @param [Array] args # Optional arguments to run the program with. # # @raise [ProgramNotFound] # The specified _program_ could not be found. # # @raise [CommandFailed] # The command failed to run. # # @example # 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