Class: Berkshelf::Resolver
- Inherits:
-
Object
- Object
- Berkshelf::Resolver
- Extended by:
- Forwardable
- Defined in:
- lib/berkshelf/resolver.rb
Instance Attribute Summary collapse
- #berksfile ⇒ Berkshelf::Berksfile readonly
- #graph ⇒ Solve::Graph readonly
Instance Method Summary collapse
- #[](source) ⇒ Berkshelf::CookbookSource (also: #get_source)
-
#add_source(source, include_dependencies = true) ⇒ Array<CookbookSource>
Add the given source to the collection of sources for this instance of Resolver.
-
#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.
- #has_source?(source) ⇒ Boolean
-
#initialize(berksfile, options = {}) ⇒ Resolver
constructor
A new instance of Resolver.
-
#resolve ⇒ Array<Berkshelf::CachedCookbook>
Finds a solution for the currently added sources and their dependencies and returns an array of CachedCookbooks.
-
#sources ⇒ Array<Berkshelf::CookbookSource>
An array of CookbookSources that are currently added to this resolver.
Constructor Details
#initialize(berksfile, options = {}) ⇒ Resolver
Returns a new instance of Resolver.
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, = {}) @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([:sources]).each do |source| add_source(source, false) end unless [:skip_dependencies] Array([:sources]).each do |source| add_source_dependencies(source) end end end |
Instance Attribute Details
#berksfile ⇒ Berkshelf::Berksfile (readonly)
10 11 12 |
# File 'lib/berkshelf/resolver.rb', line 10 def berksfile @berksfile end |
#graph ⇒ Solve::Graph (readonly)
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
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.
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.
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
125 126 127 |
# File 'lib/berkshelf/resolver.rb', line 125 def has_source?(source) !get_source(source).nil? end |
#resolve ⇒ Array<Berkshelf::CachedCookbook>
Finds a solution for the currently added sources and their dependencies and returns an array of CachedCookbooks.
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 |
#sources ⇒ Array<Berkshelf::CookbookSource>
Returns an array of CookbookSources that are currently added to this resolver.
87 88 89 |
# File 'lib/berkshelf/resolver.rb', line 87 def sources @sources.values end |