Sha256: 3f1724de21bbe81fdcfb3ec73ec0ee93fc77eab8aaaa28b786ed486ce0e54deb

Contents?: true

Size: 1.06 KB

Versions: 11

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

require "date"

module Groundskeeper
  # The `CHANGELOG.md` file in an application.
  class Changelog
    attr_reader :document, :date

    FILENAME = "CHANGELOG.md"
    TITLE_EXPRESSION = "# Changelog"

    def self.build(filename: FILENAME)
      new(
        document: Groundskeeper::Document.new(filename),
        date: Date.today.iso8601
      )
    end

    def initialize(document:, date:)
      @document = document
      @date = date
    end

    # rubocop:disable Performance/RedundantMatch,Performance/RegexpMatch
    def update_file(version, changes)
      unless document.match(/(#{TITLE_EXPRESSION})/)
        document.prepend_file("#{TITLE_EXPRESSION}\n\n")
      end

      document
        .append_to_file(TITLE_EXPRESSION, version_template(version, changes))
    end
    # rubocop:enable Performance/RedundantMatch,Performance/RegexpMatch

    private

    def version_template(version, changes)
      ["\n", "## #{version} - #{date}", ""]
        .concat(changes.map { |change| "* #{change}" })
        .join("\n")
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
groundskeeper-bitcore-0.3.1 lib/groundskeeper/changelog.rb
groundskeeper-bitcore-0.3.0 lib/groundskeeper/changelog.rb
groundskeeper-bitcore-0.2.7 lib/groundskeeper/changelog.rb
groundskeeper-bitcore-0.2.6 lib/groundskeeper/changelog.rb
groundskeeper-bitcore-0.2.5 lib/groundskeeper/changelog.rb
groundskeeper-bitcore-0.2.4 lib/groundskeeper/changelog.rb
groundskeeper-bitcore-0.2.3 lib/groundskeeper/changelog.rb
groundskeeper-bitcore-0.2.2 lib/groundskeeper/changelog.rb
groundskeeper-bitcore-0.2.1 lib/groundskeeper/changelog.rb
groundskeeper-bitcore-0.2.0 lib/groundskeeper/changelog.rb
groundskeeper-bitcore-0.1.1 lib/groundskeeper/changelog.rb