Class: Berkshelf::Lockfile

Inherits:
Object
  • Object
show all
Defined in:
lib/berkshelf/lockfile.rb

Overview

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.

Defined Under Namespace

Classes: LockfileLegacy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(berksfile) ⇒ Lockfile

Create a new lockfile instance associated with the given Berksfile. If a Lockfile exists, it is automatically loaded. Otherwise, an empty instance is created and ready for use.

Parameters:



22
23
24
25
26
27
28
# File 'lib/berkshelf/lockfile.rb', line 22

def initialize(berksfile)
  @berksfile = berksfile
  @filepath  = File.expand_path("#{berksfile.filepath}.lock")
  @sources   = {}

  load! if File.exists?(@filepath)
end

Instance Attribute Details

#berksfileBerkshelf::Berksfile (readonly)

Returns the Berksfile for this Lockfile.

Returns:



14
15
16
# File 'lib/berkshelf/lockfile.rb', line 14

def berksfile
  @berksfile
end

#filepathPathname (readonly)

Returns the path to this Lockfile.

Returns:

  • (Pathname)

    the path to this Lockfile



10
11
12
# File 'lib/berkshelf/lockfile.rb', line 10

def filepath
  @filepath
end

Instance Method Details

#add(source) ⇒ Object Also known as: append

Add the given source to the ‘sources` list, if it doesn’t already exist.

Parameters:



97
98
99
# File 'lib/berkshelf/lockfile.rb', line 97

def add(source)
  @sources[cookbook_name(source)] = source
end

#find(source) ⇒ CookbookSource?

Find the given source in this lockfile. This method accepts a source attribute which may either be the name of a cookbook (String) or an actual cookbook source.

Parameters:

Returns:

  • (CookbookSource, nil)

    the cookbook source from this lockfile or nil if one was not found



66
67
68
# File 'lib/berkshelf/lockfile.rb', line 66

def find(source)
  @sources[cookbook_name(source).to_s]
end

#has_source?(source) ⇒ Boolean

Determine if this lockfile contains the given source.

Parameters:

Returns:

  • (Boolean)

    true if the source exists, false otherwise



76
77
78
# File 'lib/berkshelf/lockfile.rb', line 76

def has_source?(source)
  !find(source).nil?
end

#inspectString

Returns the detailed string representation of the lockfile.

Returns:

  • (String)

    the detailed string representation of the lockfile



128
129
130
# File 'lib/berkshelf/lockfile.rb', line 128

def inspect
  "#<Berkshelf::Lockfile #{Pathname.new(filepath).basename}, sources: #{sources.inspect}>"
end

#load!Object

Load the lockfile from file system.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/berkshelf/lockfile.rb', line 31

def load!
  contents = File.read(filepath).strip
  hash = parse(contents)

  hash[:sources].each do |name, options|
    # Dynamically calculate paths relative to the Berksfile if a path is given
    options[:path] &&= File.expand_path(options[:path], File.dirname(filepath))

    begin
      add(CookbookSource.new(berksfile, name.to_s, options))
    rescue Berkshelf::CookbookNotFound
      # It's possible that a source is locked that contains a path location, and
      # that path location was renamed or no longer exists. When loading the
      # lockfile, Berkshelf will throw an error if it can't find a cookbook that
      # previously existed at a path location.
    end
  end
end

#remove(source) ⇒ Object Also known as: unlock

Remove the given source from this lockfile. This method accepts a source attribute which may either be the name of a cookbook (String) or an actual cookbook source.

Parameters:

Raises:



111
112
113
114
115
116
117
# File 'lib/berkshelf/lockfile.rb', line 111

def remove(source)
  unless has_source?(source)
    raise Berkshelf::CookbookNotFound, "'#{cookbook_name(source)}' does not exist in this lockfile!"
  end

  @sources.delete(cookbook_name(source))
end

#sourcesArray<Berkshelf::CookbookSource>

The list of sources constrained in this lockfile.

Returns:



54
55
56
# File 'lib/berkshelf/lockfile.rb', line 54

def sources
  @sources.values
end

#to_hashHash

Write the current lockfile to a hash

Returns:

  • (Hash)

    the hash representation of this lockfile

    • :sources [Array<Berkshelf::CookbookSource>] the list of sources



137
138
139
140
141
# File 'lib/berkshelf/lockfile.rb', line 137

def to_hash
  {
    sources: @sources
  }
end

#to_json(options = {}) ⇒ String

The JSON representation of this lockfile

Relies on #to_hash to generate the json

Returns:

  • (String)

    the JSON representation of this lockfile



149
150
151
# File 'lib/berkshelf/lockfile.rb', line 149

def to_json(options = {})
  JSON.pretty_generate(to_hash, options)
end

#to_sString

Returns the string representation of the lockfile.

Returns:

  • (String)

    the string representation of the lockfile



122
123
124
# File 'lib/berkshelf/lockfile.rb', line 122

def to_s
  "#<Berkshelf::Lockfile #{Pathname.new(filepath).basename}>"
end

#update(sources) ⇒ Object

Replace the current list of sources with ‘sources`. This method does not write out the lockfile - it only changes the state of the object.

Parameters:

  • sources (Array<Berkshelf::CookbookSource>)

    the list of sources to update

  • options (Hash)

    a customizable set of options



87
88
89
90
91
# File 'lib/berkshelf/lockfile.rb', line 87

def update(sources)
  reset_sources!
  sources.each { |source| append(source) }
  save
end