Class: Kettle::Dev::ChangelogCLI

Inherits:
Object
  • Object
show all
Defined in:
lib/kettle/dev/changelog_cli.rb

Overview

CLI for updating CHANGELOG.md with new version sections

Automatically extracts unreleased changes, formats them into a new version section,
includes coverage and YARD stats, and updates link references.

Constant Summary collapse

UNRELEASED_SECTION_HEADING =
"[Unreleased]:"

Instance Method Summary collapse

Constructor Details

#initialize(strict: true) ⇒ ChangelogCLI

Initialize the changelog CLI
Sets up paths for CHANGELOG.md and coverage.json

Parameters:

  • strict (Boolean) (defaults to: true)

    when true (default), require coverage and yard data; raise errors if unavailable



17
18
19
20
21
22
# File 'lib/kettle/dev/changelog_cli.rb', line 17

def initialize(strict: true)
  @root = Kettle::Dev::CIHelpers.project_root
  @changelog_path = File.join(@root, "CHANGELOG.md")
  @coverage_path = File.join(@root, "coverage", "coverage.json")
  @strict = strict
end

Instance Method Details

#runvoid

This method returns an undefined value.

Main entry point to update CHANGELOG.md

Detects current version, extracts unreleased changes, formats them into
a new version section with coverage/YARD stats, and updates all link references.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/kettle/dev/changelog_cli.rb', line 30

def run
  version = Kettle::Dev::Versioning.detect_version(@root)
  today = Time.now.strftime("%Y-%m-%d")
  owner, repo = Kettle::Dev::CIHelpers.repo_info
  unless owner && repo
    warn("Could not determine GitHub owner/repo from origin remote.")
    warn("Make sure 'origin' points to github.com. Alternatively, set origin or update links manually afterward.")
  end

  line_cov_line, branch_cov_line = coverage_lines
  yard_line = yard_percent_documented

  changelog = File.read(@changelog_path)

  # If the detected version already exists in the changelog, offer reformat-only mode
  if changelog =~ /^## \[#{Regexp.escape(version)}\]/
    warn("CHANGELOG.md already has a section for version #{version}.")
    warn("It appears the version has not been bumped. You can reformat CHANGELOG.md without adding a new release section.")
    print("Proceed with reformat only? [y/N]: ")
    ans = Kettle::Dev::InputAdapter.gets&.strip&.downcase
    if ans == "y" || ans == "yes"
      updated = convert_heading_tag_suffix_to_list(changelog)
      updated = normalize_heading_spacing(updated)
      updated = ensure_footer_spacing(updated)
      updated = updated.rstrip + "\n"
      File.write(@changelog_path, updated)
      puts "CHANGELOG.md reformatted. No new version section added."
      return
    else
      abort("Aborting: version not bumped. Re-run after bumping version or answer 'y' to reformat-only.")
    end
  end

  unreleased_block, before, after = extract_unreleased(changelog)
  if unreleased_block.nil?
    abort("Could not find '## [Unreleased]' section in CHANGELOG.md")
  end

  if unreleased_block.strip.empty?
    warn("No entries found under Unreleased. Creating an empty version section anyway.")
  end

  prev_version = detect_previous_version(after)

  new_section = +""
  new_section << "## [#{version}] - #{today}\n"
  new_section << "- TAG: [v#{version}][#{version}t]\n"
  new_section << "- #{line_cov_line}\n" if line_cov_line
  new_section << "- #{branch_cov_line}\n" if branch_cov_line
  new_section << "- #{yard_line}\n" if yard_line
  new_section << filter_unreleased_sections(unreleased_block)
  # Ensure exactly one blank line separates this new section from the next section
  new_section.rstrip!
  new_section << "\n\n"

  # Reset the Unreleased section to empty category headings
  unreleased_reset = <<~MD
    ## [Unreleased]
    ### Added
    ### Changed
    ### Deprecated
    ### Removed
    ### Fixed
    ### Security
  MD

  # Preserve everything from the first released section down to the line containing the [Unreleased] link ref.
  # Many real-world changelogs intersperse stray link refs between sections; we should keep them.
  updated = before + unreleased_reset + "\n" + new_section
  # Find the [Unreleased]: link-ref line and append everything from the start of the first released section
  # through to the end of the file, but if a [Unreleased]: ref exists, ensure we do not duplicate the
  # section content above it.
  if after && !after.empty?
    # Split 'after' by lines so we can locate the first link-ref to Unreleased
    after_lines = after.lines
    unreleased_ref_idx = after_lines.index { |l| l.start_with?(UNRELEASED_SECTION_HEADING) }
    if unreleased_ref_idx
      # Keep all content prior to the link-ref (older releases and interspersed refs)
      preserved_body = after_lines[0...unreleased_ref_idx].join
      # Then append the tail starting from the Unreleased link-ref line to preserve the footer refs
      preserved_footer = after_lines[unreleased_ref_idx..-1].join
      updated << preserved_body << preserved_footer
    else
      # No Unreleased ref found; just append the remainder as-is
      updated << after
    end
  end

  updated = update_link_refs(updated, owner, repo, prev_version, version)

  # Transform legacy heading suffix tags into list items under headings
  updated = convert_heading_tag_suffix_to_list(updated)

  # Normalize spacing around headings to aid Markdown renderers
  updated = normalize_heading_spacing(updated)

  # Ensure exactly one trailing newline at EOF
  updated = updated.rstrip + "\n"

  File.write(@changelog_path, updated)
  puts "CHANGELOG.md updated with v#{version} section."
end