lib/bundler.rb in bundler-2.0.2 vs lib/bundler.rb in bundler-2.1.0.pre.1

- old
+ new

@@ -1,67 +1,64 @@ # frozen_string_literal: true -require "bundler/compatibility_guard" - -require "bundler/vendored_fileutils" +require_relative "bundler/vendored_fileutils" require "pathname" require "rbconfig" -require "thread" -require "bundler/errors" -require "bundler/environment_preserver" -require "bundler/plugin" -require "bundler/rubygems_ext" -require "bundler/rubygems_integration" -require "bundler/version" -require "bundler/constants" -require "bundler/current_ruby" -require "bundler/build_metadata" +require_relative "bundler/errors" +require_relative "bundler/environment_preserver" +require_relative "bundler/plugin" +require_relative "bundler/rubygems_ext" +require_relative "bundler/rubygems_integration" +require_relative "bundler/version" +require_relative "bundler/constants" +require_relative "bundler/current_ruby" +require_relative "bundler/build_metadata" module Bundler environment_preserver = EnvironmentPreserver.new(ENV, EnvironmentPreserver::BUNDLER_KEYS) 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" - autoload :Deprecate, "bundler/deprecate" - autoload :Dsl, "bundler/dsl" - autoload :EndpointSpecification, "bundler/endpoint_specification" - autoload :Env, "bundler/env" - autoload :Fetcher, "bundler/fetcher" - autoload :FeatureFlag, "bundler/feature_flag" - autoload :GemHelper, "bundler/gem_helper" - autoload :GemHelpers, "bundler/gem_helpers" - autoload :GemRemoteFetcher, "bundler/gem_remote_fetcher" - autoload :GemVersionPromoter, "bundler/gem_version_promoter" - autoload :Graph, "bundler/graph" - autoload :Index, "bundler/index" - autoload :Injector, "bundler/injector" - autoload :Installer, "bundler/installer" - autoload :LazySpecification, "bundler/lazy_specification" - autoload :LockfileParser, "bundler/lockfile_parser" - autoload :MatchPlatform, "bundler/match_platform" - autoload :ProcessLock, "bundler/process_lock" - autoload :RemoteSpecification, "bundler/remote_specification" - autoload :Resolver, "bundler/resolver" - autoload :Retry, "bundler/retry" - autoload :RubyDsl, "bundler/ruby_dsl" - autoload :RubyGemsGemInstaller, "bundler/rubygems_gem_installer" - autoload :RubyVersion, "bundler/ruby_version" - autoload :Runtime, "bundler/runtime" - autoload :Settings, "bundler/settings" - autoload :SharedHelpers, "bundler/shared_helpers" - autoload :Source, "bundler/source" - autoload :SourceList, "bundler/source_list" - autoload :SpecSet, "bundler/spec_set" - autoload :StubSpecification, "bundler/stub_specification" - autoload :UI, "bundler/ui" - autoload :URICredentialsFilter, "bundler/uri_credentials_filter" - autoload :VersionRanges, "bundler/version_ranges" + autoload :Definition, File.expand_path("bundler/definition", __dir__) + autoload :Dependency, File.expand_path("bundler/dependency", __dir__) + autoload :DepProxy, File.expand_path("bundler/dep_proxy", __dir__) + autoload :Deprecate, File.expand_path("bundler/deprecate", __dir__) + autoload :Dsl, File.expand_path("bundler/dsl", __dir__) + autoload :EndpointSpecification, File.expand_path("bundler/endpoint_specification", __dir__) + autoload :Env, File.expand_path("bundler/env", __dir__) + autoload :Fetcher, File.expand_path("bundler/fetcher", __dir__) + autoload :FeatureFlag, File.expand_path("bundler/feature_flag", __dir__) + autoload :GemHelper, File.expand_path("bundler/gem_helper", __dir__) + autoload :GemHelpers, File.expand_path("bundler/gem_helpers", __dir__) + autoload :GemRemoteFetcher, File.expand_path("bundler/gem_remote_fetcher", __dir__) + autoload :GemVersionPromoter, File.expand_path("bundler/gem_version_promoter", __dir__) + autoload :Graph, File.expand_path("bundler/graph", __dir__) + autoload :Index, File.expand_path("bundler/index", __dir__) + autoload :Injector, File.expand_path("bundler/injector", __dir__) + autoload :Installer, File.expand_path("bundler/installer", __dir__) + autoload :LazySpecification, File.expand_path("bundler/lazy_specification", __dir__) + autoload :LockfileParser, File.expand_path("bundler/lockfile_parser", __dir__) + autoload :MatchPlatform, File.expand_path("bundler/match_platform", __dir__) + autoload :ProcessLock, File.expand_path("bundler/process_lock", __dir__) + autoload :RemoteSpecification, File.expand_path("bundler/remote_specification", __dir__) + autoload :Resolver, File.expand_path("bundler/resolver", __dir__) + autoload :Retry, File.expand_path("bundler/retry", __dir__) + autoload :RubyDsl, File.expand_path("bundler/ruby_dsl", __dir__) + autoload :RubyGemsGemInstaller, File.expand_path("bundler/rubygems_gem_installer", __dir__) + autoload :RubyVersion, File.expand_path("bundler/ruby_version", __dir__) + autoload :Runtime, File.expand_path("bundler/runtime", __dir__) + autoload :Settings, File.expand_path("bundler/settings", __dir__) + autoload :SharedHelpers, File.expand_path("bundler/shared_helpers", __dir__) + autoload :Source, File.expand_path("bundler/source", __dir__) + autoload :SourceList, File.expand_path("bundler/source_list", __dir__) + autoload :SpecSet, File.expand_path("bundler/spec_set", __dir__) + autoload :StubSpecification, File.expand_path("bundler/stub_specification", __dir__) + autoload :UI, File.expand_path("bundler/ui", __dir__) + autoload :URICredentialsFilter, File.expand_path("bundler/uri_credentials_filter", __dir__) + autoload :VersionRanges, File.expand_path("bundler/version_ranges", __dir__) class << self def configure @configured ||= configure_gem_home_and_path end @@ -117,11 +114,11 @@ def load @load ||= Runtime.new(root, definition) end def environment - SharedHelpers.major_deprecation 3, "Bundler.environment has been removed in favor of Bundler.load" + SharedHelpers.major_deprecation 2, "Bundler.environment has been removed in favor of Bundler.load" load end # Returns an instance of Bundler::Definition for given Gemfile and lockfile # @@ -151,11 +148,11 @@ LockfileParser.new(lock) end end def ruby_scope - "#{Bundler.rubygems.ruby_engine}/#{Bundler.rubygems.config_map[:ruby_version]}" + "#{Bundler.rubygems.ruby_engine}/#{RbConfig::CONFIG["ruby_version"]}" end def user_home @user_home ||= begin home = Bundler.rubygems.user_home @@ -196,23 +193,23 @@ end def user_bundle_path(dir = "home") env_var, fallback = case dir when "home" - ["BUNDLE_USER_HOME", Pathname.new(user_home).join(".bundle")] + ["BUNDLE_USER_HOME", proc { Pathname.new(user_home).join(".bundle") }] when "cache" - ["BUNDLE_USER_CACHE", user_bundle_path.join("cache")] + ["BUNDLE_USER_CACHE", proc { user_bundle_path.join("cache") }] when "config" - ["BUNDLE_USER_CONFIG", user_bundle_path.join("config")] + ["BUNDLE_USER_CONFIG", proc { user_bundle_path.join("config") }] when "plugin" - ["BUNDLE_USER_PLUGIN", user_bundle_path.join("plugin")] + ["BUNDLE_USER_PLUGIN", proc { user_bundle_path.join("plugin") }] else raise BundlerError, "Unknown user path requested: #{dir}" end # `fallback` will already be a Pathname, but Pathname.new() is # idempotent so it's OK - Pathname.new(ENV.fetch(env_var, fallback)) + Pathname.new(ENV.fetch(env_var, &fallback)) end def user_cache user_bundle_path("cache") end @@ -261,11 +258,11 @@ FileUtils.remove_entry_secure(path) if path && File.exist?(path) rescue ArgumentError message = <<EOF It is a security vulnerability to allow your home directory to be world-writable, and bundler can not continue. You should probably consider fixing this issue by running `chmod o-w ~` on *nix. -Please refer to http://ruby-doc.org/stdlib-2.1.2/libdoc/fileutils/rdoc/FileUtils.html#method-c-remove_entry_secure for details. +Please refer to https://ruby-doc.org/stdlib-2.1.2/libdoc/fileutils/rdoc/FileUtils.html#method-c-remove_entry_secure for details. EOF File.world_writable?(path) ? Bundler.ui.warn(message) : raise raise PathError, "Please fix the world-writable issue with your #{path} directory" end @@ -278,14 +275,23 @@ # @return [Hash] Environment present before Bundler was activated def original_env ORIGINAL_ENV.clone end - # @deprecated Use `original_env` instead - # @return [Hash] Environment with all bundler-related variables removed + # @deprecated Use `unbundled_env` instead def clean_env - Bundler::SharedHelpers.major_deprecation(3, "`Bundler.clean_env` has weird edge cases, use `.original_env` instead") + Bundler::SharedHelpers.major_deprecation( + 2, + "`Bundler.clean_env` has been deprecated in favor of `Bundler.unbundled_env`. " \ + "If you instead want the environment before bundler was originally loaded, use `Bundler.original_env`" + ) + + unbundled_env + end + + # @return [Hash] Environment with all bundler-related variables removed + def unbundled_env env = original_env if env.key?("BUNDLER_ORIG_MANPATH") env["MANPATH"] = env["BUNDLER_ORIG_MANPATH"] end @@ -303,26 +309,73 @@ end env end + # Run block with environment present before Bundler was activated def with_original_env with_env(original_env) { yield } end + # @deprecated Use `with_unbundled_env` instead def with_clean_env - with_env(clean_env) { yield } + Bundler::SharedHelpers.major_deprecation( + 2, + "`Bundler.with_clean_env` has been deprecated in favor of `Bundler.with_unbundled_env`. " \ + "If you instead want the environment before bundler was originally loaded, use `Bundler.with_original_env`" + ) + + with_env(unbundled_env) { yield } end + # Run block with all bundler-related variables removed + def with_unbundled_env + with_env(unbundled_env) { yield } + end + + # Run subcommand with the environment present before Bundler was activated + def original_system(*args) + with_original_env { Kernel.system(*args) } + end + + # @deprecated Use `unbundled_system` instead def clean_system(*args) - with_clean_env { Kernel.system(*args) } + Bundler::SharedHelpers.major_deprecation( + 2, + "`Bundler.clean_system` has been deprecated in favor of `Bundler.unbundled_system`. " \ + "If you instead want to run the command in the environment before bundler was originally loaded, use `Bundler.original_system`" + ) + + with_env(unbundled_env) { Kernel.system(*args) } end + # Run subcommand in an environment with all bundler related variables removed + def unbundled_system(*args) + with_unbundled_env { Kernel.system(*args) } + end + + # Run a `Kernel.exec` to a subcommand with the environment present before Bundler was activated + def original_exec(*args) + with_original_env { Kernel.exec(*args) } + end + + # @deprecated Use `unbundled_exec` instead def clean_exec(*args) - with_clean_env { Kernel.exec(*args) } + Bundler::SharedHelpers.major_deprecation( + 2, + "`Bundler.clean_exec` has been deprecated in favor of `Bundler.unbundled_exec`. " \ + "If you instead want to exec to a command in the environment before bundler was originally loaded, use `Bundler.original_exec`" + ) + + with_env(unbundled_env) { Kernel.exec(*args) } end + # Run a `Kernel.exec` to a subcommand in an environment with all bundler related variables removed + def unbundled_exec(*args) + with_env(unbundled_env) { Kernel.exec(*args) } + end + def local_platform return Gem::Platform::RUBY if settings[:force_ruby_platform] Gem::Platform.local end @@ -341,11 +394,11 @@ def system_bindir # Gem.bindir doesn't always return the location that RubyGems will install # system binaries. If you put '-n foo' in your .gemrc, RubyGems will # install binstubs there instead. Unfortunately, RubyGems doesn't expose # that directory at all, so rather than parse .gemrc ourselves, we allow - # the directory to be set as well, via `bundle config bindir foo`. + # the directory to be set as well, via `bundle config set bindir foo`. Bundler.settings[:system_bindir] || Bundler.rubygems.gem_bindir end def use_system_gems? configured_bundle_path.use_system_gems? @@ -507,11 +560,11 @@ end private def eval_yaml_gemspec(path, contents) - Kernel.send(:require, "bundler/psyched_yaml") + require_relative "bundler/psyched_yaml" # If the YAML is invalid, Syck raises an ArgumentError, and Psych # raises a Psych::SyntaxError. See psyched_yaml.rb for more info. Gem::Specification.from_yaml(contents) rescue YamlLibrarySyntaxError, ArgumentError, Gem::EndOfYAMLException, Gem::Exception @@ -521,10 +574,10 @@ def eval_gemspec(path, contents) eval(contents, TOPLEVEL_BINDING.dup, path.expand_path.to_s) rescue ScriptError, StandardError => e msg = "There was an error while loading `#{path.basename}`: #{e.message}" - if e.is_a?(LoadError) && RUBY_VERSION >= "1.9" + if e.is_a?(LoadError) msg += "\nDoes it try to require a relative path? That's been removed in Ruby 1.9" end raise GemspecError, Dsl::DSLError.new(msg, path, e.backtrace, contents) end