2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2011-05-31 23:45:05 -04:00
|
|
|
##
|
2012-11-29 01:52:18 -05:00
|
|
|
#
|
2011-05-31 23:45:05 -04:00
|
|
|
# Gem::PathSupport facilitates the GEM_HOME and GEM_PATH environment settings
|
|
|
|
# to the rest of RubyGems.
|
|
|
|
#
|
|
|
|
class Gem::PathSupport
|
|
|
|
##
|
|
|
|
# The default system path for managing Gems.
|
|
|
|
attr_reader :home
|
|
|
|
|
|
|
|
##
|
|
|
|
# Array of paths to search for Gems.
|
|
|
|
attr_reader :path
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
##
|
|
|
|
# Directory with spec cache
|
|
|
|
attr_reader :spec_cache_dir # :nodoc:
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Constructor. Takes a single argument which is to be treated like a
|
|
|
|
# hashtable, or defaults to ENV, the system environment.
|
|
|
|
#
|
2016-03-03 19:29:40 -05:00
|
|
|
def initialize(env)
|
2018-08-27 06:05:04 -04:00
|
|
|
@home = env["GEM_HOME"] || Gem.default_dir
|
2011-07-26 21:40:07 -04:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if File::ALT_SEPARATOR
|
2018-08-27 06:05:04 -04:00
|
|
|
@home = @home.gsub(File::ALT_SEPARATOR, File::SEPARATOR)
|
2011-07-26 21:40:07 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
@home = expand(@home)
|
|
|
|
|
2016-03-03 19:29:40 -05:00
|
|
|
@path = split_gem_path env["GEM_PATH"], @home
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2016-03-03 19:29:40 -05:00
|
|
|
@spec_cache_dir = env["GEM_SPEC_CACHE"] || Gem.default_spec_cache_dir
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2019-11-11 01:03:57 -05:00
|
|
|
@spec_cache_dir = @spec_cache_dir.dup.tap(&Gem::UNTAINT)
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
##
|
2016-03-03 19:29:40 -05:00
|
|
|
# Split the Gem search path (as reported by Gem.path).
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def split_gem_path(gpaths, home)
|
2012-11-29 01:52:18 -05:00
|
|
|
# FIX: it should be [home, *path], not [*path, home]
|
|
|
|
|
|
|
|
gem_path = []
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
if gpaths
|
2016-03-03 19:29:40 -05:00
|
|
|
gem_path = gpaths.split(Gem.path_separator)
|
|
|
|
# Handle the path_separator being set to a regexp, which will cause
|
|
|
|
# end_with? to error
|
|
|
|
if gpaths =~ /#{Gem.path_separator}\z/
|
|
|
|
gem_path += default_path
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if File::ALT_SEPARATOR
|
2011-05-31 23:45:05 -04:00
|
|
|
gem_path.map! do |this_path|
|
|
|
|
this_path.gsub File::ALT_SEPARATOR, File::SEPARATOR
|
|
|
|
end
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2016-03-03 19:29:40 -05:00
|
|
|
gem_path << home
|
2011-05-31 23:45:05 -04:00
|
|
|
else
|
2016-02-01 07:43:26 -05:00
|
|
|
gem_path = default_path
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
|
|
|
|
2020-06-10 13:46:05 -04:00
|
|
|
gem_path.map {|path| expand(path) }.uniq
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
2016-02-01 07:43:26 -05:00
|
|
|
|
|
|
|
# Return the default Gem path
|
|
|
|
def default_path
|
2021-08-08 04:40:11 -04:00
|
|
|
Gem.default_path + [@home]
|
2016-02-01 07:43:26 -05:00
|
|
|
end
|
2018-08-27 06:05:04 -04:00
|
|
|
|
|
|
|
def expand(path)
|
|
|
|
if File.directory?(path)
|
|
|
|
File.realpath(path)
|
|
|
|
else
|
|
|
|
path
|
|
|
|
end
|
|
|
|
end
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|