lib/minimart/mirror/local_store.rb in minimart-1.1.6 vs lib/minimart/mirror/local_store.rb in minimart-1.2.0
- old
+ new
@@ -14,18 +14,23 @@
# @param [String] directory_path The path to the local inventory
def initialize(directory_path)
@directory_path = directory_path
@cookbooks = {}
+ @metadata = {}
load_local_inventory
+
end
# :nodoc:
def add_cookbook_to_store(name, version)
cookbooks[name] ||= []
cookbooks[name] << version
+
+ metadata[name] ||= {}
+ metadata[name][version] = local_path_for("#{name}-#{version}")
end
# Copy a given cookbook to the local store, and record any metadata
# about how the cookbook was downloaded.
# @param [String] path The path to the cookbook to add to the store
@@ -43,10 +48,32 @@
def installed?(cookbook_name, cookbook_version)
!!(cookbooks[cookbook_name] &&
cookbooks[cookbook_name].include?(cookbook_version))
end
+ def cookbook_for_requirement(requirement)
+ #we dont handle caching for anything other than git requirements in this function.
+ return nil unless requirement.is_a?(InventoryRequirement::GitRequirement)
+ # if this is a branch, we can't assume that the commit is the same (remote could have changed)
+ return nil if requirement.branch
+
+ @metadata.each{ |cookbook, versions|
+
+ versions.each{ |version, lazy_metadata|
+ #lazy populate the metadata
+ if(lazy_metadata.is_a?(String))
+ lazy_metadata = Minimart::Mirror::DownloadMetadata.new(lazy_metadata)
+ end
+
+ if requirement.matching_source?(lazy_metadata)
+ return "#{cookbook}-#{version}"
+ end
+ }
+ }
+ return nil
+ end
+
# Validate that a new resolved requirement is not in the local store
# with different requirements. If we download two different branches
# of the same cookbook and they both resolve to the same version, then we
# raise an exception.
# @param [Minimart::Cookbook] new_cookbook
@@ -62,9 +89,10 @@
end
private
attr_reader :cookbooks
+ attr_reader :metadata
def copy_cookbook(source, destination)
FileUtils.rm_rf(destination) if Dir.exists?(destination)
FileUtils.cp_r(source, destination)