2016-02-01 07:43:26 -05:00
# frozen_string_literal: true
2012-11-29 01:52:18 -05:00
require 'tsort'
2013-10-16 18: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 04:59:02 -04:00
class Gem :: RequestSet
2012-11-29 01:52:18 -05:00
2013-09-14 04:59:02 -04:00
include TSort
2012-11-29 01:52:18 -05:00
2013-09-14 04:59:02 -04:00
##
# Array of gems to install even if already installed
2012-11-29 01:52:18 -05:00
2014-09-13 23:30:02 -04:00
attr_accessor :always_install
2012-11-29 01:52:18 -05:00
2013-09-14 04:59:02 -04:00
attr_reader :dependencies
2012-11-29 01:52:18 -05:00
2013-09-14 04:59:02 -04:00
attr_accessor :development
2012-11-29 01:52:18 -05:00
2014-09-13 23: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-18 19:34:13 -05:00
##
# The set of git gems imported via load_gemdeps.
attr_reader :git_set # :nodoc:
2013-12-17 20:02:58 -05:00
##
# When true, dependency resolution is not performed, only the requested gems
# are installed.
attr_accessor :ignore_dependencies
2014-09-13 23:30:02 -04:00
attr_reader :install_dir # :nodoc:
##
# If true, allow dependencies to match prerelease gems.
attr_accessor :prerelease
2014-02-03 19:48:31 -05:00
##
# When false no remote sets are used for resolving gems.
attr_accessor :remote
2014-09-13 23:30:02 -04:00
attr_reader :resolver # :nodoc:
2013-11-10 12:51:40 -05:00
##
# Sets used for resolution
attr_reader :sets # :nodoc:
2013-09-14 04:59:02 -04:00
##
# Treat missing dependencies as silent errors
2012-11-29 01:52:18 -05:00
2013-09-14 04:59:02 -04:00
attr_accessor :soft_missing
2012-11-29 01:52:18 -05:00
2013-10-17 20:41:36 -04:00
##
# The set of vendor gems imported via load_gemdeps.
attr_reader :vendor_set # :nodoc:
2016-02-01 07:43:26 -05:00
##
# The set of source gems imported via load_gemdeps.
attr_reader :source_set
2013-10-16 18: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 04:59:02 -04:00
def initialize * deps
@dependencies = deps
2013-07-09 19:21:36 -04:00
2013-12-17 20:02:58 -05:00
@always_install = [ ]
2014-09-13 23:30:02 -04:00
@conservative = false
2013-12-17 20:02:58 -05:00
@dependency_names = { }
@development = false
2014-09-13 23:30:02 -04:00
@development_shallow = false
@errors = [ ]
2013-12-17 20:02:58 -05:00
@git_set = nil
@ignore_dependencies = false
@install_dir = Gem . dir
2014-09-13 23:30:02 -04:00
@prerelease = false
2014-02-03 19:48:31 -05:00
@remote = true
2013-12-17 20:02:58 -05:00
@requests = [ ]
@sets = [ ]
@soft_missing = false
@sorted = nil
@specs = nil
@vendor_set = nil
2016-02-01 07:43:26 -05:00
@source_set = nil
2013-07-09 19:21:36 -04:00
2013-09-14 04:59:02 -04:00
yield self if block_given?
end
2012-11-29 01:52:18 -05:00
2013-09-14 04:59:02 -04:00
##
# Declare that a gem of name +name+ with +reqs+ requirements is needed.
2012-11-29 01:52:18 -05:00
2013-09-14 04:59:02 -04:00
def gem name , * reqs
2013-11-10 12:51:40 -05:00
if dep = @dependency_names [ name ] then
dep . requirement . concat reqs
else
2015-02-05 08:42:45 -05:00
dep = Gem :: Dependency . new name , * reqs
2013-11-10 12:51:40 -05:00
@dependency_names [ name ] = dep
@dependencies << dep
end
2013-09-14 04:59:02 -04:00
end
##
# Add +deps+ Gem::Dependency objects to the set.
def import deps
2013-10-17 20:41:36 -04:00
@dependencies . concat deps
2013-09-14 04:59:02 -04:00
end
2013-07-09 19:21:36 -04:00
2013-11-10 12:51:40 -05: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 04:59:02 -04:00
if dir = options [ :install_dir ]
2014-09-13 23:30:02 -04:00
requests = install_into dir , false , options , & block
return requests
2013-09-13 15:58:57 -04:00
end
2013-07-09 19:21:36 -04:00
2014-09-13 23:30:02 -04:00
@prerelease = options [ :prerelease ]
2013-09-14 04:59:02 -04:00
2014-09-13 23:30:02 -04:00
requests = [ ]
2013-09-14 04:59:02 -04:00
sorted_requests . each do | req |
2013-10-15 20: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 01:52:18 -05:00
end
2017-10-07 21:32:18 -04:00
spec =
begin
req . spec . install options do | installer |
yield req , installer if block_given?
end
rescue Gem :: RuntimeRequirementNotMetError = > e
recent_match = req . spec . set . find_all ( req . request ) . sort_by ( & :version ) . reverse_each . find do | s |
s = s . spec
2018-05-17 21:39:13 -04:00
s . required_ruby_version . satisfied_by? ( Gem . ruby_version ) &&
s . required_rubygems_version . satisfied_by? ( Gem . rubygems_version ) &&
Gem :: Platform . installable? ( s )
2017-10-07 21:32:18 -04:00
end
if recent_match
suggestion = " The last version of #{ req . request } to support your Ruby & RubyGems was #{ recent_match . version } . Try installing it with `gem install #{ recent_match . name } -v #{ recent_match . version } ` "
suggestion += " and then running the current command again " unless @always_install . include? ( req . spec . spec )
else
suggestion = " There are no versions of #{ req . request } compatible with your Ruby & RubyGems "
suggestion += " . Maybe try installing an older version of the gem you're looking for? " unless @always_install . include? ( req . spec . spec )
end
e . suggestion = suggestion
raise
end
2012-11-29 01:52:18 -05:00
2016-02-01 07:43:26 -05:00
requests << spec
2014-09-13 23:30:02 -04:00
end
return requests if options [ :gemdeps ]
2018-05-30 09:01:35 -04:00
install_hooks requests , options
2015-07-01 17:50:14 -04:00
requests
2013-09-14 04:59:02 -04:00
end
2012-11-29 01:52:18 -05:00
2013-11-10 12:51:40 -05: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-11-30 18:27:52 -05:00
gemdeps = options [ :gemdeps ]
@install_dir = options [ :install_dir ] || Gem . dir
2014-09-13 23:30:02 -04:00
@prerelease = options [ :prerelease ]
2014-02-03 19:48:31 -05:00
@remote = options [ :domain ] != :local
2014-09-13 23:30:02 -04:00
@conservative = true if options [ :conservative ]
2013-11-30 18:27:52 -05:00
2014-09-13 23:30:02 -04:00
gem_deps_api = load_gemdeps gemdeps , options [ :without_groups ] , true
2013-11-10 12:51:40 -05:00
resolve
2013-11-21 18:27:30 -05:00
if options [ :explain ]
puts " Gems to install: "
2014-09-13 23:30:02 -04:00
sorted_requests . each do | spec |
puts " #{ spec . full_name } "
2013-11-21 18:27:30 -05:00
end
2013-12-07 20:22:39 -05:00
if Gem . configuration . really_verbose
@resolver . stats . display
end
2013-11-21 18:27:30 -05:00
else
2013-11-30 18:27:52 -05:00
installed = install options , & block
2014-09-13 23:30:02 -04:00
if options . fetch :lock , true then
lockfile =
2015-02-27 08:00:45 -05:00
Gem :: RequestSet :: Lockfile . build self , gemdeps , gem_deps_api . dependencies
2014-09-13 23:30:02 -04:00
lockfile . write
end
2013-11-30 18:27:52 -05:00
installed
2013-11-21 18:27:30 -05:00
end
2013-11-10 12:51:40 -05:00
end
2013-09-14 04:59:02 -04:00
def install_into dir , force = true , options = { }
2013-12-07 20:22:39 -05:00
gem_home , ENV [ 'GEM_HOME' ] = ENV [ 'GEM_HOME' ] , dir
2013-09-14 04:59:02 -04:00
existing = force ? [ ] : specs_in ( dir )
existing . delete_if { | s | @always_install . include? s }
2012-11-29 01:52:18 -05:00
2013-09-14 04:59:02 -04:00
dir = File . expand_path dir
2012-11-29 01:52:18 -05:00
2013-09-14 04:59:02 -04:00
installed = [ ]
2012-11-29 01:52:18 -05:00
2014-09-13 23:30:02 -04:00
options [ :development ] = false
2013-11-25 14:14:49 -05:00
options [ :install_dir ] = dir
options [ :only_install_dir ] = true
2014-09-13 23:30:02 -04:00
@prerelease = options [ :prerelease ]
2012-11-29 01:52:18 -05:00
2013-11-25 14:14:49 -05:00
sorted_requests . each do | request |
spec = request . spec
2012-11-29 01:52:18 -05:00
2013-11-25 14:14:49 -05:00
if existing . find { | s | s . full_name == spec . full_name } then
yield request , nil if block_given?
2013-09-14 04:59:02 -04:00
next
2012-11-29 01:52:18 -05:00
end
2013-11-25 14:14:49 -05:00
spec . install options do | installer |
yield request , installer if block_given?
end
2012-11-29 01:52:18 -05:00
2013-11-25 14:14:49 -05:00
installed << request
2013-09-14 04:59:02 -04:00
end
2012-11-29 01:52:18 -05:00
2018-05-30 09:01:35 -04:00
install_hooks installed , options
2013-09-14 04:59:02 -04:00
installed
2013-12-07 20:22:39 -05:00
ensure
ENV [ 'GEM_HOME' ] = gem_home
2013-09-14 04:59:02 -04:00
end
2012-11-29 01:52:18 -05:00
2018-05-30 09:01:35 -04:00
##
# Call hooks on installed gems
def install_hooks requests , options
specs = requests . map do | request |
case request
when Gem :: Resolver :: ActivationRequest then
request . spec . spec
else
request
end
end
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?
end
2013-09-14 04:59:02 -04:00
##
# Load a dependency management file.
2012-11-29 01:52:18 -05:00
2014-09-13 23:30:02 -04:00
def load_gemdeps path , without_groups = [ ] , installing = false
2013-11-18 19:34:13 -05:00
@git_set = Gem :: Resolver :: GitSet . new
@vendor_set = Gem :: Resolver :: VendorSet . new
2016-02-01 07:43:26 -05:00
@source_set = Gem :: Resolver :: SourceSet . new
2013-10-17 20:41:36 -04:00
2013-11-30 18:27:52 -05:00
@git_set . root_dir = @install_dir
2016-02-01 07:43:26 -05:00
lock_file = " #{ File . expand_path ( path ) } .lock " . dup . untaint
2015-02-05 08:42:45 -05:00
begin
tokenizer = Gem :: RequestSet :: Lockfile :: Tokenizer . from_file lock_file
parser = tokenizer . make_parser self , [ ]
parser . parse
rescue Errno :: ENOENT
end
2013-11-30 18:27:52 -05:00
2013-10-16 22:08:53 -04:00
gf = Gem :: RequestSet :: GemDependencyAPI . new self , path
2014-09-13 23:30:02 -04:00
gf . installing = installing
2013-11-10 12:51:40 -05:00
gf . without_groups = without_groups if without_groups
2013-09-14 04:59:02 -04:00
gf . load
end
2012-11-29 01:52:18 -05:00
2014-10-01 04: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 04:59:02 -04:00
##
# Resolve the requested dependencies and return an Array of Specification
# objects to be activated.
2012-11-29 01:52:18 -05:00
2013-11-21 18:27:30 -05:00
def resolve set = Gem :: Resolver :: BestSet . new
2013-11-10 12:51:40 -05:00
@sets << set
2013-11-18 19:34:13 -05:00
@sets << @git_set
2013-11-10 12:51:40 -05:00
@sets << @vendor_set
2016-02-01 07:43:26 -05:00
@sets << @source_set
2013-10-18 17:56:18 -04:00
2013-11-18 19:34:13 -05:00
set = Gem :: Resolver . compose_sets ( * @sets )
2014-02-03 19:48:31 -05:00
set . remote = @remote
2014-09-13 23:30:02 -04:00
set . prerelease = @prerelease
2013-10-18 17:56:18 -04:00
2013-11-18 19:34:13 -05:00
resolver = Gem :: Resolver . new @dependencies , set
2013-12-17 20:02:58 -05:00
resolver . development = @development
2014-09-13 23:30:02 -04:00
resolver . development_shallow = @development_shallow
2013-12-17 20:02:58 -05:00
resolver . ignore_dependencies = @ignore_dependencies
resolver . soft_missing = @soft_missing
2012-11-29 01:52:18 -05:00
2014-09-13 23: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-07 20:22:39 -05:00
@resolver = resolver
2013-09-14 04:59:02 -04:00
@requests = resolver . resolve
2014-09-13 23:30:02 -04:00
@errors = set . errors
@requests
2013-09-14 04:59:02 -04:00
end
2012-11-29 01:52:18 -05:00
2013-09-14 04: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 01:52:18 -05:00
2013-09-14 04:59:02 -04:00
def resolve_current
2013-11-18 19:34:13 -05:00
resolve Gem :: Resolver :: CurrentSet . new
2013-09-14 04:59:02 -04:00
end
2012-11-29 01:52:18 -05:00
2013-09-14 04:59:02 -04:00
def sorted_requests
@sorted || = strongly_connected_components . flatten
end
2013-07-09 19:21:36 -04:00
2013-09-14 04: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 01:52:18 -05:00
2013-09-14 04: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 01:52:18 -05:00
2014-09-13 23: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-06 19:53:01 -05:00
yield match
2012-11-29 01:52:18 -05:00
end
end
2013-09-14 04:59:02 -04:00
2012-11-29 01:52:18 -05:00
end
2013-09-14 04:59:02 -04:00
require 'rubygems/request_set/gem_dependency_api'
2013-11-30 18:27:52 -05:00
require 'rubygems/request_set/lockfile'
2015-02-05 08:42:45 -05:00
require 'rubygems/request_set/lockfile/tokenizer'