class MetaCommit::Changelog::Adapters::Changelog

Adapter class to write repository changes to changelog file

Constants

VERSION_DELIMITER
VERSION_HEADER_REGEX

Attributes

date[RW]
filename[RW]
path[RW]
tag[RW]

Public Class Methods

new(path, filename, tag, date) click to toggle source

@param [String] path @param [String] filename @param [String] tag version @param [String] date of version release

# File lib/meta_commit/changelog/adapters/changelog.rb, line 14
def initialize(path, filename, tag, date)
  @path=path
  @filename=filename
  @tag=tag
  @date=date
end

Public Instance Methods

write_repository_change_chunk(repo, diffs) click to toggle source

Builds changelog message and adds it after description text and before latest version @param repo @param [Array<MetaCommit::Contracts::Diff>] diffs @return [String]

# File lib/meta_commit/changelog/adapters/changelog.rb, line 25
def write_repository_change_chunk(repo, diffs)
  message_builder = changelog_message_builder(@tag, @date)
  diffs.each do |diff|
    message_builder.add_to_added(diff.string_representation) if diff.type_addition?
    message_builder.add_to_removed(diff.string_representation) if diff.type_deletion?
    message_builder.add_to_changed(diff.string_representation) if diff.type_replace?
  end
  prepend_to_changelog(message_builder.build)
end

Private Instance Methods

changelog_message_builder(version, date) click to toggle source

@param [String] version @param [String] date @return [MetaCommit::Services::KeepAChangelogVerReportBuilder]

# File lib/meta_commit/changelog/adapters/changelog.rb, line 45
def changelog_message_builder(version, date)
  MetaCommit::Changelog::Formatters::KeepAChangelogVerReportBuilder.new(version, date)
end
changelog_path() click to toggle source

@return [String] path to changelog file

# File lib/meta_commit/changelog/adapters/changelog.rb, line 36
def changelog_path
  File.join(@path, @filename)
end
prepend_to_changelog(text) click to toggle source

@param [String] text @return [String]

# File lib/meta_commit/changelog/adapters/changelog.rb, line 53
def prepend_to_changelog(text)
  current_changelog_content = read_from_changelog
  current_changelog_parts = current_changelog_content.split(VERSION_HEADER_REGEX)

  if starts_with_description(current_changelog_parts)
    new_changelog_parts = [current_changelog_parts[0]] + [text, "#{VERSION_DELIMITER}"] + current_changelog_parts[1..-1]
  else
    new_changelog_parts = [text, "#{VERSION_DELIMITER}"] + current_changelog_parts
  end

  write_to_changelog(new_changelog_parts.join)
end
read_from_changelog() click to toggle source

@return [String] changelog file content

# File lib/meta_commit/changelog/adapters/changelog.rb, line 69
def read_from_changelog
  File.read(changelog_path)
end
starts_with_description(changelog_parts) click to toggle source

@param [Array<String>] changelog_parts content sections from changelog @return [Boolean]

# File lib/meta_commit/changelog/adapters/changelog.rb, line 86
def starts_with_description(changelog_parts)
  return false if changelog_parts.empty?
  changelog_parts[0].match(VERSION_HEADER_REGEX).nil?
end
write_to_changelog(text) click to toggle source

@param [String] text content to write to changelog file @return [String]

# File lib/meta_commit/changelog/adapters/changelog.rb, line 77
def write_to_changelog(text)
  File.open(changelog_path, 'w').write(text)
  text
end