Class: Kettle::Changelog::CLI
- Inherits:
-
Object
- Object
- Kettle::Changelog::CLI
- Defined in:
- lib/kettle/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]:"- CHANGELOG_VERSION_PATTERN =
/\d+\.\d+\.\d+(?:[.-][0-9A-Za-z]+)*/- CHANGELOG_VERSION_PATTERN_SOURCE =
CHANGELOG_VERSION_PATTERN.source
- LINK_REF_DEF_RE =
Matches a Markdown link-reference definition line, e.g.
[key]: https://... /^\s*\[[^\]]+\]:\s+\S+/- DEEP_HEADING_RE =
Matches an ATX heading at H4 or deeper (####, #####, ...)
/^\#{4,}\s/
Instance Method Summary collapse
-
#initialize(strict: true, enforce_coverage_thresholds: true, update_prep: false, reformat_only: false, historical_backfill_version: nil, version: nil, root: Kettle::Dev::CIHelpers.project_root, refresh_cache: false, yes: false, event_stream: nil) ⇒ CLI
constructor
Initialize the changelog CLI Sets up paths for CHANGELOG.md and coverage.json.
- #pending_release_status ⇒ Object
- #release_state ⇒ Object
- #release_state_table(state = release_state) ⇒ Object
-
#run ⇒ void
Main entry point to update CHANGELOG.md.
Constructor Details
#initialize(strict: true, enforce_coverage_thresholds: true, update_prep: false, reformat_only: false, historical_backfill_version: nil, version: nil, root: Kettle::Dev::CIHelpers.project_root, refresh_cache: false, yes: false, event_stream: nil) ⇒ CLI
Initialize the changelog CLI Sets up paths for CHANGELOG.md and coverage.json
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/kettle/changelog/cli.rb', line 35 def initialize(strict: true, enforce_coverage_thresholds: true, update_prep: false, reformat_only: false, historical_backfill_version: nil, version: nil, root: Kettle::Dev::CIHelpers.project_root, refresh_cache: false, yes: false, event_stream: nil) @root = root @changelog_path = resolved_changelog_path @coverage_root = resolved_coverage_root @coverage_path = File.join(@coverage_root, "coverage", "coverage.json") @strict = strict @enforce_coverage_thresholds = enforce_coverage_thresholds @update_prep = update_prep @reformat_only = reformat_only @historical_backfill_version = Kettle::Dev::Versioning.normalize_explicit_version(historical_backfill_version) @version_override = Kettle::Dev::Versioning.normalize_explicit_version(version) @refresh_cache = refresh_cache @yes = !!yes @event_recorder = Kettle::Ndjson.event_recorder(event_stream, phase_timings: []) end |
Instance Method Details
#pending_release_status ⇒ Object
172 173 174 |
# File 'lib/kettle/changelog/cli.rb', line 172 def pending_release_status release_state end |
#release_state ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/kettle/changelog/cli.rb', line 176 def release_state changelog_present = ensure_changelog_for_release_state! version = detect_version gem_name = detect_gem_name unless changelog_present latest_overall, latest_for_series, latest_for_major = latest_released_versions(gem_name, version) latest_target = latest_release_target(version, latest_overall, latest_for_series, latest_for_major) ahead = commits_ahead_of_release(latest_target || latest_overall) return { root: @root, gem_name: gem_name, version: version, changelog_present: false, pending: false, pending_release: false, unreleased_entries: false, prepared_release_pending: false, latest_changelog_version: nil, latest_released: latest_target || latest_overall, latest_released_overall: latest_overall, latest_released_for_current_major: latest_for_major, latest_released_for_current_series: latest_for_series, latest_release_target: latest_target, ahead: ahead } end changelog = File.read(@changelog_path) unreleased_block, _before, after = extract_unreleased(changelog) unreleased_entries = unreleased_block_has_entries?(unreleased_block) latest_changelog_version = detect_previous_version(after.to_s) release_lookup_version = latest_changelog_version || version latest_overall, latest_for_series, latest_for_major = latest_released_versions(gem_name, release_lookup_version) latest_target = latest_release_target(release_lookup_version, latest_overall, latest_for_series, latest_for_major) prepared_release_pending = !!latest_changelog_version && latest_target != latest_changelog_version ahead = commits_ahead_of_release(latest_target || latest_overall) { root: @root, gem_name: gem_name, version: version, changelog_present: true, pending: unreleased_entries || prepared_release_pending, pending_release: unreleased_entries || prepared_release_pending, unreleased_entries: unreleased_entries, prepared_release_pending: prepared_release_pending, latest_changelog_version: latest_changelog_version, latest_released: latest_target || latest_overall, latest_released_overall: latest_overall, latest_released_for_current_major: latest_for_major, latest_released_for_current_series: latest_for_series, latest_release_target: latest_target, ahead: ahead } end |
#release_state_table(state = release_state) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/kettle/changelog/cli.rb', line 232 def release_state_table(state = release_state) rows = [ ["gem", "version.rb", "latest released", "latest changelog", "ahead", "unreleased", "prepared", "pending"], [ state.fetch(:gem_name), state.fetch(:version), state.fetch(:latest_released) || "unknown", state.fetch(:latest_changelog_version) || "none", state.fetch(:ahead, nil).nil? ? "unknown" : state.fetch(:ahead).to_s, yes_no(state.fetch(:unreleased_entries)), yes_no(state.fetch(:prepared_release_pending)), yes_no(state.fetch(:pending_release)) ] ] widths = rows.transpose.map { |column| column.map(&:length).max } rows.map.with_index do |row, index| line = row.each_with_index.map { |value, i| value.ljust(widths.fetch(i)) }.join(" ").rstrip if index == 0 [line, widths.map { |width| "-" * width }.join(" ")].join("\n") else line end end.join("\n") end |
#run ⇒ void
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.
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/kettle/changelog/cli.rb', line 57 def run if @reformat_only reformat_changelog!(File.read(@changelog_path)) emit_changelog_event(action: "reformat", status: "ok", plan: "reformat_only") return end if @historical_backfill_version backfill_historical_release!(@historical_backfill_version) return end version = detect_version 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 changelog = File.read(@changelog_path) plan = @update_prep ? explicit_update_prep_plan(changelog) : detect_plan(changelog, version) confirm_plan!(plan) if plan.fetch(:action) == :reformat_only reformat_changelog!(changelog) return end if plan.fetch(:action) == :rollback_prepared_release rollback_prepared_release!(changelog, owner, repo, version) emit_changelog_event(action: "rollback", status: "ok", version: plan.fetch(:latest_changelog_version), plan: "rollback_prepared_release") return end line_cov_line, branch_cov_line = coverage_lines yard_line = yard_percent_documented if plan.fetch(:action) == :update_prepared_release update_prepared_release!(changelog, today, owner, repo, line_cov_line, branch_cov_line, yard_line) emit_changelog_event(action: "update", status: "ok", version: version, plan: plan.fetch(:action).to_s) return 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 = after_lines[unreleased_ref_idx..-1].join updated << preserved_body << 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) emit_changelog_event(action: "update", status: "ok", version: version, plan: plan.fetch(:action).to_s) puts "CHANGELOG.md updated with v#{version} section." end |