summaryrefslogtreecommitdiff
path: root/lib/rubygems/dependency_installer.rb
diff options
context:
space:
mode:
authordrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-09 23:21:36 +0000
committerdrbrain <drbrain@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-09 23:21:36 +0000
commit47f0248b0858898dd24d1e654cedf174059ca677 (patch)
tree493e84160f8609db408d88349f0624a3ff92c3c2 /lib/rubygems/dependency_installer.rb
parentcd9f9e471977447a991ced4ea38efb2309459ef5 (diff)
* lib/rubygems: Import RubyGems 2.1
* test/rubygems: Ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41873 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/rubygems/dependency_installer.rb')
-rw-r--r--lib/rubygems/dependency_installer.rb342
1 files changed, 180 insertions, 162 deletions
diff --git a/lib/rubygems/dependency_installer.rb b/lib/rubygems/dependency_installer.rb
index 6f19a310f7..2523d1b2ae 100644
--- a/lib/rubygems/dependency_installer.rb
+++ b/lib/rubygems/dependency_installer.rb
@@ -1,11 +1,12 @@
require 'rubygems'
require 'rubygems/dependency_list'
+require 'rubygems/dependency_resolver'
require 'rubygems/package'
require 'rubygems/installer'
require 'rubygems/spec_fetcher'
require 'rubygems/user_interaction'
-require 'rubygems/source_local'
-require 'rubygems/source_specific_file'
+require 'rubygems/source/local'
+require 'rubygems/source/specific_file'
require 'rubygems/available_set'
##
@@ -15,15 +16,7 @@ class Gem::DependencyInstaller
include Gem::UserInteraction
- attr_reader :gems_to_install
- attr_reader :installed_gems
-
- ##
- # Documentation types. For use by the Gem.done_installing hook
-
- attr_reader :document
-
- DEFAULT_OPTIONS = {
+ DEFAULT_OPTIONS = { # :nodoc:
:env_shebang => false,
:document => %w[ri],
:domain => :both, # HACK dup
@@ -35,9 +28,31 @@ class Gem::DependencyInstaller
:wrappers => true,
:build_args => nil,
:build_docs_in_background => false,
+ :install_as_default => false
}.freeze
##
+ # Documentation types. For use by the Gem.done_installing hook
+
+ attr_reader :document
+
+ ##
+ # Errors from SpecFetcher while searching for remote specifications
+
+ attr_reader :errors
+
+ ##
+ #--
+ # TODO remove, no longer used
+
+ attr_reader :gems_to_install # :nodoc:
+
+ ##
+ # List of gems installed by #install in alphabetic order
+
+ attr_reader :installed_gems
+
+ ##
# Creates a new installer instance.
#
# Options are:
@@ -56,7 +71,8 @@ class Gem::DependencyInstaller
# :wrappers:: See Gem::Installer::new
# :build_args:: See Gem::Installer::new
- def initialize(options = {})
+ def initialize options = {}
+ @only_install_dir = !!options[:install_dir]
@install_dir = options[:install_dir] || Gem.dir
if options[:install_dir] then
@@ -82,6 +98,7 @@ class Gem::DependencyInstaller
@wrappers = options[:wrappers]
@build_args = options[:build_args]
@build_docs_in_background = options[:build_docs_in_background]
+ @install_as_default = options[:install_as_default]
# Indicates that we should not try to update any deps unless
# we absolutely must.
@@ -93,13 +110,61 @@ class Gem::DependencyInstaller
@cache_dir = options[:cache_dir] || @install_dir
- # Set with any errors that SpecFetcher finds while search through
- # gemspecs for a dep
@errors = nil
end
- attr_reader :errors
+ ##
+ #--
+ # TODO remove, no longer used
+
+ def add_found_dependencies to_do, dependency_list # :nodoc:
+ seen = {}
+ dependencies = Hash.new { |h, name| h[name] = Gem::Dependency.new name }
+
+ until to_do.empty? do
+ spec = to_do.shift
+
+ # HACK why is spec nil?
+ next if spec.nil? or seen[spec.name]
+ seen[spec.name] = true
+
+ deps = spec.runtime_dependencies
+
+ if @development
+ if @dev_shallow
+ if @toplevel_specs.include? spec.full_name
+ deps |= spec.development_dependencies
+ end
+ else
+ deps |= spec.development_dependencies
+ end
+ end
+
+ deps.each do |dep|
+ dependencies[dep.name] = dependencies[dep.name].merge dep
+
+ if @minimal_deps
+ next if Gem::Specification.any? do |installed_spec|
+ dep.name == installed_spec.name and
+ dep.requirement.satisfied_by? installed_spec.version
+ end
+ end
+
+ results = find_gems_with_sources(dep)
+ results.sorted.each do |t|
+ to_do.push t.spec
+ end
+
+ results.remove_installed! dep
+
+ @available << results
+ results.inject_into_list dependency_list
+ end
+ end
+
+ dependency_list.remove_specs_unsatisfied_by dependencies
+ end
##
# Creates an AvailableSet to install from based on +dep_or_name+ and
# +version+
@@ -138,7 +203,7 @@ class Gem::DependencyInstaller
# sources. Gems are sorted with newer gems preferred over older gems, and
# local gems preferred over remote gems.
- def find_gems_with_sources(dep)
+ def find_gems_with_sources dep # :nodoc:
set = Gem::AvailableSet.new
if consider_local?
@@ -179,10 +244,52 @@ class Gem::DependencyInstaller
end
##
+ # Finds a spec and the source_uri it came from for gem +gem_name+ and
+ # +version+. Returns an Array of specs and sources required for
+ # installation of the gem.
+
+ def find_spec_by_name_and_version gem_name,
+ version = Gem::Requirement.default,
+ prerelease = false
+
+ set = Gem::AvailableSet.new
+
+ if consider_local?
+ if gem_name =~ /\.gem$/ and File.file? gem_name then
+ src = Gem::Source::SpecificFile.new(gem_name)
+ set.add src.spec, src
+ else
+ local = Gem::Source::Local.new
+
+ if s = local.find_gem(gem_name, version)
+ set.add s, local
+ end
+ end
+ end
+
+ if set.empty?
+ dep = Gem::Dependency.new gem_name, version
+ # HACK Dependency objects should be immutable
+ dep.prerelease = true if prerelease
+
+ set = find_gems_with_sources(dep)
+ set.match_platform!
+ end
+
+ if set.empty?
+ raise Gem::SpecificGemNotFoundException.new(gem_name, version, @errors)
+ end
+
+ @available = set
+ end
+
+ ##
# Gathers all dependencies necessary for the installation from local and
# remote sources unless the ignore_dependencies was given.
+ #--
+ # TODO remove, no longer used
- def gather_dependencies
+ def gather_dependencies # :nodoc:
specs = @available.all_specs
# these gems were listed by the user, always install them
@@ -214,93 +321,19 @@ class Gem::DependencyInstaller
@gems_to_install = dependency_list.dependency_order.reverse
end
- def add_found_dependencies to_do, dependency_list
- seen = {}
- dependencies = Hash.new { |h, name| h[name] = Gem::Dependency.new name }
-
- until to_do.empty? do
- spec = to_do.shift
-
- # HACK why is spec nil?
- next if spec.nil? or seen[spec.name]
- seen[spec.name] = true
-
- deps = spec.runtime_dependencies
-
- if @development
- if @dev_shallow
- if @toplevel_specs.include? spec.full_name
- deps |= spec.development_dependencies
- end
- else
- deps |= spec.development_dependencies
- end
- end
-
- deps.each do |dep|
- dependencies[dep.name] = dependencies[dep.name].merge dep
-
- if @minimal_deps
- next if Gem::Specification.any? do |installed_spec|
- dep.name == installed_spec.name and
- dep.requirement.satisfied_by? installed_spec.version
- end
- end
-
- results = find_gems_with_sources(dep)
-
- results.sorted.each do |t|
- to_do.push t.spec
- end
-
- results.remove_installed! dep
-
- @available << results
- results.inject_into_list dependency_list
- end
- end
-
- dependency_list.remove_specs_unsatisfied_by dependencies
- end
-
- ##
- # Finds a spec and the source_uri it came from for gem +gem_name+ and
- # +version+. Returns an Array of specs and sources required for
- # installation of the gem.
-
- def find_spec_by_name_and_version(gem_name,
- version = Gem::Requirement.default,
- prerelease = false)
-
- set = Gem::AvailableSet.new
-
- if consider_local?
- if gem_name =~ /\.gem$/ and File.file? gem_name then
- src = Gem::Source::SpecificFile.new(gem_name)
- set.add src.spec, src
- else
- local = Gem::Source::Local.new
-
- if s = local.find_gem(gem_name, version)
- set.add s, local
+ def in_background what # :nodoc:
+ fork_happened = false
+ if @build_docs_in_background and Process.respond_to?(:fork)
+ begin
+ Process.fork do
+ yield
end
+ fork_happened = true
+ say "#{what} in a background process."
+ rescue NotImplementedError
end
end
-
- if set.empty?
- dep = Gem::Dependency.new gem_name, version
- # HACK Dependency objects should be immutable
- dep.prerelease = true if prerelease
-
- set = find_gems_with_sources(dep)
- set.match_platform!
- end
-
- if set.empty?
- raise Gem::SpecificGemNotFoundException.new(gem_name, version, @errors)
- end
-
- @available = set
+ yield unless fork_happened
end
##
@@ -318,61 +351,30 @@ class Gem::DependencyInstaller
# separately.
def install dep_or_name, version = Gem::Requirement.default
- available_set_for dep_or_name, version
+ request_set = resolve_dependencies dep_or_name, version
@installed_gems = []
- gather_dependencies
-
- # REFACTOR is the last gem always the one that the user requested?
- # This code assumes that but is that actually validated by the code?
-
- last = @gems_to_install.size - 1
- @gems_to_install.each_with_index do |spec, index|
- # REFACTOR more current spec set hardcoding, should be abstracted?
- next if Gem::Specification.include?(spec) and index != last
-
- # TODO: make this sorta_verbose so other users can benefit from it
- say "Installing gem #{spec.full_name}" if Gem.configuration.really_verbose
-
- source = @available.source_for spec
-
- begin
- # REFACTOR make the fetcher to use configurable
- local_gem_path = source.download spec, @cache_dir
- rescue Gem::RemoteFetcher::FetchError
- # TODO I doubt all fetch errors are recoverable, we should at least
- # report the errors probably.
- next if @force
- raise
- end
-
- if @development
- if @dev_shallow
- is_dev = @toplevel_specs.include? spec.full_name
- else
- is_dev = true
- end
- end
+ options = {
+ :bin_dir => @bin_dir,
+ :build_args => @build_args,
+ :env_shebang => @env_shebang,
+ :force => @force,
+ :format_executable => @format_executable,
+ :ignore_dependencies => @ignore_dependencies,
+ :security_policy => @security_policy,
+ :user_install => @user_install,
+ :wrappers => @wrappers,
+ :install_as_default => @install_as_default
+ }
+ options[:install_dir] = @install_dir if @only_install_dir
- inst = Gem::Installer.new local_gem_path,
- :bin_dir => @bin_dir,
- :development => is_dev,
- :env_shebang => @env_shebang,
- :force => @force,
- :format_executable => @format_executable,
- :ignore_dependencies => @ignore_dependencies,
- :install_dir => @install_dir,
- :security_policy => @security_policy,
- :user_install => @user_install,
- :wrappers => @wrappers,
- :build_args => @build_args
-
- spec = inst.install
-
- @installed_gems << spec
+ request_set.install options do |_, installer|
+ @installed_gems << installer.spec if installer
end
+ @installed_gems.sort!
+
# Since this is currently only called for docs, we can be lazy and just say
# it's documentation. Ideally the hook adder could decide whether to be in
# the background or not, and what to call it.
@@ -385,18 +387,34 @@ class Gem::DependencyInstaller
@installed_gems
end
- def in_background what
- fork_happened = false
- if @build_docs_in_background and Process.respond_to?(:fork)
- begin
- Process.fork do
- yield
- end
- fork_happened = true
- say "#{what} in a background process."
- rescue NotImplementedError
- end
+ def install_development_deps # :nodoc:
+ if @development and @dev_shallow then
+ :shallow
+ elsif @development then
+ :all
+ else
+ :none
end
- yield unless fork_happened
end
+
+ def resolve_dependencies dep_or_name, version # :nodoc:
+ as = available_set_for dep_or_name, version
+
+ request_set = as.to_request_set install_development_deps
+ request_set.soft_missing = @force
+
+ installer_set = Gem::DependencyResolver::InstallerSet.new @domain
+ installer_set.always_install.concat request_set.always_install
+ installer_set.ignore_installed = @only_install_dir
+
+ if @ignore_dependencies then
+ installer_set.ignore_dependencies = true
+ request_set.soft_missing = true
+ end
+
+ request_set.resolve Gem::DependencyResolver.compose_sets(as, installer_set)
+
+ request_set
+ end
+
end