Class: Reissue::ChangelogUpdater
- Inherits:
-
Object
- Object
- Reissue::ChangelogUpdater
- Defined in:
- lib/reissue/changelog_updater.rb
Overview
Updates the changelog file with new versions and changes.
Instance Attribute Summary collapse
-
#changelog ⇒ Object
readonly
Returns the value of attribute changelog.
Instance Method Summary collapse
-
#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.
- #finalize(date: Date.today, changelog_file: @changelog_file, retain_changelogs: false, resolved_version: nil) ⇒ Object
-
#initialize(changelog_file) ⇒ ChangelogUpdater
constructor
A new instance of ChangelogUpdater.
-
#reformat(result_file = @changelog_file, version_limit: 2, retain_changelogs: false) ⇒ Hash
Reformats the changelog file to ensure it is correctly formatted.
-
#store_current_changelog(version, path) ⇒ Object
Stores the current changelog in a file.
-
#to_s ⇒ String
Returns the string representation of the changelog.
-
#update(version, date: "Unreleased", changes: {}, version_limit: 2, fragment: nil, tag_pattern: nil) ⇒ Hash
Updates the changelog with a new version and its changes.
-
#write(changelog_file = @changelog_file, retain_changelogs: false) ⇒ Object
Writes the changelog to the specified file.
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
#changelog ⇒ Object (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.
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.
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.
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_s ⇒ String
Returns the 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.
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.
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 |