From 6da402eef5c233fe3acb1350e129dfd694b8ff40 Mon Sep 17 00:00:00 2001 From: Jim Gay Date: Sat, 8 Jun 2024 10:00:18 -0400 Subject: [PATCH] Remove dependence on keepachangelog --- Gemfile.lock | 3 ++ lib/reissue.rb | 1 - lib/reissue/changelog_updater.rb | 24 ++++++++-------- lib/reissue/printer.rb | 47 ++++++++++++++++++++++++++++++++ reissue.gemspec | 1 + 5 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 lib/reissue/printer.rb diff --git a/Gemfile.lock b/Gemfile.lock index 9dd9228..ac2e8fd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,6 +2,7 @@ PATH remote: . specs: reissue (0.1.1) + kramdown rake GEM @@ -17,6 +18,8 @@ GEM rdoc (>= 4.0.0) reline (>= 0.4.2) json (2.7.2) + kramdown (2.4.0) + rexml language_server-protocol (3.17.0.3) lint_roller (1.1.0) minitest (5.23.1) diff --git a/lib/reissue.rb b/lib/reissue.rb index 31b0fc7..215cd1b 100644 --- a/lib/reissue.rb +++ b/lib/reissue.rb @@ -3,7 +3,6 @@ require_relative "reissue/version" require_relative "reissue/version_updater" require_relative "reissue/changelog_updater" -require_relative "reissue/parser" # Reissue is a module that provides functionality for updating version numbers and changelogs. module Reissue diff --git a/lib/reissue/changelog_updater.rb b/lib/reissue/changelog_updater.rb index 05c19d4..406747f 100644 --- a/lib/reissue/changelog_updater.rb +++ b/lib/reissue/changelog_updater.rb @@ -1,4 +1,5 @@ -require "keepachangelog" +require "reissue/parser" +require "reissue/printer" module Reissue # Updates the changelog file with new versions and changes. @@ -23,14 +24,14 @@ def call(version, date: "Unreleased", changes: {}, changelog_file: @changelog_fi end def finalize(date: Date.today, changelog_file: @changelog_file) - @changelog = Keepachangelog::MarkdownParser.parse(File.read(changelog_file)) + @changelog = Parser.parse(File.read(changelog_file)) # find the highest version number and if it is unreleased, update the date - version = changelog["versions"].keys.max - version_date = changelog.dig("versions", version, "date") + version = changelog["versions"].max_by { |v| Gem::Version.new(v["version"]) } + version_date = version["date"] if version_date.nil? || version_date == "Unreleased" - updated = changelog["versions"].delete(version) - new_version = version.sub(/\s-.*/, "") - changelog["versions"][new_version] = updated.merge("date" => date) + changelog["versions"].find do |v| + v["version"] == version["version"] + end["date"] = date end write(changelog_file) changelog @@ -42,12 +43,9 @@ def finalize(date: Date.today, changelog_file: @changelog_file) # @param date [String] The release date (default: "Unreleased"). # @param changes [Hash] The changes for the version (default: {}). def update(version, date: "Unreleased", changes: {}) - @changelog = Keepachangelog::MarkdownParser.parse(File.read(@changelog_file)) + @changelog = Parser.parse(File.read(@changelog_file)) - changelog["versions"][version] = {"date" => date, "changes" => changes} - changes.each do |section, change| - changelog["versions"][version]["changes"][section] = change - end + changelog["versions"].unshift({"version" => version, "date" => date, "changes" => changes}) changelog end @@ -55,7 +53,7 @@ def update(version, date: "Unreleased", changes: {}) # # @return [String] The Markdown string representation of the changelog. def to_s - Keepachangelog::MarkdownPrinter.new(changelog["versions"]).to_s + Printer.new(changelog).to_s end # Writes the changelog to the specified file. diff --git a/lib/reissue/printer.rb b/lib/reissue/printer.rb new file mode 100644 index 0000000..1857361 --- /dev/null +++ b/lib/reissue/printer.rb @@ -0,0 +1,47 @@ +require "kramdown" + +module Reissue + class Printer + def initialize(changelog) + @changelog = changelog + @title = @changelog["title"] + @preamble = @changelog["preamble"] + @versions = versions + end + + def template = <<~MARKDOWN + # #{@title} + + #{@preamble} + + #{@versions} + MARKDOWN + + def to_s + Kramdown::Document.new(template).to_kramdown + end + + private + + def versions + @changelog["versions"].map do |data| + version = data["version"] + date = data["date"] + changes = data.fetch("changes") do + {} + end + <<~MARKDOWN + ## #{version} - #{date} + #{changes.map { |section, changes| format_section(section, changes) }.join("\n")} + MARKDOWN + end.join("\n") + end + + def format_section(section, changes) + <<~MARKDOWN + ### #{section} + #{changes.map { |change| "- #{change}" }.join("\n")} + MARKDOWN + end + end +end diff --git a/reissue.gemspec b/reissue.gemspec index 6e5ad19..bd5df2f 100644 --- a/reissue.gemspec +++ b/reissue.gemspec @@ -23,4 +23,5 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.add_dependency "rake" + spec.add_dependency "kramdown" end