TASAKA Mamoru bc96e30
module Gem
TASAKA Mamoru bc96e30
  class << self
TASAKA Mamoru bc96e30
TASAKA Mamoru bc96e30
    ##
TASAKA Mamoru bc96e30
    # Returns a string representing that part or the directory tree that is
TASAKA Mamoru bc96e30
    # common to all specified directories.
TASAKA Mamoru bc96e30
TASAKA Mamoru bc96e30
    def common_path(dirs)
TASAKA Mamoru bc96e30
      paths = dirs.collect {|dir| dir.split(File::SEPARATOR)}
TASAKA Mamoru bc96e30
      uncommon_idx = paths.transpose.each_with_index.find {|dirnames, idx| dirnames.uniq.length > 1}.last
TASAKA Mamoru bc96e30
      paths[0][0 ... uncommon_idx].join(File::SEPARATOR)
TASAKA Mamoru bc96e30
    end
TASAKA Mamoru bc96e30
    private :common_path
TASAKA Mamoru bc96e30
TASAKA Mamoru bc96e30
    ##
TASAKA Mamoru bc96e30
    # Default gems locations allowed on FHS system (/usr, /usr/share).
TASAKA Mamoru bc96e30
    # The locations are derived from directories specified during build
TASAKA Mamoru bc96e30
    # configuration.
TASAKA Mamoru bc96e30
TASAKA Mamoru bc96e30
    def default_locations
TASAKA Mamoru bc96e30
      @default_locations ||= {
TASAKA Mamoru bc96e30
        :system => common_path([ConfigMap[:vendorlibdir], ConfigMap[:vendorarchdir]]),
TASAKA Mamoru bc96e30
        :local => common_path([ConfigMap[:sitelibdir], ConfigMap[:sitearchdir]])
TASAKA Mamoru bc96e30
      }
TASAKA Mamoru bc96e30
    end
TASAKA Mamoru bc96e30
TASAKA Mamoru bc96e30
    ##
TASAKA Mamoru bc96e30
    # For each location provides set of directories for binaries (:bin_dir)
TASAKA Mamoru bc96e30
    # platform independent (:gem_dir) and dependent (:ext_dir) files.
TASAKA Mamoru bc96e30
TASAKA Mamoru bc96e30
    def default_dirs
TASAKA Mamoru bc96e30
      @default_dirs ||= Hash[default_locations.collect do |destination, path|
TASAKA Mamoru bc96e30
        [destination, {
TASAKA Mamoru bc96e30
          :bin_dir => File.join(path, ConfigMap[:bindir].split(File::SEPARATOR).last),
TASAKA Mamoru bc96e30
          :gem_dir => File.join(path, ConfigMap[:datadir].split(File::SEPARATOR).last, 'gems'),
TASAKA Mamoru bc96e30
          :ext_dir => File.join(path, ConfigMap[:libdir].split(File::SEPARATOR).last, 'gems')
TASAKA Mamoru bc96e30
        }]
TASAKA Mamoru bc96e30
      end]
TASAKA Mamoru bc96e30
    end
TASAKA Mamoru bc96e30
TASAKA Mamoru bc96e30
    ##
TASAKA Mamoru bc96e30
    # RubyGems default overrides.
TASAKA Mamoru bc96e30
TASAKA Mamoru bc96e30
    def default_dir
TASAKA Mamoru bc96e30
      if Process.uid == 0
TASAKA Mamoru bc96e30
        Gem.default_dirs[:local][:gem_dir]
TASAKA Mamoru bc96e30
      else
TASAKA Mamoru bc96e30
        Gem.user_dir
TASAKA Mamoru bc96e30
      end
TASAKA Mamoru bc96e30
    end
TASAKA Mamoru bc96e30
TASAKA Mamoru bc96e30
    def default_path
TASAKA Mamoru bc96e30
      path = default_dirs.collect {|location, paths| paths[:gem_dir]}
TASAKA Mamoru bc96e30
      path.unshift Gem.user_dir if File.exist? Gem.user_home
TASAKA Mamoru bc96e30
    end
TASAKA Mamoru bc96e30
TASAKA Mamoru bc96e30
    def default_bindir
TASAKA Mamoru bc96e30
      if Process.uid == 0
TASAKA Mamoru bc96e30
        Gem.default_dirs[:local][:bin_dir]
TASAKA Mamoru bc96e30
      else
TASAKA Mamoru bc96e30
        File.join [Dir.home, 'bin']
TASAKA Mamoru bc96e30
      end
TASAKA Mamoru bc96e30
    end
TASAKA Mamoru bc96e30
TASAKA Mamoru bc96e30
    def default_ext_dir_for base_dir
TASAKA Mamoru bc96e30
      dirs = Gem.default_dirs.detect {|location, paths| paths[:gem_dir] == base_dir}
TASAKA Mamoru bc96e30
      dirs && File.join(dirs.last[:ext_dir], 'exts')
TASAKA Mamoru bc96e30
    end
TASAKA Mamoru bc96e30
  end
TASAKA Mamoru bc96e30
end