Class: Berkshelf::Resolver

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/berkshelf/resolver.rb

Overview

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(downloader, options = {}) ⇒ Resolver

Returns a new instance of Resolver.

Parameters:

  • downloader (Downloader)
  • options (Hash) (defaults to: {})

Options Hash (options):



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/berkshelf/resolver.rb', line 12

def initialize(downloader, options = {})
  @downloader = downloader
  @graph = Solve::Graph.new
  @sources = Hash.new

  # Dependencies need to be added AFTER the sources. If they are
  # not, then one of the dependencies of a source that is added
  # may take precedence over an explicitly set source that appears
  # later in the iterator.
  Array(options[:sources]).each do |source|
    add_source(source, false)
  end

  Array(options[:sources]).each do |source|
    add_source_dependencies(source)
  end
end

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph.



6
7
8
# File 'lib/berkshelf/resolver.rb', line 6

def graph
  @graph
end

Instance Method Details

#[](source) ⇒ Berkshelf::CookbookSource Also known as: get_source

Parameters:

Returns:



105
106
107
108
109
110
# File 'lib/berkshelf/resolver.rb', line 105

def [](source)
  if source.is_a?(CookbookSource)
    source = source.name
  end
  @sources[source.to_s]
end

#add_source(source, include_dependencies = true) ⇒ Array<CookbookSource>

Add the given source to the collection of sources for this instance of Resolver. By default the dependencies of the given source will also be added as sources to the collection.

Parameters:

  • source (Berkshelf::CookbookSource)

    source to add

  • include_dependencies (Boolean) (defaults to: true)

    adds the dependencies of the given source as sources to the collection of if true. Dependencies will be ignored if false.

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/berkshelf/resolver.rb', line 41

def add_source(source, include_dependencies = true)
  if has_source?(source)
    raise DuplicateSourceDefined, "A source named '#{source.name}' is already present."
  end

  @sources[source.name] = source
  use_source(source) || install_source(source)

  graph.artifacts(source.name, source.cached_cookbook.version)

  if include_dependencies
    add_source_dependencies(source)
  end

  sources
end

#add_source_dependencies(source) ⇒ Array<CookbookSource>

Add the dependencies of the given source as sources in the collection of sources on this instance of Resolver. Any dependencies which already have a source in the collection of sources of the same name will not be added to the collection a second time.

Parameters:

  • source (CookbookSource)

    source to convert dependencies into sources

Returns:



67
68
69
70
71
72
73
# File 'lib/berkshelf/resolver.rb', line 67

def add_source_dependencies(source)
  source.cached_cookbook.dependencies.each do |name, constraint|
    next if has_source?(name)

    add_source(CookbookSource.new(name, constraint: constraint))
  end
end

#has_source?(source) ⇒ Boolean

Parameters:

  • source (CoobookSource, #to_s)

    the source to test if the resolver has added

Returns:

  • (Boolean)


115
116
117
# File 'lib/berkshelf/resolver.rb', line 115

def has_source?(source)
  !get_source(source).nil?
end

#resolveArray<Berkshelf::CachedCookbook>

Finds a solution for the currently added sources and their dependencies and returns an array of CachedCookbooks.

Returns:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/berkshelf/resolver.rb', line 85

def resolve
  demands = [].tap do |l_demands|
    graph.artifacts.each do |artifact|
      l_demands << [ artifact.name, artifact.version ]
    end
  end

  solution = Solve.it!(graph, demands)

  [].tap do |cached_cookbooks|
    solution.each do |name, version|
      cached_cookbooks << get_source(name).cached_cookbook
    end
  end
end

#sourcesArray<Berkshelf::CookbookSource>

Returns an array of CookbookSources that are currently added to this resolver.

Returns:



77
78
79
# File 'lib/berkshelf/resolver.rb', line 77

def sources
  @sources.collect { |name, source| source }
end