Class: Berkshelf::CookbookStore
- Inherits:
-
Object
- Object
- Berkshelf::CookbookStore
- Defined in:
- lib/berkshelf/cookbook_store.rb
Instance Attribute Summary collapse
-
#storage_path ⇒ String
readonly
Filepath to where cookbooks are stored.
Class Method Summary collapse
-
.default_path ⇒ String
The default path to the cookbook store relative to the Berkshelf path.
-
.import(name, version, path) ⇒ Berkshelf::CachedCookbook
Import a cookbook found on the local filesystem into the current instance of the cookbook store.
- .instance ⇒ Berkshelf::CookbookStore
Instance Method Summary collapse
-
#clean! ⇒ Object
Destroy the contents of the initialized storage path.
-
#cookbook(name, version) ⇒ Berkshelf::CachedCookbook?
Returns an instance of CachedCookbook representing the Cookbook of your given name and version.
-
#cookbook_path(name, version) ⇒ Pathname
Returns an expanded path to the location on disk where the Cookbook of the given name and version is located.
-
#cookbooks(filter = nil) ⇒ Array<Berkshelf::CachedCookbook>
Returns an array of the Cookbooks that have been cached to the storage_path of this instance of CookbookStore.
-
#import(name, version, path) ⇒ Berkshelf::CachedCookbook
Import a cookbook found on the local filesystem into this instance of the cookbook store.
-
#initialize(storage_path) ⇒ CookbookStore
constructor
Create a new instance of CookbookStore with the given storage_path.
- #initialize_filesystem ⇒ Object
-
#satisfy(name, constraint) ⇒ Berkshelf::CachedCookbook?
Return a CachedCookbook matching the best solution for the given name and constraint.
Constructor Details
#initialize(storage_path) ⇒ CookbookStore
Create a new instance of CookbookStore with the given storage_path.
44 45 46 47 |
# File 'lib/berkshelf/cookbook_store.rb', line 44 def initialize(storage_path) @storage_path = Pathname.new(storage_path) initialize_filesystem end |
Instance Attribute Details
#storage_path ⇒ String (readonly)
Returns filepath to where cookbooks are stored.
36 37 38 |
# File 'lib/berkshelf/cookbook_store.rb', line 36 def storage_path @storage_path end |
Class Method Details
.default_path ⇒ String
The default path to the cookbook store relative to the Berkshelf path.
9 10 11 |
# File 'lib/berkshelf/cookbook_store.rb', line 9 def default_path File.join(Berkshelf.berkshelf_path, 'cookbooks') end |
.import(name, version, path) ⇒ Berkshelf::CachedCookbook
Import a cookbook found on the local filesystem into the current instance of the cookbook store.
29 30 31 |
# File 'lib/berkshelf/cookbook_store.rb', line 29 def import(name, version, path) instance.import(name, version, path) end |
.instance ⇒ Berkshelf::CookbookStore
14 15 16 |
# File 'lib/berkshelf/cookbook_store.rb', line 14 def instance @instance ||= new(default_path) end |
Instance Method Details
#clean! ⇒ Object
Destroy the contents of the initialized storage path.
50 51 52 |
# File 'lib/berkshelf/cookbook_store.rb', line 50 def clean! FileUtils.rm_rf(Dir.glob(File.join(storage_path, '*'))) end |
#cookbook(name, version) ⇒ Berkshelf::CachedCookbook?
Returns an instance of CachedCookbook representing the Cookbook of your given name and version.
82 83 84 85 86 87 |
# File 'lib/berkshelf/cookbook_store.rb', line 82 def cookbook(name, version) path = cookbook_path(name, version) return nil unless path.cookbook? CachedCookbook.from_store_path(path) end |
#cookbook_path(name, version) ⇒ Pathname
Returns an expanded path to the location on disk where the Cookbook of the given name and version is located.
123 124 125 |
# File 'lib/berkshelf/cookbook_store.rb', line 123 def cookbook_path(name, version) storage_path.join("#{name}-#{version}") end |
#cookbooks(filter = nil) ⇒ Array<Berkshelf::CachedCookbook>
Returns an array of the Cookbooks that have been cached to the storage_path of this instance of CookbookStore.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/berkshelf/cookbook_store.rb', line 96 def cookbooks(filter = nil) cookbooks = storage_path.children.collect do |path| begin Semverse::Version.split(File.basename(path).slice(CachedCookbook::DIRNAME_REGEXP, 2)) rescue Semverse::InvalidVersionFormat # Skip cookbooks that were downloaded by an SCM location. These can not be considered # complete cookbooks. next end CachedCookbook.from_store_path(path) end.compact return cookbooks unless filter cookbooks.select do |cookbook| filter === cookbook.cookbook_name end end |
#import(name, version, path) ⇒ Berkshelf::CachedCookbook
Import a cookbook found on the local filesystem into this instance of the cookbook store.
64 65 66 67 68 69 70 71 |
# File 'lib/berkshelf/cookbook_store.rb', line 64 def import(name, version, path) destination = cookbook_path(name, version) FileUtils.mv(path, destination) cookbook(name, version) rescue => ex FileUtils.rm_f(destination) raise end |
#initialize_filesystem ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/berkshelf/cookbook_store.rb', line 127 def initialize_filesystem FileUtils.mkdir_p(storage_path, mode: 0755) unless File.writable?(storage_path) raise InsufficientPrivledges.new(storage_path) end end |
#satisfy(name, constraint) ⇒ Berkshelf::CachedCookbook?
Return a CachedCookbook matching the best solution for the given name and constraint. Nil is returned if no matching CachedCookbook is found.
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/berkshelf/cookbook_store.rb', line 142 def satisfy(name, constraint) graph = Solve::Graph.new cookbooks(name).each { |cookbook| graph.artifact(name, cookbook.version) } name, version = Solve.it!(graph, [[name, constraint]], ENV['DEBUG_RESOLVER'] ? { ui: Berkshelf.ui } : {}).first cookbook(name, version) rescue Solve::Errors::NoSolutionError nil end |