2013-02-03 07:31:53 +00:00
|
|
|
require "rubygems"
|
2013-02-08 00:32:11 +00:00
|
|
|
require "rubygems/config_file"
|
2013-02-03 07:31:53 +00:00
|
|
|
require "rubygems/gem_runner"
|
|
|
|
|
|
|
|
require "log4r"
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module CommandPlugin
|
|
|
|
# This class provides methods to help with calling out to the
|
|
|
|
# `gem` command but using the RubyGems API.
|
|
|
|
class GemHelper
|
|
|
|
def initialize(gem_home)
|
|
|
|
@gem_home = gem_home.to_s
|
|
|
|
@logger = Log4r::Logger.new("vagrant::plugins::plugincommand::gemhelper")
|
|
|
|
end
|
|
|
|
|
2013-02-03 07:52:34 +00:00
|
|
|
# This will yield the given block with the proper ENV setup so
|
|
|
|
# that RubyGems only sees the gems in the Vagrant-managed gem
|
|
|
|
# path.
|
|
|
|
def with_environment
|
|
|
|
old_gem_home = ENV["GEM_HOME"]
|
2013-02-03 18:15:46 +00:00
|
|
|
old_gem_path = ENV["GEM_PATH"]
|
2013-02-03 07:52:34 +00:00
|
|
|
ENV["GEM_HOME"] = @gem_home
|
2013-02-03 18:15:46 +00:00
|
|
|
ENV["GEM_PATH"] = @gem_home
|
|
|
|
@logger.debug("Set GEM_* to: #{ENV["GEM_HOME"]}")
|
2013-02-03 07:52:34 +00:00
|
|
|
|
|
|
|
# Clear paths so that it reads the new GEM_HOME setting
|
2013-02-03 18:15:46 +00:00
|
|
|
Gem.paths = ENV
|
2013-02-03 07:52:34 +00:00
|
|
|
|
2013-02-08 00:32:11 +00:00
|
|
|
# Set a custom configuration to avoid loading ~/.gemrc loads and
|
|
|
|
# /etc/gemrc and so on.
|
2014-01-06 01:11:24 +00:00
|
|
|
old_config = nil
|
|
|
|
begin
|
|
|
|
old_config = Gem.configuration
|
|
|
|
rescue Psych::SyntaxError
|
|
|
|
# Just ignore this. This means that the ".gemrc" file has
|
|
|
|
# an invalid syntax and can't be loaded. We don't care, because
|
|
|
|
# when we set Gem.configuration to nil later, it'll force a reload
|
|
|
|
# if it is needed.
|
|
|
|
end
|
2013-02-08 00:32:11 +00:00
|
|
|
Gem.configuration = NilGemConfig.new
|
|
|
|
|
2013-02-04 19:18:57 +00:00
|
|
|
# Clear the sources so that installation uses custom sources
|
|
|
|
old_sources = Gem.sources
|
|
|
|
Gem.sources = Gem.default_sources
|
2017-04-05 00:53:04 +00:00
|
|
|
Vagrant::Bundler::DEFAULT_GEM_SOURCES.each do |source|
|
|
|
|
if !Gem.sources.include?(source)
|
|
|
|
Gem.sources << source
|
|
|
|
end
|
|
|
|
end
|
2013-02-04 19:18:57 +00:00
|
|
|
|
2013-02-03 18:32:31 +00:00
|
|
|
# Use a silent UI so that we have no output
|
|
|
|
Gem::DefaultUserInteraction.use_ui(Gem::SilentUI.new) do
|
|
|
|
return yield
|
|
|
|
end
|
2013-02-03 07:52:34 +00:00
|
|
|
ensure
|
2013-02-03 18:15:46 +00:00
|
|
|
# Restore the old GEM_* settings
|
2013-02-03 07:52:34 +00:00
|
|
|
ENV["GEM_HOME"] = old_gem_home
|
2013-02-03 18:15:46 +00:00
|
|
|
ENV["GEM_PATH"] = old_gem_path
|
|
|
|
|
|
|
|
# Reset everything
|
2013-02-08 00:32:11 +00:00
|
|
|
Gem.configuration = old_config
|
2013-02-04 19:18:57 +00:00
|
|
|
Gem.paths = ENV
|
|
|
|
Gem.sources = old_sources.to_a
|
2013-02-03 07:52:34 +00:00
|
|
|
end
|
2013-02-08 00:32:11 +00:00
|
|
|
|
|
|
|
# This is pretty hacky but it is a custom implementatin of
|
|
|
|
# Gem::ConfigFile so that we don't load any gemrc files.
|
|
|
|
class NilGemConfig < Gem::ConfigFile
|
|
|
|
def initialize
|
|
|
|
# We _can not_ `super` here because that can really mess up
|
|
|
|
# some other configuration state. We need to just set everything
|
|
|
|
# directly.
|
|
|
|
|
|
|
|
@api_keys = {}
|
|
|
|
@args = []
|
|
|
|
@backtrace = false
|
|
|
|
@bulk_threshold = 1000
|
|
|
|
@hash = {}
|
|
|
|
@update_sources = true
|
|
|
|
@verbose = true
|
|
|
|
end
|
|
|
|
end
|
2013-02-03 07:31:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|