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
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
attr_accessor :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
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
##
|
|
|
|
# Errors fetching gems during resolution.
|
|
|
|
|
|
|
|
attr_reader :errors
|
|
|
|
|
|
|
|
##
|
|
|
|
# Set to true if you want to install only direct development dependencies.
|
|
|
|
|
|
|
|
attr_accessor :development_shallow
|
|
|
|
|
2013-11-19 04:34:13 +04:00
|
|
|
##
|
|
|
|
# The set of git gems imported via load_gemdeps.
|
|
|
|
|
|
|
|
attr_reader :git_set # :nodoc:
|
|
|
|
|
2013-12-18 05:02:58 +04:00
|
|
|
##
|
|
|
|
# When true, dependency resolution is not performed, only the requested gems
|
|
|
|
# are installed.
|
|
|
|
|
|
|
|
attr_accessor :ignore_dependencies
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
attr_reader :install_dir # :nodoc:
|
|
|
|
|
|
|
|
##
|
|
|
|
# If true, allow dependencies to match prerelease gems.
|
|
|
|
|
|
|
|
attr_accessor :prerelease
|
|
|
|
|
2014-02-04 04:48:31 +04:00
|
|
|
##
|
|
|
|
# When false no remote sets are used for resolving gems.
|
|
|
|
|
|
|
|
attr_accessor :remote
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
attr_reader :resolver # :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-12-18 05:02:58 +04:00
|
|
|
@always_install = []
|
2014-09-14 07:30:02 +04:00
|
|
|
@conservative = false
|
2013-12-18 05:02:58 +04:00
|
|
|
@dependency_names = {}
|
|
|
|
@development = false
|
2014-09-14 07:30:02 +04:00
|
|
|
@development_shallow = false
|
|
|
|
@errors = []
|
2013-12-18 05:02:58 +04:00
|
|
|
@git_set = nil
|
|
|
|
@ignore_dependencies = false
|
|
|
|
@install_dir = Gem.dir
|
2014-09-14 07:30:02 +04:00
|
|
|
@prerelease = false
|
2014-02-04 04:48:31 +04:00
|
|
|
@remote = true
|
2013-12-18 05:02:58 +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
|
2015-02-05 16:42:45 +03:00
|
|
|
dep = Gem::Dependency.new name, *reqs
|
2013-11-10 21:51:40 +04:00
|
|
|
@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]
|
2014-09-14 07:30:02 +04:00
|
|
|
requests = install_into dir, false, options, &block
|
|
|
|
return requests
|
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
|
2014-09-14 07:30:02 +04:00
|
|
|
@prerelease = options[:prerelease]
|
2013-09-14 12:59:02 +04:00
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
requests = []
|
2013-09-14 12:59:02 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
requests << inst.install
|
|
|
|
end
|
|
|
|
|
|
|
|
requests
|
|
|
|
ensure
|
|
|
|
raise if $!
|
|
|
|
return requests if options[:gemdeps]
|
|
|
|
|
|
|
|
specs = requests.map do |request|
|
|
|
|
case request
|
|
|
|
when Gem::Resolver::ActivationRequest then
|
|
|
|
request.spec.spec
|
|
|
|
else
|
|
|
|
request
|
|
|
|
end
|
2013-09-14 12:59:02 +04:00
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
require 'rubygems/dependency_installer'
|
|
|
|
inst = Gem::DependencyInstaller.new options
|
|
|
|
inst.installed_gems.replace specs
|
|
|
|
|
|
|
|
Gem.done_installing_hooks.each do |hook|
|
|
|
|
hook.call inst, specs
|
|
|
|
end unless Gem.done_installing_hooks.empty?
|
2013-09-14 12:59:02 +04:00
|
|
|
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
|
2014-09-14 07:30:02 +04:00
|
|
|
@prerelease = options[:prerelease]
|
2014-02-04 04:48:31 +04:00
|
|
|
@remote = options[:domain] != :local
|
2014-09-14 07:30:02 +04:00
|
|
|
@conservative = true if options[:conservative]
|
2013-12-01 03:27:52 +04:00
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
gem_deps_api = load_gemdeps gemdeps, options[:without_groups], true
|
2013-11-10 21:51:40 +04:00
|
|
|
|
|
|
|
resolve
|
|
|
|
|
2013-11-22 03:27:30 +04:00
|
|
|
if options[:explain]
|
|
|
|
puts "Gems to install:"
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
sorted_requests.each do |spec|
|
|
|
|
puts " #{spec.full_name}"
|
2013-11-22 03:27:30 +04:00
|
|
|
end
|
2013-12-08 05:22:39 +04:00
|
|
|
|
|
|
|
if Gem.configuration.really_verbose
|
|
|
|
@resolver.stats.display
|
|
|
|
end
|
2013-11-22 03:27:30 +04:00
|
|
|
else
|
2013-12-01 03:27:52 +04:00
|
|
|
installed = install options, &block
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
if options.fetch :lock, true then
|
|
|
|
lockfile =
|
|
|
|
Gem::RequestSet::Lockfile.new self, gemdeps, gem_deps_api.dependencies
|
|
|
|
lockfile.write
|
|
|
|
end
|
2013-12-01 03:27:52 +04:00
|
|
|
|
|
|
|
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 = {}
|
2013-12-08 05:22:39 +04:00
|
|
|
gem_home, ENV['GEM_HOME'] = ENV['GEM_HOME'], dir
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
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
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
options[:development] = false
|
2013-11-25 23:14:49 +04:00
|
|
|
options[:install_dir] = dir
|
|
|
|
options[:only_install_dir] = true
|
2014-09-14 07:30:02 +04:00
|
|
|
@prerelease = options[:prerelease]
|
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
|
2013-12-08 05:22:39 +04:00
|
|
|
ensure
|
|
|
|
ENV['GEM_HOME'] = gem_home
|
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
|
|
|
##
|
|
|
|
# Load a dependency management file.
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
def load_gemdeps path, without_groups = [], installing = false
|
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
|
|
|
|
|
2015-02-05 16:42:45 +03:00
|
|
|
lock_file = "#{File.expand_path(path)}.lock"
|
|
|
|
begin
|
|
|
|
tokenizer = Gem::RequestSet::Lockfile::Tokenizer.from_file lock_file
|
|
|
|
parser = tokenizer.make_parser self, []
|
|
|
|
parser.parse
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
end
|
2013-12-01 03:27:52 +04:00
|
|
|
|
2013-10-17 06:08:53 +04:00
|
|
|
gf = Gem::RequestSet::GemDependencyAPI.new self, path
|
2014-09-14 07:30:02 +04:00
|
|
|
gf.installing = installing
|
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
|
|
|
|
2014-10-01 12:30:21 +04:00
|
|
|
def pretty_print q # :nodoc:
|
|
|
|
q.group 2, '[RequestSet:', ']' do
|
|
|
|
q.breakable
|
|
|
|
|
|
|
|
if @remote then
|
|
|
|
q.text 'remote'
|
|
|
|
q.breakable
|
|
|
|
end
|
|
|
|
|
|
|
|
if @prerelease then
|
|
|
|
q.text 'prerelease'
|
|
|
|
q.breakable
|
|
|
|
end
|
|
|
|
|
|
|
|
if @development_shallow then
|
|
|
|
q.text 'shallow development'
|
|
|
|
q.breakable
|
|
|
|
elsif @development then
|
|
|
|
q.text 'development'
|
|
|
|
q.breakable
|
|
|
|
end
|
|
|
|
|
|
|
|
if @soft_missing then
|
|
|
|
q.text 'soft missing'
|
|
|
|
end
|
|
|
|
|
|
|
|
q.group 2, '[dependencies:', ']' do
|
|
|
|
q.breakable
|
|
|
|
@dependencies.map do |dep|
|
|
|
|
q.text dep.to_s
|
|
|
|
q.breakable
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
q.breakable
|
|
|
|
q.text 'sets:'
|
|
|
|
|
|
|
|
q.breakable
|
|
|
|
q.pp @sets.map { |set| set.class }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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)
|
2014-02-04 04:48:31 +04:00
|
|
|
set.remote = @remote
|
2014-09-14 07:30:02 +04:00
|
|
|
set.prerelease = @prerelease
|
2013-10-19 01:56:18 +04:00
|
|
|
|
2013-11-19 04:34:13 +04:00
|
|
|
resolver = Gem::Resolver.new @dependencies, set
|
2013-12-18 05:02:58 +04:00
|
|
|
resolver.development = @development
|
2014-09-14 07:30:02 +04:00
|
|
|
resolver.development_shallow = @development_shallow
|
2013-12-18 05:02:58 +04:00
|
|
|
resolver.ignore_dependencies = @ignore_dependencies
|
|
|
|
resolver.soft_missing = @soft_missing
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
if @conservative
|
|
|
|
installed_gems = {}
|
|
|
|
Gem::Specification.find_all do |spec|
|
|
|
|
(installed_gems[spec.name] ||= []) << spec
|
|
|
|
end
|
|
|
|
resolver.skip_gems = installed_gems
|
|
|
|
end
|
|
|
|
|
2013-12-08 05:22:39 +04:00
|
|
|
@resolver = resolver
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
@requests = resolver.resolve
|
2014-09-14 07:30:02 +04:00
|
|
|
|
|
|
|
@errors = set.errors
|
|
|
|
|
|
|
|
@requests
|
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
|
|
|
##
|
|
|
|
# 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
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
match = @requests.find { |r|
|
|
|
|
dep.match? r.spec.name, r.spec.version, @prerelease
|
|
|
|
}
|
|
|
|
|
|
|
|
unless match then
|
|
|
|
next if dep.type == :development and @development_shallow
|
|
|
|
next if @soft_missing
|
|
|
|
raise Gem::DependencyError,
|
|
|
|
"Unresolved dependency found during sorting - #{dep} (requested by #{node.spec.full_name})"
|
|
|
|
end
|
|
|
|
|
2014-12-07 03:53:01 +03:00
|
|
|
yield match
|
2012-11-29 10:52:18 +04:00
|
|
|
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'
|
2015-02-05 16:42:45 +03:00
|
|
|
require 'rubygems/request_set/lockfile/tokenizer'
|