Class: Gem::RequestSet

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Yields:

  • (_self)

Yield Parameters:


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rubygems/request_set.rb', line 93

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_installObject

Array of gems to install even if already installed


23
24
25
# File 'lib/rubygems/request_set.rb', line 23

def always_install
  @always_install
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies


25
26
27
# File 'lib/rubygems/request_set.rb', line 25

def dependencies
  @dependencies
end

#developmentObject

Returns the value of attribute development


27
28
29
# File 'lib/rubygems/request_set.rb', line 27

def development
  @development
end

#development_shallowObject

Set to true if you want to install only direct development dependencies.


37
38
39
# File 'lib/rubygems/request_set.rb', line 37

def development_shallow
  @development_shallow
end

#errorsObject (readonly)

Errors fetching gems during resolution.


32
33
34
# File 'lib/rubygems/request_set.rb', line 32

def errors
  @errors
end

#git_setObject (readonly)

The set of git gems imported via load_gemdeps.


42
43
44
# File 'lib/rubygems/request_set.rb', line 42

def git_set
  @git_set
end

#ignore_dependenciesObject

When true, dependency resolution is not performed, only the requested gems are installed.


48
49
50
# File 'lib/rubygems/request_set.rb', line 48

def ignore_dependencies
  @ignore_dependencies
end

#install_dirObject (readonly)

:nodoc:


50
51
52
# File 'lib/rubygems/request_set.rb', line 50

def install_dir
  @install_dir
end

#prereleaseObject

If true, allow dependencies to match prerelease gems.


55
56
57
# File 'lib/rubygems/request_set.rb', line 55

def prerelease
  @prerelease
end

#remoteObject

When false no remote sets are used for resolving gems.


60
61
62
# File 'lib/rubygems/request_set.rb', line 60

def remote
  @remote
end

#resolverObject (readonly)

:nodoc:


62
63
64
# File 'lib/rubygems/request_set.rb', line 62

def resolver
  @resolver
end

#setsObject (readonly)

Sets used for resolution


67
68
69
# File 'lib/rubygems/request_set.rb', line 67

def sets
  @sets
end

#soft_missingObject

Treat missing dependencies as silent errors


72
73
74
# File 'lib/rubygems/request_set.rb', line 72

def soft_missing
  @soft_missing
end

#source_setObject (readonly)

The set of source gems imported via load_gemdeps.


82
83
84
# File 'lib/rubygems/request_set.rb', line 82

def source_set
  @source_set
end

#vendor_setObject (readonly)

The set of vendor gems imported via load_gemdeps.


77
78
79
# File 'lib/rubygems/request_set.rb', line 77

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.


121
122
123
124
125
126
127
128
129
# File 'lib/rubygems/request_set.rb', line 121

def gem(name, *reqs)
  if dep = @dependency_names[name]
    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.


134
135
136
# File 'lib/rubygems/request_set.rb', line 134

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.


145
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/rubygems/request_set.rb', line 145

def install(options, &block) # :yields: request, installer
  if dir = options[:install_dir]
    requests = install_into dir, false, options, &block
    return requests
  end

  @prerelease = options[:prerelease]

  requests = []
  download_queue = Queue.new

  # Create a thread-safe list of gems to download
  sorted_requests.each do |req|
    download_queue << req
  end

  # Create N threads in a pool, have them download all the gems
  threads = Gem.configuration.concurrent_downloads.times.map do
    # When a thread pops this item, it knows to stop running. The symbol
    # is queued here so that there will be one symbol per thread.
    download_queue << :stop

    Thread.new do
      # The pop method will block waiting for items, so the only way
      # to stop a thread from running is to provide a final item that
      # means the thread should stop.
      while req = download_queue.pop
        break if req == :stop
        req.spec.download options unless req.installed?
      end
    end
  end

  # Wait for all the downloads to finish before continuing
  threads.each(&:value)

  # Install requested gems after they have been downloaded
  sorted_requests.each do |req|
    if req.installed?
      req.spec.spec.build_extensions

      if @always_install.none? {|spec| spec == req.spec.spec }
        yield req, nil if block_given?
        next
      end
    end

    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
          s.required_ruby_version.satisfied_by?(Gem.ruby_version) &&
            s.required_rubygems_version.satisfied_by?(Gem.rubygems_version) &&
            Gem::Platform.installable?(s)
        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

    requests << spec
  end

  return requests if options[:gemdeps]

  install_hooks requests, options

  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.


