2012-11-29 10:52:18 +04:00
|
|
|
require 'rubygems'
|
|
|
|
require 'rubygems/dependency'
|
|
|
|
require 'rubygems/dependency_list'
|
|
|
|
require 'rubygems/installer'
|
2013-11-22 03:27:30 +04:00
|
|
|
require 'rubygems/resolver'
|
2012-11-29 10:52:18 +04:00
|
|
|
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-19 04:34:13 +04:00
|
|
|
##
|
|
|
|
# The set of git gems imported via load_gemdeps.
|
|
|
|
|
|
|
|
attr_reader :git_set # :nodoc:
|
|
|
|
|
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
|
2013-11-19 04:34:13 +04:00
|
|
|
@git_set = nil
|
2013-12-01 03:27:52 +04:00
|
|
|
@install_dir = Gem.dir
|
2013-11-10 21:51:40 +04:00
|
|
|
@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
|
2013-12-01 03:27:52 +04:00
|
|
|
gemdeps = options[:gemdeps]
|
|
|
|
|
|
|
|
@install_dir = options[:install_dir] || Gem.dir
|
|
|
|
|
|
|
|
load_gemdeps gemdeps, options[:without_groups]
|
2013-11-10 21:51:40 +04:00
|
|
|
|
|
|
|
resolve
|
|
|
|
|
2013-11-22 03:27:30 +04:00
|
|
|
if options[:explain]
|
|
|
|
puts "Gems to install:"
|
|
|
|
|
|
|
|
specs.map { |s| s.full_name }.sort.each do |s|
|
|
|
|
puts " #{s}"
|
|
|
|
end
|
|
|
|
else
|
2013-12-01 03:27:52 +04:00
|
|
|
installed = install options, &block
|
|
|
|
|
|
|
|
lockfile = Gem::RequestSet::Lockfile.new self, gemdeps
|
|
|
|
lockfile.write
|
|
|
|
|
|
|
|
installed
|
2013-11-22 03:27:30 +04:00
|
|
|
end
|
2013-11-10 21:51:40 +04:00
|
|
|
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-11-25 23:14:49 +04:00
|
|
|
options[:install_dir] = dir
|
|
|
|
options[:only_install_dir] = true
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-11-25 23:14:49 +04:00
|
|
|
sorted_requests.each do |request|
|
|
|
|
spec = request.spec
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-11-25 23:14:49 +04:00
|
|
|
if existing.find { |s| s.full_name == spec.full_name } then
|
|
|
|
yield request, nil if block_given?
|
2013-09-14 12:59:02 +04:00
|
|
|
next
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
|
|
|
|
2013-11-25 23:14:49 +04:00
|
|
|
spec.install options do |installer|
|
|
|
|
yield request, installer if block_given?
|
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-11-25 23:14:49 +04:00
|
|
|
installed << request
|
2013-09-14 12:59:02 +04:00
|
|
|
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-11-19 04:34:13 +04:00
|
|
|
@git_set = Gem::Resolver::GitSet.new
|
|
|
|
@vendor_set = Gem::Resolver::VendorSet.new
|
2013-10-18 04:41:36 +04:00
|
|
|
|
2013-12-01 03:27:52 +04:00
|
|
|
@git_set.root_dir = @install_dir
|
|
|
|
|
|
|
|
lockfile = Gem::RequestSet::Lockfile.new self, path
|
|
|
|
lockfile.parse
|
|
|
|
|
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-11-22 03:27:30 +04:00
|
|
|
def resolve set = Gem::Resolver::BestSet.new
|
2013-11-10 21:51:40 +04:00
|
|
|
@sets << set
|
2013-11-19 04:34:13 +04:00
|
|
|
@sets << @git_set
|
2013-11-10 21:51:40 +04:00
|
|
|
@sets << @vendor_set
|
2013-10-19 01:56:18 +04:00
|
|
|
|
2013-11-19 04:34:13 +04:00
|
|
|
set = Gem::Resolver.compose_sets(*@sets)
|
2013-10-19 01:56:18 +04:00
|
|
|
|
2013-11-19 04:34:13 +04:00
|
|
|
resolver = Gem::Resolver.new @dependencies, set
|
2013-09-14 12:59:02 +04:00
|
|
|
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
|
2013-11-19 04:34:13 +04:00
|
|
|
resolve Gem::Resolver::CurrentSet.new
|
2013-09-14 12:59:02 +04:00
|
|
|
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
|
2013-11-22 03:27:30 +04:00
|
|
|
raise Gem::DependencyError, "Unresolved dependency found during sorting - #{dep} (requested by #{node.spec.full_name})"
|
2013-09-14 12:59:02 +04:00
|
|
|
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'
|
2013-12-01 03:27:52 +04:00
|
|
|
require 'rubygems/request_set/lockfile'
|