2017-09-08 04:45:41 -04:00
|
|
|
# frozen_string_literal: true
|
2017-11-01 19:29:38 -04:00
|
|
|
|
2017-09-08 04:45:41 -04:00
|
|
|
module Bundler
|
|
|
|
class EnvironmentPreserver
|
2017-11-01 19:29:38 -04:00
|
|
|
INTENTIONALLY_NIL = "BUNDLER_ENVIRONMENT_PRESERVER_INTENTIONALLY_NIL".freeze
|
|
|
|
BUNDLER_KEYS = %w[
|
|
|
|
BUNDLE_BIN_PATH
|
|
|
|
BUNDLE_GEMFILE
|
|
|
|
BUNDLER_ORIG_MANPATH
|
|
|
|
BUNDLER_VERSION
|
|
|
|
GEM_HOME
|
|
|
|
GEM_PATH
|
|
|
|
MANPATH
|
|
|
|
PATH
|
|
|
|
RB_USER_INSTALL
|
|
|
|
RUBYLIB
|
|
|
|
RUBYOPT
|
|
|
|
].map(&:freeze).freeze
|
|
|
|
BUNDLER_PREFIX = "BUNDLER_ORIG_".freeze
|
|
|
|
|
2017-09-08 04:45:41 -04:00
|
|
|
# @param env [ENV]
|
|
|
|
# @param keys [Array<String>]
|
|
|
|
def initialize(env, keys)
|
|
|
|
@original = env.to_hash
|
|
|
|
@keys = keys
|
2017-11-01 19:29:38 -04:00
|
|
|
@prefix = BUNDLER_PREFIX
|
2017-09-08 04:45:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# @return [Hash]
|
|
|
|
def backup
|
|
|
|
env = @original.clone
|
|
|
|
@keys.each do |key|
|
|
|
|
value = env[key]
|
2017-11-01 19:29:38 -04:00
|
|
|
if !value.nil? && !value.empty?
|
|
|
|
env[@prefix + key] ||= value
|
|
|
|
elsif value.nil?
|
|
|
|
env[@prefix + key] ||= INTENTIONALLY_NIL
|
2017-09-08 04:45:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
env
|
|
|
|
end
|
|
|
|
|
|
|
|
# @return [Hash]
|
|
|
|
def restore
|
|
|
|
env = @original.clone
|
|
|
|
@keys.each do |key|
|
|
|
|
value_original = env[@prefix + key]
|
2017-11-01 19:29:38 -04:00
|
|
|
next if value_original.nil? || value_original.empty?
|
|
|
|
if value_original == INTENTIONALLY_NIL
|
|
|
|
env.delete(key)
|
|
|
|
else
|
2017-09-08 04:45:41 -04:00
|
|
|
env[key] = value_original
|
|
|
|
end
|
2017-11-01 19:29:38 -04:00
|
|
|
env.delete(@prefix + key)
|
2017-09-08 04:45:41 -04:00
|
|
|
end
|
|
|
|
env
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|