1##
2#
3# Gem::PathSupport facilitates the GEM_HOME and GEM_PATH environment settings
4# to the rest of RubyGems.
5#
6class Gem::PathSupport
7  ##
8  # The default system path for managing Gems.
9  attr_reader :home
10
11  ##
12  # Array of paths to search for Gems.
13  attr_reader :path
14
15  ##
16  #
17  # Constructor. Takes a single argument which is to be treated like a
18  # hashtable, or defaults to ENV, the system environment.
19  #
20  def initialize(env=ENV)
21    @env = env
22
23    # note 'env' vs 'ENV'...
24    @home     = env["GEM_HOME"] || ENV["GEM_HOME"] || Gem.default_dir
25
26    if File::ALT_SEPARATOR then
27      @home   = @home.gsub(File::ALT_SEPARATOR, File::SEPARATOR)
28    end
29
30    self.path = env["GEM_PATH"] || ENV["GEM_PATH"]
31  end
32
33  private
34
35  ##
36  # Set the Gem home directory (as reported by Gem.dir).
37
38  def home=(home)
39    @home = home.to_s
40  end
41
42  ##
43  # Set the Gem search path (as reported by Gem.path).
44
45  def path=(gpaths)
46    # FIX: it should be [home, *path], not [*path, home]
47
48    gem_path = []
49
50    # FIX: I can't tell wtf this is doing.
51    gpaths ||= (ENV['GEM_PATH'] || "").empty? ? nil : ENV["GEM_PATH"]
52
53    if gpaths
54      if gpaths.kind_of?(Array)
55        gem_path = gpaths.dup
56      else
57        gem_path = gpaths.split(Gem.path_separator)
58      end
59
60      if File::ALT_SEPARATOR then
61        gem_path.map! do |this_path|
62          this_path.gsub File::ALT_SEPARATOR, File::SEPARATOR
63        end
64      end
65
66      gem_path << @home
67    else
68      gem_path = Gem.default_path + [@home]
69
70      if defined?(APPLE_GEM_HOME)
71        gem_path << APPLE_GEM_HOME
72      end
73    end
74
75    @path = gem_path.uniq
76  end
77end
78