Class: Bundler::Resolver

Inherits:
Object
  • Object
show all
Includes:
Molinillo::SpecificationProvider, Molinillo::UI
Defined in:
lib/bundler/resolver.rb

Defined Under Namespace

Classes: SpecGroup

Constant Summary

ALL =
Bundler::Dependency::PLATFORM_MAP.values.uniq.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Molinillo::SpecificationProvider

#allow_missing?

Methods included from Molinillo::UI

#output, #progress_rate

Constructor Details

#initialize(index, source_requirements, base, ruby_version, gem_version_promoter, additional_base_requirements) ⇒ Resolver

Returns a new instance of Resolver



185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/bundler/resolver.rb', line 185

def initialize(index, source_requirements, base, ruby_version, gem_version_promoter, additional_base_requirements)
  @index = index
  @source_requirements = source_requirements
  @base = base
  @resolver = Molinillo::Resolver.new(self, self)
  @search_for = {}
  @base_dg = Molinillo::DependencyGraph.new
  @base.each {|ls| @base_dg.add_vertex(ls.name, Dependency.new(ls.name, ls.version), true) }
  additional_base_requirements.each {|d| @base_dg.add_vertex(d.name, d) }
  @ruby_version = ruby_version
  @gem_version_promoter = gem_version_promoter
end

Class Method Details

.resolve(requirements, index, source_requirements = {}, base = [], ruby_version = nil, gem_version_promoter = GemVersionPromoter.new, additional_base_requirements = []) ⇒ Object

Figures out the best possible configuration of gems that satisfies the list of passed dependencies and any child dependencies without causing any gem activation errors.

Parameters

*dependencies<Gem::Dependency>

The list of dependencies to resolve

Returns

<GemBundle>,nil

If the list of dependencies can be resolved, a

collection of gemspecs is returned. Otherwise, nil is returned.


178
179
180
181
182
183
# File 'lib/bundler/resolver.rb', line 178

def self.resolve(requirements, index, source_requirements = {}, base = [], ruby_version = nil, gem_version_promoter = GemVersionPromoter.new, additional_base_requirements = [])
  base = SpecSet.new(base) unless base.is_a?(SpecSet)
  resolver = new(index, source_requirements, base, ruby_version, gem_version_promoter, additional_base_requirements)
  result = resolver.start(requirements)
  SpecSet.new(result)
end

Instance Method Details

#after_resolutionObject



234
235
236
# File 'lib/bundler/resolver.rb', line 234

def after_resolution
  Bundler.ui.info ""
end

#before_resolutionObject



230
231
232
# File 'lib/bundler/resolver.rb', line 230

def before_resolution
  Bundler.ui.info "Resolving dependencies...", false
end

#debug(depth = 0) ⇒ void

This method returns an undefined value.

Conveys debug information to the user.

Parameters:

  • depth (Integer) (defaults to: 0)

    the current depth of the resolution process.



218
219
220
221
222
223
# File 'lib/bundler/resolver.rb', line 218

def debug(depth = 0)
  return unless debug?
  debug_info = yield
  debug_info = debug_info.inspect unless debug_info.is_a?(String)
  STDERR.puts debug_info.split("\n").map {|s| "  " * depth + s }
end

#debug?Boolean

Returns:

  • (Boolean)


225
226
227
228
# File 'lib/bundler/resolver.rb', line 225

def debug?
  return @debug_mode if defined?(@debug_mode)
  @debug_mode = ENV["DEBUG_RESOLVER"] || ENV["DEBUG_RESOLVER_TREE"]
end

#dependencies_for(specification) ⇒ Object



244
245
246
# File 'lib/bundler/resolver.rb', line 244

def dependencies_for(specification)
  specification.dependencies_for_activated_platforms
end

#index_for(dependency) ⇒ Object



285
286
287
# File 'lib/bundler/resolver.rb', line 285

def index_for(dependency)
  @source_requirements[dependency.name] || @index
end

#indicate_progressObject



238
239
240
# File 'lib/bundler/resolver.rb', line 238

def indicate_progress
  Bundler.ui.info ".", false
end

#name_for(dependency) ⇒ Object



289
290
291
# File 'lib/bundler/resolver.rb', line 289

def name_for(dependency)
  dependency.name
end

#name_for_explicit_dependency_sourceObject



293
294
295
296
297
# File 'lib/bundler/resolver.rb', line 293

def name_for_explicit_dependency_source
  Bundler.default_gemfile.basename.to_s
rescue
  "Gemfile"
end

#name_for_locking_dependency_sourceObject



299
300
301
302
303
# File 'lib/bundler/resolver.rb', line 299

def name_for_locking_dependency_source
  Bundler.default_lockfile.basename.to_s
rescue
  "Gemfile.lock"
end

#requirement_satisfied_by?(requirement, activated, spec) ⇒ Boolean

Returns:

  • (Boolean)


305
306
307
# File 'lib/bundler/resolver.rb', line 305

def requirement_satisfied_by?(requirement, activated, spec)
  requirement.matches_spec?(spec) || spec.source.is_a?(Source::Gemspec)
end

#search_for(dependency) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/bundler/resolver.rb', line 248

def search_for(dependency)
  platform = dependency.__platform
  dependency = dependency.dep unless dependency.is_a? Gem::Dependency
  search = @search_for[dependency] ||= begin
    index = index_for(dependency)
    results = index.search(dependency, @base[dependency.name])
    if vertex = @base_dg.vertex_named(dependency.name)
      locked_requirement = vertex.payload.requirement
    end
    spec_groups = if results.any?
      nested = []
      results.each do |spec|
        version, specs = nested.last
        if version == spec.version
          specs << spec
        else
          nested << [spec.version, [spec]]
        end
      end
      nested.reduce([]) do |groups, (version, specs)|
        next groups if locked_requirement && !locked_requirement.satisfied_by?(version)
        groups << SpecGroup.new(specs)
      end
    else
      []
    end
    # GVP handles major itself, but it's still a bit risky to trust it with it
    # until we get it settled with new behavior. For 2.x it can take over all cases.
    if @gem_version_promoter.major?
      spec_groups
    else
      @gem_version_promoter.sort_versions(dependency, spec_groups)
    end
  end
  search.select {|sg| sg.for?(platform, @ruby_version) }.each {|sg| sg.activate_platform!(platform) }
end

#sort_dependencies(dependencies, activated, conflicts) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
# File 'lib/bundler/resolver.rb', line 309

def sort_dependencies(dependencies, activated, conflicts)
  dependencies.sort_by do |dependency|
    name = name_for(dependency)
    [
      activated.vertex_named(name).payload ? 0 : 1,
      amount_constrained(dependency),
      conflicts[name] ? 0 : 1,
      activated.vertex_named(name).payload ? 0 : search_for(dependency).count,
    ]
  end
end

#start(requirements) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/bundler/resolver.rb', line 198

def start(requirements)
  verify_gemfile_dependencies_are_found!(requirements)
  dg = @resolver.resolve(requirements, @base_dg)
  dg.map(&:payload).map(&:to_specs).flatten
rescue Molinillo::VersionConflict => e
  raise VersionConflict.new(e.conflicts.keys.uniq, e.message)
rescue Molinillo::CircularDependencyError => e
  names = e.dependencies.sort_by(&:name).map {|d| "gem '#{d.name}'" }
  raise CyclicDependencyError, "Your bundle requires gems that depend" \
    " on each other, creating an infinite loop. Please remove" \
    " #{names.count > 1 ? "either " : ""}#{names.join(" or ")}" \
    " and try again."
end