class Keepachangelog::YamlPrinter

Attributes

changelog[RW]

Public Class Methods

new(changelog) click to toggle source
# File lib/keepachangelog/printer/yaml.rb, line 7
def initialize(changelog)
  self.changelog = changelog
end

Public Instance Methods

write(path) click to toggle source
# File lib/keepachangelog/printer/yaml.rb, line 11
def write(path)
  FileUtils.mkdir_p(path)
  write_meta File.join(path, 'meta.yaml')
  write_versions path, changelog['versions']
end

Private Instance Methods

create_unique_file(folder, filename) click to toggle source
# File lib/keepachangelog/printer/yaml.rb, line 71
def create_unique_file(folder, filename)
  if File.exist? File.join(folder, filename)
    randstr = SecureRandom.hex(3)
    filename = "#{randstr}-#{filename}"
  end
  File.join(folder, filename)
end
extract_field!(line, pattern, name) click to toggle source
# File lib/keepachangelog/printer/yaml.rb, line 64
def extract_field!(line, pattern, name)
  match = line.match(pattern)
  return nil unless match && match[name]
  line.gsub!(pattern, '').strip
  match[name]
end
parse_line(line) click to toggle source
# File lib/keepachangelog/printer/yaml.rb, line 53
def parse_line(line)
  issue = extract_field! line, /\s*\(#(?<issue>\d+)\)\s*/, :issue
  mr = extract_field! line, /\s*\(!(?<mr>\d+)\)\s*/, :mr
  author = extract_field! line, /\s*\((?<author>.*@.*)\)\s*/, :author
  data = { 'title' => line }
  data['author'] = author if author
  data['merge_request'] = mr.to_i if mr
  data['issue'] = issue.to_i if issue
  data
end
write_change(folder, change) click to toggle source
# File lib/keepachangelog/printer/yaml.rb, line 45
def write_change(folder, change)
  fname = change['title'].gsub(/\W/, ' ').strip.tr(' ', '-').downcase
  fname = "#{change['issue']}-#{fname}" if change['issue']
  fname = "#{change['merge_request']}-#{fname}" if change['merge_request']
  path = create_unique_file(folder, fname + '.yaml')
  File.write(path, change.to_yaml)
end
write_changes(path, changes) click to toggle source
# File lib/keepachangelog/printer/yaml.rb, line 35
def write_changes(path, changes)
  changes.each do |section, lines|
    lines.each do |line|
      change = parse_line(line)
      change['type'] = section
      write_change path, change
    end
  end
end
write_meta(path) click to toggle source
# File lib/keepachangelog/printer/yaml.rb, line 19
def write_meta(path)
  meta = {}
  %w[url title intro].each do |key|
    meta[key] = changelog[key] unless changelog[key].to_s.empty?
  end
  File.write(path, meta.to_yaml)
end
write_versions(path, versions) click to toggle source
# File lib/keepachangelog/printer/yaml.rb, line 27
def write_versions(path, versions)
  versions.each do |version, data|
    folder = File.join(path, version)
    FileUtils.mkdir_p folder
    write_changes folder, data['changes']
  end
end