Skip to content

Commit

Permalink
Remove dependence on keepachangelog
Browse files Browse the repository at this point in the history
  • Loading branch information
saturnflyer committed Jun 8, 2024
1 parent 90c403d commit 6da402e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ PATH
remote: .
specs:
reissue (0.1.1)
kramdown
rake

GEM
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion lib/reissue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 11 additions & 13 deletions lib/reissue/changelog_updater.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "keepachangelog"
require "reissue/parser"
require "reissue/printer"

module Reissue
# Updates the changelog file with new versions and changes.
Expand All @@ -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
Expand All @@ -42,20 +43,17 @@ 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

# Returns the string representation of the changelog.
#
# @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.
Expand Down
47 changes: 47 additions & 0 deletions lib/reissue/printer.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions reissue.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "rake"
spec.add_dependency "kramdown"
end

0 comments on commit 6da402e

Please sign in to comment.