Update with_clean_env helpers

This commit is contained in:
Seth Vargo 2015-07-09 16:03:40 -06:00
parent 50a64ea30b
commit 46563560f4
1 changed files with 21 additions and 13 deletions

View File

@ -1,7 +1,17 @@
require "bundler"
module Vagrant module Vagrant
module Util module Util
class Env class Env
# def self.with_original_env
original_env = ENV.to_hash
ENV.replace(::Bundler::ORIGINAL_ENV) if defined?(::Bundler::ORIGINAL_ENV)
ENV.update(Vagrant.original_env)
yield
ensure
ENV.replace(original_env.to_hash)
end
# Execute the given command, removing any Ruby-specific environment # Execute the given command, removing any Ruby-specific environment
# variables. This is an "enhanced" version of `Bundler.with_clean_env`, # variables. This is an "enhanced" version of `Bundler.with_clean_env`,
# which only removes Bundler-specific values. We need to remove all # which only removes Bundler-specific values. We need to remove all
@ -25,18 +35,16 @@ module Vagrant
# #
# @param [Proc] block # @param [Proc] block
# the block to execute with the cleaned environment # the block to execute with the cleaned environment
# def self.with_clean_env
def self.with_clean_env(&block) with_original_env do
original = ENV.to_hash ENV["MANPATH"] = ENV["BUNDLE_ORIG_MANPATH"]
ENV.delete_if { |k,_| k[0,7] == "BUNDLE_" }
ENV.delete('_ORIGINAL_GEM_PATH') if ENV.has_key? "RUBYOPT"
ENV.delete_if { |k,_| k.start_with?('BUNDLE_') } ENV["RUBYOPT"] = ENV["RUBYOPT"].sub("-rbundler/setup", "")
ENV.delete_if { |k,_| k.start_with?('GEM_') } ENV["RUBYOPT"] = ENV["RUBYOPT"].sub("-I#{File.expand_path('..', __FILE__)}", "")
ENV.delete_if { |k,_| k.start_with?('RUBY') } end
yield yield
ensure end
ENV.replace(original.to_hash)
end end
end end
end end