2012-11-29 10:52:18 +04:00
|
|
|
require 'rubygems'
|
|
|
|
require 'rubygems/dependency'
|
|
|
|
require 'rubygems/dependency_resolver'
|
|
|
|
require 'rubygems/dependency_list'
|
|
|
|
require 'rubygems/installer'
|
|
|
|
require 'tsort'
|
|
|
|
|
2013-10-17 02:37:39 +04:00
|
|
|
##
|
|
|
|
# A RequestSet groups a request to activate a set of dependencies.
|
|
|
|
#
|
|
|
|
# nokogiri = Gem::Dependency.new 'nokogiri', '~> 1.6'
|
|
|
|
# pg = Gem::Dependency.new 'pg', '~> 0.14'
|
|
|
|
#
|
|
|
|
# set = Gem::RequestSet.new nokogiri, pg
|
|
|
|
#
|
|
|
|
# requests = set.resolve
|
|
|
|
#
|
|
|
|
# p requests.map { |r| r.full_name }
|
|
|
|
# #=> ["nokogiri-1.6.0", "mini_portile-0.5.1", "pg-0.17.0"]
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
class Gem::RequestSet
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
include TSort
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
|
|
|
# Array of gems to install even if already installed
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
attr_reader :always_install
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
attr_reader :dependencies
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
attr_accessor :development
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
##
|
|
|
|
# Sets used for resolution
|
|
|
|
|
|
|
|
attr_reader :sets # :nodoc:
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
|
|
|
# Treat missing dependencies as silent errors
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
attr_accessor :soft_missing
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-10-18 04:41:36 +04:00
|
|
|
##
|
|
|
|
# The set of vendor gems imported via load_gemdeps.
|
|
|
|
|
|
|
|
attr_reader :vendor_set # :nodoc:
|
|
|
|
|
2013-10-17 02:37:39 +04:00
|
|
|
##
|
|
|
|
# Creates a RequestSet for a list of Gem::Dependency objects, +deps+. You
|
|
|
|
# can then #resolve and #install the resolved list of dependencies.
|
|
|
|
#
|
|
|
|
# nokogiri = Gem::Dependency.new 'nokogiri', '~> 1.6'
|
|
|
|
# pg = Gem::Dependency.new 'pg', '~> 0.14'
|
|
|
|
#
|
|
|
|
# set = Gem::RequestSet.new nokogiri, pg
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
def initialize *deps
|
|
|
|
@dependencies = deps
|
2013-07-10 03:21:36 +04:00
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
@always_install = []
|
|
|
|
@dependency_names = {}
|
|
|
|
@development = false
|
|
|
|
@requests = []
|
|
|
|
@sets = []
|
|
|
|
@soft_missing = false
|
|
|
|
@sorted = nil
|
|
|
|
@specs = nil
|
|
|
|
@vendor_set = nil
|
2013-07-10 03:21:36 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
yield self if block_given?
|
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
|
|
|
# Declare that a gem of name +name+ with +reqs+ requirements is needed.
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
def gem name, *reqs
|
2013-11-10 21:51:40 +04:00
|
|
|
if dep = @dependency_names[name] then
|
|
|
|
dep.requirement.concat reqs
|
|
|
|
else
|
|
|
|
dep = Gem::Dependency.new name, reqs
|
|
|
|
@dependency_names[name] = dep
|
|
|
|
@dependencies << dep
|
|
|
|
end
|
2013-09-14 12:59:02 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Add +deps+ Gem::Dependency objects to the set.
|
|
|
|
|
|
|
|
def import deps
|
2013-10-18 04:41:36 +04:00
|
|
|
@dependencies.concat deps
|
2013-09-14 12:59:02 +04:00
|
|
|
end
|
2013-07-10 03:21:36 +04:00
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
##
|
|
|
|
# Installs gems for this RequestSet using the Gem::Installer +options+.
|
|
|
|
#
|
|
|
|
# If a +block+ is given an activation +request+ and +installer+ are yielded.
|
|
|
|
# The +installer+ will be +nil+ if a gem matching the request was already
|
|
|
|
# installed.
|
|
|
|
|
|
|
|
def install options, &block # :yields: request, installer
|
2013-09-14 12:59:02 +04:00
|
|
|
if dir = options[:install_dir]
|
|
|
|
return install_into dir, false, options, &block
|
2013-09-13 23:58:57 +04:00
|
|
|
end
|
2013-07-10 03:21:36 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
cache_dir = options[:cache_dir] || Gem.dir
|
|
|
|
|
|
|
|
specs = []
|
|
|
|
|
|
|
|
sorted_requests.each do |req|
|
2013-10-16 04:14:16 +04:00
|
|
|
if req.installed? then
|
|
|
|
req.spec.spec.build_extensions
|
|
|
|
|
|
|
|
if @always_install.none? { |spec| spec == req.spec.spec } then
|
|
|
|
yield req, nil if block_given?
|
|
|
|
next
|
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
path = req.download cache_dir
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
inst = Gem::Installer.new path, options
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
yield req, inst if block_given?
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
specs << inst.install
|
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
specs
|
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
##
|
|
|
|
# Installs from the gem dependencies files in the +:gemdeps+ option in
|
|
|
|
# +options+, yielding to the +block+ as in #install.
|
|
|
|
#
|
|
|
|
# If +:without_groups+ is given in the +options+, those groups in the gem
|
|
|
|
# dependencies file are not used. See Gem::Installer for other +options+.
|
|
|
|
|
|
|
|
def install_from_gemdeps options, &block
|
|
|
|
load_gemdeps options[:gemdeps], options[:without_groups]
|
|
|
|
|
|
|
|
resolve
|
|
|
|
|
|
|
|
install options, &block
|
|
|
|
end
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
def install_into dir, force = true, options = {}
|
|
|
|
existing = force ? [] : specs_in(dir)
|
|
|
|
existing.delete_if { |s| @always_install.include? s }
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
dir = File.expand_path dir
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
installed = []
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
sorted_requests.each do |req|
|
|
|
|
if existing.find { |s| s.full_name == req.spec.full_name }
|
|
|
|
yield req, nil if block_given?
|
|
|
|
next
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
path = req.download(dir)
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
unless path then # already installed
|
|
|
|
yield req, nil if block_given?
|
|
|
|
next
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
options[:install_dir] = dir
|
|
|
|
options[:only_install_dir] = true
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
inst = Gem::Installer.new path, options
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
yield req, inst if block_given?
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
inst.install
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
installed << req
|
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
installed
|
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
|
|
|
# Load a dependency management file.
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
def load_gemdeps path, without_groups = []
|
2013-10-18 04:41:36 +04:00
|
|
|
@vendor_set = Gem::DependencyResolver::VendorSet.new
|
|
|
|
|
2013-10-17 06:08:53 +04:00
|
|
|
gf = Gem::RequestSet::GemDependencyAPI.new self, path
|
2013-11-10 21:51:40 +04:00
|
|
|
gf.without_groups = without_groups if without_groups
|
2013-09-14 12:59:02 +04:00
|
|
|
gf.load
|
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
|
|
|
# Resolve the requested dependencies and return an Array of Specification
|
|
|
|
# objects to be activated.
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-10-19 01:56:18 +04:00
|
|
|
def resolve set = Gem::DependencyResolver::IndexSet.new
|
2013-11-10 21:51:40 +04:00
|
|
|
@sets << set
|
|
|
|
@sets << @vendor_set
|
2013-10-19 01:56:18 +04:00
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
set = Gem::DependencyResolver.compose_sets(*@sets)
|
2013-10-19 01:56:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
resolver = Gem::DependencyResolver.new @dependencies, set
|
|
|
|
resolver.development = @development
|
|
|
|
resolver.soft_missing = @soft_missing
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
@requests = resolver.resolve
|
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
|
|
|
# Resolve the requested dependencies against the gems available via Gem.path
|
|
|
|
# and return an Array of Specification objects to be activated.
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
def resolve_current
|
|
|
|
resolve Gem::DependencyResolver::CurrentSet.new
|
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
def sorted_requests
|
|
|
|
@sorted ||= strongly_connected_components.flatten
|
|
|
|
end
|
2013-07-10 03:21:36 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
def specs
|
|
|
|
@specs ||= @requests.map { |r| r.full_spec }
|
|
|
|
end
|
|
|
|
|
|
|
|
def specs_in dir
|
|
|
|
Dir["#{dir}/specifications/*.gemspec"].map do |g|
|
|
|
|
Gem::Specification.load g
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def tsort_each_node &block # :nodoc:
|
|
|
|
@requests.each(&block)
|
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
def tsort_each_child node # :nodoc:
|
|
|
|
node.spec.dependencies.each do |dep|
|
|
|
|
next if dep.type == :development and not @development
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
match = @requests.find { |r| dep.match? r.spec.name, r.spec.version }
|
|
|
|
if match
|
|
|
|
begin
|
|
|
|
yield match
|
|
|
|
rescue TSort::Cyclic
|
|
|
|
end
|
|
|
|
else
|
|
|
|
unless @soft_missing
|
|
|
|
raise Gem::DependencyError, "Unresolved depedency found during sorting - #{dep}"
|
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-09-14 12:59:02 +04:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
2013-09-14 12:59:02 +04:00
|
|
|
|
|
|
|
require 'rubygems/request_set/gem_dependency_api'
|