Class: Berkshelf::Resolver

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

Defined Under Namespace

Classes: Graph

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(berksfile, demands = []) ⇒ Resolver

Returns a new instance of Resolver.

Parameters:

  • berksfile (Berksfile)
  • demands (Array<Dependency>, Dependency) (defaults to: [])

    a dependency, or array of dependencies, which must be satisfied



20
21
22
23
24
25
26
# File 'lib/berkshelf/resolver.rb', line 20

def initialize(berksfile, demands = [])
  @berksfile = berksfile
  @graph     = Graph.new
  @demands   = Array.new

  Array(demands).each { |demand| add_demand(demand) }
end

Instance Attribute Details

#berksfileBerksfile (readonly)

Returns:



8
9
10
# File 'lib/berkshelf/resolver.rb', line 8

def berksfile
  @berksfile
end

#demandsArray<Dependency> (readonly)

Returns an array of dependencies that must be satisfied.

Returns:

  • (Array<Dependency>)

    an array of dependencies that must be satisfied



15
16
17
# File 'lib/berkshelf/resolver.rb', line 15

def demands
  @demands
end

#graphResolver::Graph (readonly)

Returns:



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

def graph
  @graph
end

Instance Method Details

#[](demand) ⇒ Dependency Also known as: get_demand

Retrieve the given demand from the resolver

Parameters:

  • demand (Dependency, #to_s)

    name of the dependency to return

Returns:



92
93
94
95
# File 'lib/berkshelf/resolver.rb', line 92

def [](demand)
  name = demand.respond_to?(:name) ? demand.name : demand.to_s
  demands.find { |demand| demand.name == name }
end

#add_demand(demand) ⇒ Array<Dependency>

Add the given dependency to the collection of demands

Parameters:

  • demand (Dependency)

    add a dependency that must be satisfied to the graph

Returns:

Raises:



36
37
38
39
40
41
42
# File 'lib/berkshelf/resolver.rb', line 36

def add_demand(demand)
  if has_demand?(demand)
    raise DuplicateDemand, "A demand named '#{demand.name}' is already present."
  end

  demands.push(demand)
end

#add_explicit_dependencies(cookbook) ⇒ Hash

Add dependencies of a locally cached cookbook which will take precedence over anything found in the universe.

Parameters:

Returns:

  • (Hash)


50
51
52
# File 'lib/berkshelf/resolver.rb', line 50

def add_explicit_dependencies(cookbook)
  graph.populate_local(cookbook)
end

#demand_arrayArray<String, String>

Note:

this is the format that Solve uses to determine a solution for the graph

An array of arrays containing the name and constraint of each demand

Returns:



59
60
61
62
63
64
# File 'lib/berkshelf/resolver.rb', line 59

def demand_array
  demands.collect do |demand|
    constraint = demand.locked_version || demand.version_constraint
    [demand.name, constraint]
  end
end

#has_demand?(demand) ⇒ Boolean

Check if the given demand has been added to the resolver

Parameters:

  • demand (Dependency, #to_s)

    the demand or the name of the demand to check for

Returns:

  • (Boolean)


102
103
104
# File 'lib/berkshelf/resolver.rb', line 102

def has_demand?(demand)
  !get_demand(demand).nil?
end

#resolveArray<Array<String, String, Dependency>>

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

Returns:

Raises:

  • (NoSolutionError)

    when a solution could not be found for the given demands



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/berkshelf/resolver.rb', line 72

def resolve
  graph.populate_store
  graph.populate(berksfile.sources)

  Solve.it!(graph, demand_array, ENV['DEBUG_RESOLVER'] ? { ui: Berkshelf.ui } : {}).collect do |name, version|
    dependency = get_demand(name) || Dependency.new(berksfile, name)
    dependency.locked_version = version

    dependency
  end
rescue Solve::Errors::NoSolutionError
  raise NoSolutionError.new(demands)
end