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.



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

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

Instance Attribute Details

#changelogObject (readonly)

Returns the value of attribute changelog.



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

def changelog
  @changelog
end

Instance Method Details

#call(version, date: "Unreleased", changes: {}, changelog_file: @changelog_file, version_limit: 2, retain_changelogs: false, fragment: nil, fragment_directory: nil, tag_pattern: nil) ⇒ 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).

  • fragment (String) (defaults to: nil)

    The fragment source configuration (default: nil).

  • fragment_directory (String) (defaults to: nil)

    @deprecated Use fragment instead



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/reissue/changelog_updater.rb', line 24

def call(version, date: "Unreleased", changes: {}, changelog_file: @changelog_file, version_limit: 2, retain_changelogs: false, fragment: nil, fragment_directory: nil, tag_pattern: nil)
  # Handle deprecation
  if fragment_directory && !fragment
    warn "[DEPRECATION] `fragment_directory` parameter is deprecated. Please use `fragment` instead."
    fragment = fragment_directory
  end

  update(version, date:, changes:, version_limit:, fragment:, tag_pattern:)
  write(changelog_file, retain_changelogs:)

  changelog
end

#finalize(date: Date.today, changelog_file: @changelog_file, retain_changelogs: false, resolved_version: nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/reissue/changelog_updater.rb', line 37

def finalize(date: Date.today, changelog_file: @changelog_file, retain_changelogs: false, resolved_version: nil)
  @changelog = Parser.parse(File.read(changelog_file))

  # find the highest version number and if it is unreleased, update the date
  version = latest_version
  version_value = version["version"]
  version_date = version["date"]

  # In deferred mode, resolved_version replaces "Unreleased" version string
  if resolved_version && version_value == "Unreleased"
    version["version"] = resolved_version
  end

  if version_date.nil? || version_date == "Unreleased"
    changelog["versions"].find do |v|
      v["version"] == version["version"]
    end["date"] = date
  end
  write(changelog_file, retain_changelogs:)
  changelog
end

#reformat(result_file = @changelog_file, version_limit: 2, retain_changelogs: false) ⇒ 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.



90
91
92
93
94
95
# File 'lib/reissue/changelog_updater.rb', line 90

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

#store_current_changelog(version, path) ⇒ Object

Stores the current changelog in a file.

Parameters:

  • version (Hash)

    The version information.

  • path (String, Proc)

    The path to store the changelog or a callable that receives the version and the changelog.



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/reissue/changelog_updater.rb', line 118

def store_current_changelog(version, path)
  if path
    if path.respond_to?(:call)
      path.call(version, to_s)
    else
      require "fileutils"
      path = path.is_a?(String) ? path : "changelogs"
      FileUtils.mkdir_p(path)
      File.write("#{path}/#{version["version"]}.md", to_s)
    end
  end
end

#to_sString

Returns the string representation of the changelog.

Returns:

  • (String)

    The Markdown string representation of the changelog.



100
101
102
# File 'lib/reissue/changelog_updater.rb', line 100

def to_s
  Printer.new(changelog).to_s
end

#update(version, date: "Unreleased", changes: {}, version_limit: 2, fragment: nil, tag_pattern: nil) ⇒ 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).

  • fragment (String) (defaults to: nil)

    The fragment source configuration (default: nil).

Returns:

  • (Hash)

    The updated changelog.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/reissue/changelog_updater.rb', line 67

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

  # Merge fragment changes if source is provided
  merged_changes = changes.dup
  if fragment
    handler = FragmentHandler.for(fragment, tag_pattern:)
    fragment_changes = handler.read
    fragment_changes.each do |section, entries|
      merged_changes[section] ||= []
      merged_changes[section].concat(entries)
    end
  end

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

#write(changelog_file = @changelog_file, retain_changelogs: false) ⇒ 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).



107
108
109
110
111
112
# File 'lib/reissue/changelog_updater.rb', line 107

def write(changelog_file = @changelog_file, retain_changelogs: false)
  if retain_changelogs
    store_current_changelog(latest_version, retain_changelogs)
  end
  File.write(changelog_file, to_s)
end