lib/bundler.rb in bundler-1.12.0.pre.1 vs lib/bundler.rb in bundler-1.12.0.pre.2

- old
+ new

@@ -1,23 +1,23 @@ # frozen_string_literal: true require "fileutils" require "pathname" require "rbconfig" require "thread" -require "bundler/path_preserver" +require "bundler/environment_preserver" require "bundler/gem_remote_fetcher" require "bundler/rubygems_ext" require "bundler/rubygems_integration" require "bundler/version" require "bundler/constants" require "bundler/current_ruby" require "bundler/errors" module Bundler - PathPreserver.preserve_path_in_environment("PATH", ENV) - PathPreserver.preserve_path_in_environment("GEM_PATH", ENV) - ORIGINAL_ENV = ENV.to_hash + environment_preserver = EnvironmentPreserver.new(ENV, %w(PATH GEM_PATH)) + ORIGINAL_ENV = environment_preserver.restore + ENV.replace(environment_preserver.backup) SUDO_MUTEX = Mutex.new autoload :Definition, "bundler/definition" autoload :Dependency, "bundler/dependency" autoload :DepProxy, "bundler/dep_proxy" @@ -198,37 +198,47 @@ @settings = Settings.new(app_config_path) rescue GemfileNotFound @settings = Settings.new(Pathname.new(".bundle").expand_path) end - def with_original_env - bundled_env = ENV.to_hash - ENV.replace(ORIGINAL_ENV) - yield - ensure - ENV.replace(bundled_env.to_hash) + # @return [Hash] Environment present before Bundler was activated + def original_env + ORIGINAL_ENV.clone end - def with_clean_env - with_original_env do - ENV["MANPATH"] = ENV["BUNDLE_ORIG_MANPATH"] - ENV.delete_if {|k, _| k[0, 7] == "BUNDLE_" } + # @deprecated Use `original_env` instead + # @return [Hash] Environment with all bundler-related variables removed + def clean_env + env = original_env - if ENV.key?("RUBYOPT") - ENV["RUBYOPT"] = ENV["RUBYOPT"].sub "-rbundler/setup", "" - end + if env.key?("BUNDLE_ORIG_MANPATH") + env["MANPATH"] = env["BUNDLE_ORIG_MANPATH"] + end - if ENV.key?("RUBYLIB") - rubylib = ENV["RUBYLIB"].split(File::PATH_SEPARATOR) - rubylib.delete(File.expand_path("..", __FILE__)) - ENV["RUBYLIB"] = rubylib.join(File::PATH_SEPARATOR) - end + env.delete_if {|k, _| k[0, 7] == "BUNDLE_" } - yield + if env.key?("RUBYOPT") + env["RUBYOPT"] = env["RUBYOPT"].sub "-rbundler/setup", "" end + + if env.key?("RUBYLIB") + rubylib = env["RUBYLIB"].split(File::PATH_SEPARATOR) + rubylib.delete(File.expand_path("..", __FILE__)) + env["RUBYLIB"] = rubylib.join(File::PATH_SEPARATOR) + end + + env end + def with_original_env + with_env(original_env) { yield } + end + + def with_clean_env + with_env(clean_env) { yield } + end + def clean_system(*args) with_clean_env { Kernel.system(*args) } end def clean_exec(*args) @@ -343,22 +353,20 @@ path = Pathname.new(file) # Eval the gemspec from its parent directory, because some gemspecs # depend on "./" relative paths. SharedHelpers.chdir(path.dirname.to_s) do contents = path.read - if contents[0..2] == "---" # YAML header - spec = eval_yaml_gemspec(path, contents) + spec = if contents[0..2] == "---" # YAML header + eval_yaml_gemspec(path, contents) else - spec = eval_gemspec(path, contents) + eval_gemspec(path, contents) end - Bundler.rubygems.validate(spec) if spec && validate + return unless spec + spec.loaded_from = path.expand_path.to_s + Bundler.rubygems.validate(spec) if validate spec end - rescue Gem::InvalidSpecificationException => e - error_message = "The gemspec at #{file} is not valid. Please fix this gemspec.\n" \ - "The validation error was '#{e.message}'\n" - raise Gem::InvalidSpecificationException.new(error_message) end def clear_gemspec_cache @gemspec_cache = {} end @@ -427,8 +435,17 @@ def upgrade_lockfile lockfile = default_lockfile return unless lockfile.exist? && lockfile.read(3) == "---" Bundler.ui.warn "Detected Gemfile.lock generated by 0.9, deleting..." lockfile.rmtree + end + + # @param env [Hash] + def with_env(env) + backup = ENV.to_hash + ENV.replace(env) + yield + ensure + ENV.replace(backup) end end end