Class: Berkshelf::Resolver

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Resolver.

Parameters:

Options Hash (options):



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/berkshelf/resolver.rb', line 19

def initialize(berksfile, options = {})
  @berksfile  = berksfile
  @downloader = berksfile.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

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

Instance Attribute Details

#berksfileBerkshelf::Berksfile (readonly)



10
11
12
# File 'lib/berkshelf/resolver.rb', line 10

def berksfile
  @berksfile
end

#graphSolve::Graph (readonly)

Returns:

  • (Solve::Graph)


13
14
15
# File 'lib/berkshelf/resolver.rb', line 13

def graph
  @graph
end

Instance Method Details

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

Parameters:

Returns:



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

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:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/berkshelf/resolver.rb', line 51

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:



77
78
79
80
81
82
83
# File 'lib/berkshelf/resolver.rb', line 77

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

    add_source(CookbookSource.new(berksfile, 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)


125
126
127
# File 'lib/berkshelf/resolver.rb', line 125

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:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/berkshelf/resolver.rb', line 95

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:



87
88
89
# File 'lib/berkshelf/resolver.rb', line 87

def sources
  @sources.values
end