Class: Reissue::ChangelogUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/reissue/changelog_updater.rb

Overview

Updates the changelog file with new versions and changes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(changelog_file) ⇒ ChangelogUpdater

Returns a new instance of ChangelogUpdater.



7
8
9
10
# File 'lib/reissue/changelog_updater.rb', line 7

def initialize(changelog_file)
  @changelog_file = changelog_file
  @changelog = {}
end

Instance Attribute Details

#changelogObject (readonly)

Returns the value of attribute changelog.



12
13
14
# File 'lib/reissue/changelog_updater.rb', line 12

def changelog
  @changelog
end

Instance Method Details

#call(version, date: "Unreleased", changes: {}, changelog_file: @changelog_file, version_limit: 2) ⇒ Object

Updates the changelog with a new version and its changes.

Parameters:

  • version (String)

    The version number.

  • date (String) (defaults to: "Unreleased")

    The release date (default: “Unreleased”).

  • changes (Hash) (defaults to: {})

    The changes for the version (default: {}).

  • changelog_file (String) (defaults to: @changelog_file)

    The path to the changelog file (default: @changelog_file).

  • version_limit (Integer) (defaults to: 2)

    The number of versions to keep (default: 2).



21
22
23
24
25
# File 'lib/reissue/changelog_updater.rb', line 21

def call(version, date: "Unreleased", changes: {}, changelog_file: @changelog_file, version_limit: 2)
  update(version, date:, changes:, version_limit:)
  write(changelog_file)
  changelog
end

#finalize(date: Date.today, changelog_file: @changelog_file) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/reissue/changelog_updater.rb', line 27

def finalize(date: Date.today, changelog_file: @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"].max_by { |v| ::Gem::Version.new(v["version"]) }
  version_date = version["date"]
  if version_date.nil? || version_date == "Unreleased"
    changelog["versions"].find do |v|
      v["version"] == version["version"]
    end["date"] = date
  end
  write
  changelog
end

#reformat(result_file = @changelog_file, version_limit: 2) ⇒ Hash

Reformats the changelog file to ensure it is correctly formatted.

Parameters:

  • changelog_file (String)

    The path to the changelog file (default: @changelog_file).

Returns:

  • (Hash)

    The parsed changelog.



60
61
62
63
64
65
# File 'lib/reissue/changelog_updater.rb', line 60

def reformat(result_file = @changelog_file, version_limit: 2)
  @changelog = Parser.parse(File.read(@changelog_file))
  changelog["versions"] = changelog["versions"].first(version_limit)
  write(result_file)
  changelog
end

#to_sString

Returns the string representation of the changelog.

Returns:

  • (String)

    The Markdown string representation of the changelog.



70
71
72
# File 'lib/reissue/changelog_updater.rb', line 70

def to_s
  Printer.new(changelog).to_s
end

#update(version, date: "Unreleased", changes: {}, version_limit: 2) ⇒ Hash

Updates the changelog with a new version and its changes.

Parameters:

  • version (String)

    The version number.

  • date (String) (defaults to: "Unreleased")

    The release date (default: “Unreleased”).

  • changes (Hash) (defaults to: {})

    The changes for the version (default: {}).

  • version_limit (Integer) (defaults to: 2)

    The number of versions to keep (default: 2).

Returns:

  • (Hash)

    The updated changelog.



48
49
50
51
52
53
54
# File 'lib/reissue/changelog_updater.rb', line 48

def update(version, date: "Unreleased", changes: {}, version_limit: 2)
  @changelog = Parser.parse(File.read(@changelog_file))

  changelog["versions"].unshift({"version" => version, "date" => date, "changes" => changes})
  changelog["versions"] = changelog["versions"].first(version_limit)
  changelog
end

#write(changelog_file = @changelog_file) ⇒ Object

Writes the changelog to the specified file.

Parameters:

  • changelog_file (String) (defaults to: @changelog_file)

    The path to the changelog file (default: @changelog_file).



77
78
79
# File 'lib/reissue/changelog_updater.rb', line 77

def write(changelog_file = @changelog_file)
  File.write(changelog_file, to_s)
end