Class: Gem::RequestSet
- Inherits:
-
Object
- Object
- Gem::RequestSet
- Includes:
- TSort
- Defined in:
- lib/rubygems/request_set.rb
Overview
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"]
Defined Under Namespace
Classes: GemDependencyAPI, Lockfile
Constant Summary collapse
- GemDepedencyAPI =
TODO: remove this typo name at RubyGems 3.0
self
Instance Attribute Summary collapse
-
#always_install ⇒ Object
Array of gems to install even if already installed.
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
-
#development ⇒ Object
Returns the value of attribute development.
-
#development_shallow ⇒ Object
Set to true if you want to install only direct development dependencies.
-
#errors ⇒ Object
readonly
Errors fetching gems during resolution.
-
#git_set ⇒ Object
readonly
The set of git gems imported via load_gemdeps.
-
#ignore_dependencies ⇒ Object
When true, dependency resolution is not performed, only the requested gems are installed.
-
#install_dir ⇒ Object
readonly
:nodoc:.
-
#prerelease ⇒ Object
If true, allow dependencies to match prerelease gems.
-
#remote ⇒ Object
When false no remote sets are used for resolving gems.
-
#resolver ⇒ Object
readonly
:nodoc:.
-
#sets ⇒ Object
readonly
Sets used for resolution.
-
#soft_missing ⇒ Object
Treat missing dependencies as silent errors.
-
#source_set ⇒ Object
readonly
The set of source gems imported via load_gemdeps.
-
#vendor_set ⇒ Object
readonly
The set of vendor gems imported via load_gemdeps.
Instance Method Summary collapse
-
#gem(name, *reqs) ⇒ Object
Declare that a gem of name
name
withreqs
requirements is needed. -
#import(deps) ⇒ Object
Add
deps
Gem::Dependency objects to the set. -
#initialize(*deps) {|_self| ... } ⇒ RequestSet
constructor
Creates a RequestSet for a list of Gem::Dependency objects,
deps
. -
#install(options, &block) ⇒ Object
Installs gems for this RequestSet using the Gem::Installer
options
. -
#install_from_gemdeps(options, &block) ⇒ Object
Installs from the gem dependencies files in the
:gemdeps
option inoptions
, yielding to theblock
as in #install. - #install_into(dir, force = true, options = {}) ⇒ Object
-
#load_gemdeps(path, without_groups = [], installing = false) ⇒ Object
Load a dependency management file.
-
#pretty_print(q) ⇒ Object
:nodoc:.
-
#resolve(set = Gem::Resolver::BestSet.new) ⇒ Object
Resolve the requested dependencies and return an Array of Specification objects to be activated.
-
#resolve_current ⇒ Object
Resolve the requested dependencies against the gems available via Gem.path and return an Array of Specification objects to be activated.
- #sorted_requests ⇒ Object
- #specs ⇒ Object
- #specs_in(dir) ⇒ Object
-
#tsort_each_child(node) ⇒ Object
:nodoc:.
-
#tsort_each_node(&block) ⇒ Object
:nodoc:.
Constructor Details
#initialize(*deps) {|_self| ... } ⇒ RequestSet
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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rubygems/request_set.rb', line 94 def initialize *deps @dependencies = deps @always_install = [] @conservative = false @dependency_names = {} @development = false @development_shallow = false @errors = [] @git_set = nil @ignore_dependencies = false @install_dir = Gem.dir @prerelease = false @remote = true @requests = [] @sets = [] @soft_missing = false @sorted = nil @specs = nil @vendor_set = nil @source_set = nil yield self if block_given? end |
Instance Attribute Details
#always_install ⇒ Object
Array of gems to install even if already installed
24 25 26 |
# File 'lib/rubygems/request_set.rb', line 24 def always_install @always_install end |
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
26 27 28 |
# File 'lib/rubygems/request_set.rb', line 26 def dependencies @dependencies end |
#development ⇒ Object
Returns the value of attribute development.
28 29 30 |
# File 'lib/rubygems/request_set.rb', line 28 def development @development end |
#development_shallow ⇒ Object
Set to true if you want to install only direct development dependencies.
38 39 40 |
# File 'lib/rubygems/request_set.rb', line 38 def development_shallow @development_shallow end |
#errors ⇒ Object (readonly)
Errors fetching gems during resolution.
33 34 35 |
# File 'lib/rubygems/request_set.rb', line 33 def errors @errors end |
#git_set ⇒ Object (readonly)
The set of git gems imported via load_gemdeps.
43 44 45 |
# File 'lib/rubygems/request_set.rb', line 43 def git_set @git_set end |
#ignore_dependencies ⇒ Object
When true, dependency resolution is not performed, only the requested gems are installed.
49 50 51 |
# File 'lib/rubygems/request_set.rb', line 49 def ignore_dependencies @ignore_dependencies end |
#install_dir ⇒ Object (readonly)
:nodoc:
51 52 53 |
# File 'lib/rubygems/request_set.rb', line 51 def install_dir @install_dir end |
#prerelease ⇒ Object
If true, allow dependencies to match prerelease gems.
56 57 58 |
# File 'lib/rubygems/request_set.rb', line 56 def prerelease @prerelease end |
#remote ⇒ Object
When false no remote sets are used for resolving gems.
61 62 63 |
# File 'lib/rubygems/request_set.rb', line 61 def remote @remote end |
#resolver ⇒ Object (readonly)
:nodoc:
63 64 65 |
# File 'lib/rubygems/request_set.rb', line 63 def resolver @resolver end |
#sets ⇒ Object (readonly)
Sets used for resolution
68 69 70 |
# File 'lib/rubygems/request_set.rb', line 68 def sets @sets end |
#soft_missing ⇒ Object
Treat missing dependencies as silent errors
73 74 75 |
# File 'lib/rubygems/request_set.rb', line 73 def soft_missing @soft_missing end |
#source_set ⇒ Object (readonly)
The set of source gems imported via load_gemdeps.
83 84 85 |
# File 'lib/rubygems/request_set.rb', line 83 def source_set @source_set end |
#vendor_set ⇒ Object (readonly)
The set of vendor gems imported via load_gemdeps.
78 79 80 |
# File 'lib/rubygems/request_set.rb', line 78 def vendor_set @vendor_set end |
Instance Method Details
#gem(name, *reqs) ⇒ Object
Declare that a gem of name name
with reqs
requirements is needed.
122 123 124 125 126 127 128 129 130 |
# File 'lib/rubygems/request_set.rb', line 122 def gem name, *reqs if dep = @dependency_names[name] then dep.requirement.concat reqs else dep = Gem::Dependency.new name, *reqs @dependency_names[name] = dep @dependencies << dep end end |
#import(deps) ⇒ Object
Add deps
Gem::Dependency objects to the set.
135 136 137 |
# File 'lib/rubygems/request_set.rb', line 135 def import deps @dependencies.concat deps end |
#install(options, &block) ⇒ Object
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.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/rubygems/request_set.rb', line 146 def install , &block # :yields: request, installer if dir = [:install_dir] requests = install_into dir, false, , &block return requests end @prerelease = [:prerelease] requests = [] sorted_requests.each do |req| 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 end spec = req.spec.install do |installer| yield req, installer if block_given? end requests << spec end return requests if [:gemdeps] 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 inst.installed_gems.replace specs Gem.done_installing_hooks.each do |hook| hook.call inst, specs end unless Gem.done_installing_hooks.empty? requests end |
#install_from_gemdeps(options, &block) ⇒ Object
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
.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/rubygems/request_set.rb', line 202 def install_from_gemdeps , &block gemdeps = [:gemdeps] @install_dir = [:install_dir] || Gem.dir @prerelease = [:prerelease] @remote = [:domain] != :local @conservative = true if [:conservative] gem_deps_api = load_gemdeps gemdeps, [:without_groups], true resolve if [:explain] puts "Gems to install:" sorted_requests.each do |spec| puts " #{spec.full_name}" end if Gem.configuration.really_verbose @resolver.stats.display end else installed = install , &block if .fetch :lock, true then lockfile = Gem::RequestSet::Lockfile.build self, gemdeps, gem_deps_api.dependencies lockfile.write end installed end end |
#install_into(dir, force = true, options = {}) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/rubygems/request_set.rb', line 237 def install_into dir, force = true, = {} gem_home, ENV['GEM_HOME'] = ENV['GEM_HOME'], dir existing = force ? [] : specs_in(dir) existing.delete_if { |s| @always_install.include? s } dir = File. dir installed = [] [:development] = false [:install_dir] = dir [:only_install_dir] = true @prerelease = [:prerelease] sorted_requests.each do |request| spec = request.spec if existing.find { |s| s.full_name == spec.full_name } then yield request, nil if block_given? next end spec.install do |installer| yield request, installer if block_given? end installed << request end installed ensure ENV['GEM_HOME'] = gem_home end |
#load_gemdeps(path, without_groups = [], installing = false) ⇒ Object
Load a dependency management file.
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/rubygems/request_set.rb', line 275 def load_gemdeps path, without_groups = [], installing = false @git_set = Gem::Resolver::GitSet.new @vendor_set = Gem::Resolver::VendorSet.new @source_set = Gem::Resolver::SourceSet.new @git_set.root_dir = @install_dir lock_file = "#{File.(path)}.lock".dup.untaint begin tokenizer = Gem::RequestSet::Lockfile::Tokenizer.from_file lock_file parser = tokenizer.make_parser self, [] parser.parse rescue Errno::ENOENT end gf = Gem::RequestSet::GemDependencyAPI.new self, path gf.installing = installing gf.without_groups = without_groups if without_groups gf.load end |
#pretty_print(q) ⇒ Object
:nodoc:
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/rubygems/request_set.rb', line 296 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 |
#resolve(set = Gem::Resolver::BestSet.new) ⇒ Object
Resolve the requested dependencies and return an Array of Specification objects to be activated.
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/rubygems/request_set.rb', line 342 def resolve set = Gem::Resolver::BestSet.new @sets << set @sets << @git_set @sets << @vendor_set @sets << @source_set set = Gem::Resolver.compose_sets(*@sets) set.remote = @remote set.prerelease = @prerelease resolver = Gem::Resolver.new @dependencies, set resolver.development = @development resolver.development_shallow = @development_shallow resolver.ignore_dependencies = @ignore_dependencies resolver.soft_missing = @soft_missing if @conservative installed_gems = {} Gem::Specification.find_all do |spec| (installed_gems[spec.name] ||= []) << spec end resolver.skip_gems = installed_gems end @resolver = resolver @requests = resolver.resolve @errors = set.errors @requests end |
#resolve_current ⇒ Object
Resolve the requested dependencies against the gems available via Gem.path and return an Array of Specification objects to be activated.
379 380 381 |
# File 'lib/rubygems/request_set.rb', line 379 def resolve_current resolve Gem::Resolver::CurrentSet.new end |
#sorted_requests ⇒ Object
383 384 385 |
# File 'lib/rubygems/request_set.rb', line 383 def sorted_requests @sorted ||= strongly_connected_components.flatten end |
#specs ⇒ Object
387 388 389 |
# File 'lib/rubygems/request_set.rb', line 387 def specs @specs ||= @requests.map { |r| r.full_spec } end |
#specs_in(dir) ⇒ Object
391 392 393 394 395 |
# File 'lib/rubygems/request_set.rb', line 391 def specs_in dir Dir["#{dir}/specifications/*.gemspec"].map do |g| Gem::Specification.load g end end |
#tsort_each_child(node) ⇒ Object
:nodoc:
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/rubygems/request_set.rb', line 401 def tsort_each_child node # :nodoc: node.spec.dependencies.each do |dep| next if dep.type == :development and not @development 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 yield match end end |
#tsort_each_node(&block) ⇒ Object
:nodoc:
397 398 399 |
# File 'lib/rubygems/request_set.rb', line 397 def tsort_each_node &block # :nodoc: @requests.each(&block) end |