class Chandler::Changelog

Responsible for parsing a CHANGELOG into a hash of release notes keyed by version number. Release notes for a particular version or tag can be accessed using the `fetch` method.

Constants

HEADING_PATTERNS
NoMatchingVersion

Attributes

path[R]

Public Class Methods

new(path:) click to toggle source
# File lib/chandler/changelog.rb, line 26
def initialize(path:)
  @path = path
end

Public Instance Methods

basename() click to toggle source
# File lib/chandler/changelog.rb, line 43
def basename
  File.basename(path.to_s)
end
fetch(tag) click to toggle source

Fetch release notes for the given tag or version number.

E.g. fetch(“v1.0.1”) # => “nRelease notes for 1.0.1.n” fetch(“1.0.1”) # => “nRelease notes for 1.0.1.n” fetch(“blergh”) # => Chandler::NoMatchingVersion

# File lib/chandler/changelog.rb, line 37
def fetch(tag)
  versions.fetch(tag.version_number) do
    raise NoMatchingVersion, "Couldn’t find #{tag} in #{path}"
  end
end

Private Instance Methods

sections(heading_re) click to toggle source

Parses the changelog into a hash, where the keys of the hash are the Markdown/rdoc headings matching the specified heading regexp, and values are the content delimited by those headings.

E.g. { “## v1.0.1n” => “nRelease notes for 1.0.1.n” }

# File lib/chandler/changelog.rb, line 94
def sections(heading_re)
  hash = {}
  heading = ""
  remainder = text

  until remainder.empty?
    hash[heading], heading, remainder = remainder.partition(heading_re)
  end

  hash
end
text() click to toggle source
# File lib/chandler/changelog.rb, line 106
def text
  @text ||= IO.read(path, :encoding => "UTF-8")
end
versions() click to toggle source

Transforms the changelog into a hash where the keys are version numbers and the values are the release notes for those versions. The values are not stripped of whitespace.

The version numbers are assumed to be contained at Markdown or Rdoc headings. The release notes for those version numbers are the text delimited by those headings. The algorithm tries various styles of these Markdown and Rdoc headings (see `HEADING_PATTERNS`).

The resulting hash entries look like: { “1.0.1” => “nRelease notes for 1.0.1.n” }

# File lib/chandler/changelog.rb, line 61
def versions
  @versions ||= begin
    HEADING_PATTERNS.reduce({}) do |found, heading_re|
      versions_at_headings(heading_re).merge(found)
    end
  end
end
versions_at_headings(heading_re) click to toggle source

rubocop:disable Style/SymbolProc

# File lib/chandler/changelog.rb, line 70
def versions_at_headings(heading_re)
  sections(heading_re).each_with_object({}) do |(heading, text), versions|
    tokens = heading.gsub(/[\[\]\(\)`]/, " ").split(/[[:space:]]/)
    tokens = tokens.map(&:strip)
    version = tokens.find { |t| t.version? }
    text_with_references = [text, link_references].join("\n\n")
    versions[version.version_number] = text_with_references if version
  end
end