lib/berkshelf/lockfile.rb in berkshelf-2.0.0.beta vs lib/berkshelf/lockfile.rb in berkshelf-2.0.0
- old
+ new
@@ -1,12 +1,12 @@
module Berkshelf
# The object representation of the Berkshelf lockfile. The lockfile is useful
# when working in teams where the same cookbook versions are desired across
# multiple workstations.
- #
- # @author Seth Vargo <sethvargo@gmail.com>
class Lockfile
+ require_relative 'cookbook_source'
+
# @return [Pathname]
# the path to this Lockfile
attr_reader :filepath
# @return [Berkshelf::Berksfile]
@@ -37,12 +37,12 @@
begin
hash = JSON.parse(contents, symbolize_names: true)
rescue JSON::ParserError
if contents =~ /^cookbook ["'](.+)["']/
- Berkshelf.ui.warn "You are using the old lockfile format. Attempting to convert..."
- hash = LockfileLegacy.parse(contents)
+ Berkshelf.ui.warn 'You are using the old lockfile format. Attempting to convert...'
+ hash = LockfileLegacy.parse(berksfile, contents)
else
raise
end
end
@@ -192,24 +192,27 @@
source.is_a?(CookbookSource) ? source.name : source.to_s
end
# Legacy support for old lockfiles
#
- # @author Seth Vargo <sethvargo@gmail.com>
- # @todo Remove this class in the next major release.
+ # @todo Remove this class in Berkshelf 3.0.0
class LockfileLegacy
+ require 'pathname'
+
class << self
# Read the old lockfile content and instance eval in context.
#
+ # @param [Berkshelf::Berksfile] berksfile
+ # the associated berksfile
# @param [String] content
# the string content read from a legacy lockfile
- def parse(content)
+ def parse(berksfile, content)
sources = {}.tap do |hash|
content.split("\n").each do |line|
next if line.empty?
- source = self.new(line)
+ source = self.new(berksfile, line)
hash[source.name] = source.options
end
end
{
@@ -225,15 +228,20 @@
# @return [String]
# the name of this cookbook
attr_reader :name
+ # @return [Berkshelf::Berksfile]
+ # the berksfile
+ attr_reader :berksfile
+
# Create a new legacy lockfile for processing
#
# @param [String] content
# the content to parse out and convert to a hash
- def initialize(content)
+ def initialize(berksfile, content)
+ @berksfile = berksfile
instance_eval(content).to_hash
end
# Method defined in legacy lockfiles (since we are using
# instance_eval).
@@ -242,10 +250,21 @@
# the name of this cookbook
# @option options [String] :locked_version
# the locked version of this cookbook
def cookbook(name, options = {})
@name = name
- @options = options
+ @options = manipulate(options)
end
+
+ private
+ # Perform various manipulations on the hash.
+ #
+ # @param [Hash] options
+ def manipulate(options = {})
+ if options[:path]
+ options[:path] = berksfile.find(name).instance_variable_get(:@options)[:path] || options[:path]
+ end
+ options
+ end
end
end
end