232
233
234
235
236
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
# File 'lib/rubygems/request_set.rb', line 232

def install_from_gemdeps(options, &block)
  gemdeps = options[:gemdeps]

  @install_dir = options[:install_dir] || Gem.dir
  @prerelease  = options[:prerelease]
  @remote      = options[:domain] != :local
  @conservative = true if options[:conservative]

  gem_deps_api = load_gemdeps gemdeps, options[:without_groups], true

  resolve

  if options[: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 options, &block

    if options.fetch :lock, true
      lockfile =
        Gem::RequestSet::Lockfile.build self, gemdeps, gem_deps_api.dependencies
      lockfile.write
    end

    installed
  end
end

#install_hooks(requests, options) ⇒ Object

Call hooks on installed gems


307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/rubygems/request_set.rb', line 307

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

#install_into(dir, force = true, options = {}) ⇒ Object


267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/rubygems/request_set.rb', line 267

def install_into(dir, force = true, options = {})
  gem_home, ENV['GEM_HOME'] = ENV['GEM_HOME'], dir

  existing = force ? [] : specs_in(dir)
  existing.delete_if {|s| @always_install.include? s }

  dir = File.expand_path dir

  installed = []

  options[:development] = false
  options[:install_dir] = dir
  options[:only_install_dir] = true
  @prerelease = options[:prerelease]

  sorted_requests.each do |request|
    spec = request.spec

    if existing.find {|s| s.full_name == spec.full_name }
      yield request, nil if block_given?
      next
    end

    spec.install options do |installer|
      yield request, installer if block_given?
    end

    installed << request
  end

  install_hooks installed, options

  installed
ensure
  ENV['GEM_HOME'] = gem_home
end

#load_gemdeps(path, without_groups = [], installing = false) ⇒ Object

Load a dependency management file.


329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/rubygems/request_set.rb', line 329

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.expand_path(path)}.lock".dup.tap(&Gem::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:


350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/rubygems/request_set.rb', line 350

def pretty_print(q) # :nodoc:
  q.group 2, '[RequestSet:', ']' do
    q.breakable

    if @remote
      q.text 'remote'
      q.breakable
    end

    if @prerelease
      q.text 'prerelease'
      q.breakable
    end

    if @development_shallow
      q.text 'shallow development'
      q.breakable
    elsif @development
      q.text 'development'
      q.breakable
    end

    if @soft_missing
      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.


396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/rubygems/request_set.rb', line 396

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_currentObject

Resolve the requested dependencies against the gems available via Gem.path and return an Array of Specification objects to be activated.


433
434
435
# File 'lib/rubygems/request_set.rb', line 433

def resolve_current
  resolve Gem::Resolver::CurrentSet.new
end

#sorted_requestsObject


437
438
439
# File 'lib/rubygems/request_set.rb', line 437

def sorted_requests
  @sorted ||= strongly_connected_components.flatten
end

#specsObject


441
442
443
# File 'lib/rubygems/request_set.rb', line 441

def specs
  @specs ||= @requests.map {|r| r.full_spec }
end

#specs_in(dir) ⇒ Object


445
446
447
448
449
# File 'lib/rubygems/request_set.rb', line 445

def specs_in(dir)
  Gem::Util.glob_files_in_dir("*.gemspec", File.join(dir, "specifications")).map do |g|
    Gem::Specification.load g
  end
end

#tsort_each_child(node) ⇒ Object

:nodoc:


455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/rubygems/request_set.rb', line 455

def tsort_each_child(node) # :nodoc:
  node.spec.dependencies.each do |dep|
    next if dep.type == :development and not @development

    match = @requests.find do |r|
      dep.match? r.spec.name, r.spec.version, @prerelease
    end

    unless match
      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:


451
452
453
# File 'lib/rubygems/request_set.rb', line 451

def tsort_each_node(&block) # :nodoc:
  @requests.each(&block)
end