Use custom Gem.configuration to avoid loading gemrc files

This commit is contained in:
Mitchell Hashimoto 2013-02-07 16:32:11 -08:00
parent 2b4920b4b6
commit 7bc72028bd
1 changed files with 25 additions and 0 deletions

View File

@ -1,4 +1,5 @@
require "rubygems" require "rubygems"
require "rubygems/config_file"
require "rubygems/gem_runner" require "rubygems/gem_runner"
require "log4r" require "log4r"
@ -26,6 +27,11 @@ module VagrantPlugins
# Clear paths so that it reads the new GEM_HOME setting # Clear paths so that it reads the new GEM_HOME setting
Gem.paths = ENV Gem.paths = ENV
# Set a custom configuration to avoid loading ~/.gemrc loads and
# /etc/gemrc and so on.
old_config = Gem.configuration
Gem.configuration = NilGemConfig.new
# Clear the sources so that installation uses custom sources # Clear the sources so that installation uses custom sources
old_sources = Gem.sources old_sources = Gem.sources
Gem.sources = Gem.default_sources Gem.sources = Gem.default_sources
@ -41,9 +47,28 @@ module VagrantPlugins
ENV["GEM_PATH"] = old_gem_path ENV["GEM_PATH"] = old_gem_path
# Reset everything # Reset everything
Gem.configuration = old_config
Gem.paths = ENV Gem.paths = ENV
Gem.sources = old_sources.to_a Gem.sources = old_sources.to_a
end end
# 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
end end
end end
end end