Class: Berkshelf::Lockfile
- Inherits:
-
Object
- Object
- Berkshelf::Lockfile
- 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
-
#berksfile ⇒ Berkshelf::Berksfile
readonly
The Berksfile for this Lockfile.
-
#filepath ⇒ Pathname
readonly
The path to this Lockfile.
Instance Method Summary collapse
-
#add(source) ⇒ Object
(also: #append)
Add the given source to the ‘sources` list, if it doesn’t already exist.
-
#find(source) ⇒ CookbookSource?
Find the given source in this lockfile.
-
#has_source?(source) ⇒ Boolean
Determine if this lockfile contains the given source.
-
#initialize(berksfile) ⇒ Lockfile
constructor
Create a new lockfile instance associated with the given Berksfile.
-
#inspect ⇒ String
The detailed string representation of the lockfile.
-
#load! ⇒ Object
Load the lockfile from file system.
-
#remove(source) ⇒ Object
(also: #unlock)
Remove the given source from this lockfile.
-
#sources ⇒ Array<Berkshelf::CookbookSource>
The list of sources constrained in this lockfile.
-
#to_hash ⇒ Hash
Write the current lockfile to a hash.
-
#to_json(options = {}) ⇒ String
The JSON representation of this lockfile.
-
#to_s ⇒ String
The string representation of the lockfile.
-
#update(sources) ⇒ Object
Replace the current list of sources with ‘sources`.
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.
22 23 24 25 26 27 28 |
# File 'lib/berkshelf/lockfile.rb', line 22 def initialize(berksfile) @berksfile = berksfile @filepath = File.("#{berksfile.filepath}.lock") @sources = {} load! if File.exists?(@filepath) end |
Instance Attribute Details
#berksfile ⇒ Berkshelf::Berksfile (readonly)
Returns the Berksfile for this Lockfile.
14 15 16 |
# File 'lib/berkshelf/lockfile.rb', line 14 def berksfile @berksfile end |
#filepath ⇒ Pathname (readonly)
Returns 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.
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.
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.
76 77 78 |
# File 'lib/berkshelf/lockfile.rb', line 76 def has_source?(source) !find(source).nil? end |
#inspect ⇒ String
Returns 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, | # Dynamically calculate paths relative to the Berksfile if a path is given [:path] &&= File.([:path], File.dirname(filepath)) begin add(CookbookSource.new(berksfile, name.to_s, )) 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.
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 |
#sources ⇒ Array<Berkshelf::CookbookSource>
The list of sources constrained in this lockfile.
54 55 56 |
# File 'lib/berkshelf/lockfile.rb', line 54 def sources @sources.values end |
#to_hash ⇒ Hash
Write the current lockfile to a hash
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
149 150 151 |
# File 'lib/berkshelf/lockfile.rb', line 149 def to_json( = {}) JSON.pretty_generate(to_hash, ) end |
#to_s ⇒ String
Returns 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.
87 88 89 90 91 |
# File 'lib/berkshelf/lockfile.rb', line 87 def update(sources) reset_sources! sources.each { |source| append(source) } save end |