Skip to content

Commit

Permalink
Add a reformat option and reformat the changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
saturnflyer committed Jun 8, 2024
1 parent dd3acac commit cfb139f
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 5 deletions.
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
# Change log

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## 0.1.1 - Unreleased
## [0.1.1] - Unreleased

### Added:

- bundle install when running reissue
- handling of brackets in the changelog version numbers
- Reissue::Parser class to parse the changelog file
- Reissue::Printer class to print the changelog file

### Fixed:

- bug in tests loading the changelog fixture
- format of the changelog file

### Removed:

- dependency on the keepachangelog gem

## 0.1.0 - 2024-04-11

## [0.1.0] - 2024-04-11

### Added:

- Initial release
5 changes: 5 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ namespace :reissue do
date = args[:date] || Time.now.strftime("%Y-%m-%d")
Reissue.finalize(date, changelog_file: "CHANGELOG.md")
end

task :reformat do
require_relative "lib/reissue"
Reissue.reformat("CHANGELOG.md")
end
end

Rake::Task["release"].enhance do
Expand Down
8 changes: 8 additions & 0 deletions lib/reissue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ def self.finalize(date = Date.today, changelog_file: "CHANGELOG.md")
changelog_updater = ChangelogUpdater.new(changelog_file)
changelog_updater.finalize(date:, changelog_file:)
end

# Reformats the changelog file to ensure it is correctly formatted.
#
# @param file [String] The path to the changelog file.
def self.reformat(file)
changelog_updater = ChangelogUpdater.new(file)
changelog_updater.reformat
end
end
10 changes: 10 additions & 0 deletions lib/reissue/changelog_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ def update(version, date: "Unreleased", changes: {})
changelog
end

# Reformats the changelog file to ensure it is correctly formatted.
#
# @param changelog_file [String] The path to the changelog file (default: @changelog_file).
# @return [Hash] The parsed changelog.
def reformat
@changelog = Parser.parse(File.read(@changelog_file))
write
changelog
end

# Returns the string representation of the changelog.
#
# @return [String] The Markdown string representation of the changelog.
Expand Down
5 changes: 4 additions & 1 deletion lib/reissue/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def parse_versions(scanner)
if next_line.match?(VERSION_MATCH)
scanner.scan_until(/(.+)\n/)
version, date = scanner[1].split(" - ")
date ||= "Unreleased"
version = version.gsub(VERSION_BREAK, "").strip.tr("[]", "")
changes = parse_changes(scanner)
@versions[version] = {"version" => version, "date" => date, "changes" => changes}
Expand Down Expand Up @@ -82,7 +83,9 @@ def parse_change_list(scanner, collection: [])
return collection if scanner.eos?
scanner.skip(/\s+/)
change = scanner.scan_until(/\n/)
if change.nil? || change.strip.empty? || change.match?(VERSION_OR_CHANGE_MATCH)
if change.nil? || change.strip.empty?
return collection
elsif change.match?(VERSION_OR_CHANGE_MATCH)
scanner.unscan
return collection
else
Expand Down
5 changes: 3 additions & 2 deletions lib/reissue/printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def to_s = <<~MARKDOWN
#{@preamble}
#{@versions}
MARKDOWN

private
Expand All @@ -30,8 +31,8 @@ def versions
changes_string = changes.map do |section, changes|
format_section(section, changes)
end.join("\n")
[version_string, "\n\n", changes_string].join
end.join("\n")
[version_string, changes_string].filter_map { |str| str unless str.empty? }.join("\n\n")
end.join("\n\n")
end

def format_section(section, changes)
Expand Down
6 changes: 6 additions & 0 deletions test/test_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class TestPrinter < Minitest::Spec
"title" => "Changelog",
"preamble" => "This is a changelog.",
"versions" => [
{
"version" => "1.0.1",
"date" => "Unreleased"
},
{
"version" => "1.0.0",
"date" => "2021-01-01",
Expand All @@ -24,6 +28,8 @@ class TestPrinter < Minitest::Spec
This is a changelog.
## [1.0.1] - Unreleased
## [1.0.0] - 2021-01-01
### Added
Expand Down

0 comments on commit cfb139f

Please sign in to comment.