2016-02-01 15:43:26 +03:00
|
|
|
# frozen_string_literal: true
|
2011-06-01 07:45:05 +04:00
|
|
|
##
|
2012-11-29 10:52:18 +04:00
|
|
|
#
|
2011-06-01 07:45:05 +04:00
|
|
|
# Gem::PathSupport facilitates the GEM_HOME and GEM_PATH environment settings
|
|
|
|
# to the rest of RubyGems.
|
|
|
|
#
|
|
|
|
class Gem::PathSupport
|
2019-02-14 15:59:03 +03:00
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
##
|
|
|
|
# The default system path for managing Gems.
|
|
|
|
attr_reader :home
|
|
|
|
|
|
|
|
##
|
|
|
|
# Array of paths to search for Gems.
|
|
|
|
attr_reader :path
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
|
|
|
# Directory with spec cache
|
|
|
|
attr_reader :spec_cache_dir # :nodoc:
|
|
|
|
|
2011-06-01 07: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-04 03:29:40 +03:00
|
|
|
def initialize(env)
|
2018-08-27 13:05:04 +03:00
|
|
|
@home = env["GEM_HOME"] || Gem.default_dir
|
2011-07-27 05:40:07 +04:00
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
if File::ALT_SEPARATOR
|
2018-08-27 13:05:04 +03:00
|
|
|
@home = @home.gsub(File::ALT_SEPARATOR, File::SEPARATOR)
|
2011-07-27 05:40:07 +04:00
|
|
|
end
|
|
|
|
|
2018-08-27 13:05:04 +03:00
|
|
|
@home = expand(@home)
|
|
|
|
|
2016-03-04 03:29:40 +03:00
|
|
|
@path = split_gem_path env["GEM_PATH"], @home
|
2013-09-14 12:59:02 +04:00
|
|
|
|
2016-03-04 03:29:40 +03:00
|
|
|
@spec_cache_dir = env["GEM_SPEC_CACHE"] || Gem.default_spec_cache_dir
|
2013-09-14 12:59:02 +04:00
|
|
|
|
|
|
|
@spec_cache_dir = @spec_cache_dir.dup.untaint
|
2011-06-01 07:45:05 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
##
|
2016-03-04 03:29:40 +03:00
|
|
|
# Split the Gem search path (as reported by Gem.path).
|
2011-06-01 07:45:05 +04:00
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def split_gem_path(gpaths, home)
|
2012-11-29 10:52:18 +04:00
|
|
|
# FIX: it should be [home, *path], not [*path, home]
|
|
|
|
|
|
|
|
gem_path = []
|
2011-06-01 07:45:05 +04:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
if gpaths
|
2016-03-04 03:29:40 +03: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-06-01 07:45:05 +04:00
|
|
|
end
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
if File::ALT_SEPARATOR
|
2011-06-01 07:45:05 +04:00
|
|
|
gem_path.map! do |this_path|
|
|
|
|
this_path.gsub File::ALT_SEPARATOR, File::SEPARATOR
|
|
|
|
end
|
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2016-03-04 03:29:40 +03:00
|
|
|
gem_path << home
|
2011-06-01 07:45:05 +04:00
|
|
|
else
|
2016-02-01 15:43:26 +03:00
|
|
|
gem_path = default_path
|
2011-06-01 07:45:05 +04:00
|
|
|
end
|
|
|
|
|
2018-08-27 13:05:04 +03:00
|
|
|
gem_path.map { |path| expand(path) }.uniq
|
2011-06-01 07:45:05 +04:00
|
|
|
end
|
2016-02-01 15:43:26 +03:00
|
|
|
|
|
|
|
# Return the default Gem path
|
|
|
|
def default_path
|
|
|
|
gem_path = Gem.default_path + [@home]
|
|
|
|
|
|
|
|
if defined?(APPLE_GEM_HOME)
|
|
|
|
gem_path << APPLE_GEM_HOME
|
|
|
|
end
|
|
|
|
gem_path
|
|
|
|
end
|
2018-08-27 13:05:04 +03:00
|
|
|
|
|
|
|
def expand(path)
|
|
|
|
if File.directory?(path)
|
|
|
|
File.realpath(path)
|
|
|
|
else
|
|
|
|
path
|
|
|
|
end
|
|
|
|
end
|
2019-02-14 15:59:03 +03:00
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
end
|