Class: Berkshelf::Lockfile
- Inherits:
-
Object
- Object
- Berkshelf::Lockfile
- Defined in:
- lib/berkshelf/lockfile.rb
Constant Summary collapse
- DEFAULT_FILENAME =
"#{Berkshelf::DEFAULT_FILENAME}.lock".freeze
Instance Attribute Summary collapse
-
#sources ⇒ Object
readonly
Returns the value of attribute sources.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(sources) ⇒ Lockfile
constructor
A new instance of Lockfile.
- #remove! ⇒ Object
- #write(filename = DEFAULT_FILENAME) ⇒ Object
Constructor Details
#initialize(sources) ⇒ Lockfile
Returns a new instance of Lockfile.
41 42 43 |
# File 'lib/berkshelf/lockfile.rb', line 41 def initialize(sources) @sources = Array(sources) end |
Instance Attribute Details
#sources ⇒ Object (readonly)
Returns the value of attribute sources.
39 40 41 |
# File 'lib/berkshelf/lockfile.rb', line 39 def sources @sources end |
Class Method Details
.definition(source) ⇒ String
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/berkshelf/lockfile.rb', line 22 def definition(source) definition = "cookbook '#{source.name}'" if source.location.is_a?(GitLocation) definition += ", :git => '#{source.location.uri}', :ref => '#{source.location.branch || 'HEAD'}'" elsif source.location.is_a?(PathLocation) definition += ", :path => '#{source.location.path}'" else definition += ", :locked_version => '#{source.locked_version}'" end definition end |
.remove! ⇒ Object
4 5 6 |
# File 'lib/berkshelf/lockfile.rb', line 4 def remove! FileUtils.rm_f DEFAULT_FILENAME end |
.update!(sources) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/berkshelf/lockfile.rb', line 9 def update!(sources) contents = File.readlines(DEFAULT_FILENAME) contents.delete_if do |line| line =~ /cookbook '(#{sources.map(&:name).join('|')})'/ end contents += sources.map { |source| definition(source) } File.open(DEFAULT_FILENAME, 'wb') { |f| f.write(contents.join("\n").squeeze("\n")) } end |
Instance Method Details
#remove! ⇒ Object
50 51 52 |
# File 'lib/berkshelf/lockfile.rb', line 50 def remove! self.class.remove! end |
#write(filename = DEFAULT_FILENAME) ⇒ Object
45 46 47 48 |
# File 'lib/berkshelf/lockfile.rb', line 45 def write(filename = DEFAULT_FILENAME) content = sources.map { |source| self.class.definition(source) }.join("\n") File.open(filename, "wb") { |f| f.write content } end |