lib/semmy/changelog.rb in semmy-0.4.0 vs lib/semmy/changelog.rb in semmy-1.0.0

- old
+ new

@@ -20,11 +20,11 @@ end private def unreleased_section_matcher - /#{config.changelog_unrelased_section_heading}(\s*#{compare_link_matcher})?/ + /#{config.changelog_unreleased_section_heading}(\s*#{compare_link_matcher})?/ end def compare_link_matcher # match markdown link of the form [...](...) /\[[^\]]+\]\([^)]+\)/ @@ -70,27 +70,38 @@ end end UpdateForMinor = Struct.new(:config, :options) do def call(contents) - replace_starting_at(version_line_matcher, + replace_starting_at(Changelog.version_line_matcher(config), contents, unreleased_section) end private def unreleased_section - <<-END.unindent - #{config.changelog_unrelased_section_heading} - - #{compare_link_for_master} - - #{config.changelog_unrelased_section_blank_slate} - - #{link_to_changelog_on_previous_minor_stable_branch} - END + # Once Ruby < 2.3 support is dropped, this can be rewritten + # as: + # + # <<~END + # #{config.changelog_unreleased_section_heading} + # + # #{compare_link_for_master} + # + # #{config.changelog_unreleased_section_blank_slate} + # + # #{link_to_changelog_on_previous_minor_stable_branch} + # END + # + # `unindent` cannot handle line breaks in interpolated values correctly. + [ + config.changelog_unreleased_section_heading, + compare_link_for_master, + config.changelog_unreleased_section_blank_slate, + link_to_changelog_on_previous_minor_stable_branch + ].join("\n\n") << "\n" end def compare_link_for_master Changelog.compare_link(config, homepage: options[:homepage], @@ -110,26 +121,62 @@ def previous_stable_branch_name VersionString.previous_stable_branch_name(options[:version], config.stable_branch_name) end - def version_line_matcher - Regexp.new(config.changelog_version_section_heading % { - version: '([0-9.]+)' - }) - end - def replace_starting_at(line_matcher, text, inserted_text) unless text =~ line_matcher fail(InsertPointNotFound, 'Insert point not found.') end [text.split(line_matcher).first, inserted_text].join end end + InsertUnreleasedSection = Struct.new(:config) do + def call(contents) + insert_before(Changelog.version_line_matcher(config), + contents, + config.changelog_unreleased_section_heading << "\n") + end + + private + + def insert_before(line_matcher, text, inserted_text) + text.dup.tap do |result| + unless (result.sub!(line_matcher, inserted_text + "\n\\0")) + fail(InsertPointNotFound, + 'Insert point not found.') + end + end + end + end + + ReplaceMinorStableBranchWithMajorStableBranch = Struct.new(:config, :options) do + def call(contents) + contents.gsub(minor_stable_branch(options[:version]), + major_stable_branch(options[:version])) + end + + private + + def minor_stable_branch(version) + VersionString.previous_stable_branch_name(version, config.stable_branch_name) + end + + def major_stable_branch(version) + config.stable_branch_name % VersionString.components(version).merge(minor: 'x') + end + end + def version_tag(version) "v#{version}" + end + + def version_line_matcher(config) + Regexp.new(config.changelog_version_section_heading % { + version: '([0-9.]+)' + }) end def compare_link(config, interpolations) "[Compare changes](#{compare_url(config, interpolations)})" end