Sha256: f608315ae46aa8ac60975a11a1db967cacd45fe22ab8673e825971aa174d6ac2

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

module RProgram
  module Compat
    #
    # Returns the native _platform_.
    #
    #   Compat.arch  #=> "linux"
    #
    def Compat.platform
      RUBY_PLATFORM.split('-').last
    end

    #
    # Returns an array representing the +PATH+ environment variable.
    # If the +PATH+ environment variable is not setup, an empty array will
    # be returned.
    #
    #   Compat.paths #=> ["/bin", "/usr/bin"]
    #
    def Compat.paths
      # return an empty array in case
      # the PATH variable does not exist
      return [] unless ENV['PATH']

      if Compat.platform =~ /mswin(32|64)/
        return ENV['PATH'].split(';')
      else
        return ENV['PATH'].split(':')
      end
    end

    #
    # Finds the program matching _name_ and returns it's full path.
    # If the program was not found, +nil+ will be returned.
    #
    #   Compat.find_program('as')  #=> "/usr/bin/as"
    #
    def Compat.find_program(name)
      Compat.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

    #
    # Finds the program matching one of the names within _names_ and
    # returns it's full path. If no program was found matching any of
    # the names, the +nil+ will be returned.
    #
    #   Compat.find_program_by_names("gas","as")  #=> "/usr/bin/as"
    #
    def Compat.find_program_by_names(*names)
      names.map { |name| Compat.find_program(name) }.compact.first
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rprogram-0.1.6 lib/rprogram/compat.rb