class CookbookBumper::EnvFile

Attributes

log[R]
path[R]

Public Class Methods

new(path) click to toggle source
# File lib/cookbook_bumper/envfile.rb, line 11
def initialize(path)
  @path = path
  @env = parse(File.read(path))
  @log = []
end

Public Instance Methods

[](cookbook) click to toggle source
# File lib/cookbook_bumper/envfile.rb, line 17
def [](cookbook)
  cookbook_versions[cookbook]
end
[]=(cookbook, version) click to toggle source
# File lib/cookbook_bumper/envfile.rb, line 21
def []=(cookbook, version)
  cookbook_versions[cookbook] = version
end
clean() click to toggle source
# File lib/cookbook_bumper/envfile.rb, line 59
def clean
  each do |cookbook_name, version|
    # metadata wasn't found or metadata was found using a different name
    if CookbookBumper.cookbooks[cookbook_name].nil? || CookbookBumper.cookbooks[cookbook_name].name != cookbook_name
      log_change(cookbook_name, version, nil)
      delete(cookbook_name)
    end
  end
end
cookbook_versions() click to toggle source
# File lib/cookbook_bumper/envfile.rb, line 29
def cookbook_versions
  @env['cookbook_versions']
end
deep_sort(obj = @env) click to toggle source
# File lib/cookbook_bumper/envfile.rb, line 69
def deep_sort(obj = @env)
  if obj.is_a?(Hash)
    obj.sort.map { |k, v| [k, deep_sort(v)] }.to_h
  else
    obj
  end
end
log_change(cookbook_name, old_ver, new_ver) click to toggle source
# File lib/cookbook_bumper/envfile.rb, line 37
def log_change(cookbook_name, old_ver, new_ver)
  action = if old_ver.nil?
             'Added'
           elsif new_ver.nil?
             'Deleted'
           elsif CookbookBumper.cookbooks[cookbook_name].bumped?
             'Bumped'
           else
             'Updated'
           end
  @log << [cookbook_name, action, old_ver, new_ver]
end
name() click to toggle source
# File lib/cookbook_bumper/envfile.rb, line 25
def name
  @env['name']
end
save() click to toggle source
# File lib/cookbook_bumper/envfile.rb, line 77
def save
  File.write(path, self)
end
to_s() click to toggle source
# File lib/cookbook_bumper/envfile.rb, line 33
def to_s
  JSON.pretty_generate(deep_sort)
end
update() click to toggle source
# File lib/cookbook_bumper/envfile.rb, line 50
def update
  CookbookBumper.cookbooks.each do |cookbook|
    if cookbook.version != self[cookbook.name]
      log_change(cookbook.name, self[cookbook.name], cookbook.version)
      self[cookbook.name] = cookbook.version.exact
    end
  end
end

Private Instance Methods

parse(json) click to toggle source
# File lib/cookbook_bumper/envfile.rb, line 88
def parse(json)
  JSON.parse(resolve_conflcits(json))
end
resolve_conflcits(json) click to toggle source

trim out merge/rebase conflict markings and let those sections be regenerated

# File lib/cookbook_bumper/envfile.rb, line 84
def resolve_conflcits(json)
  json.gsub(/^[<>=]{5}.+/, '')
end