Class: Kettle::Dev::PreReleaseCLI

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

Overview

PreReleaseCLI: run pre-release checks before invoking full release workflow.
Checks:
1) Normalize Markdown image URLs using Addressable normalization.
2) Validate Markdown image links resolve via HTTP(S) HEAD.

Usage: Kettle::Dev::PreReleaseCLI.new(check_num: 1).run

Defined Under Namespace

Modules: HTTP, Markdown

Instance Method Summary collapse

Constructor Details

#initialize(check_num: 1) ⇒ PreReleaseCLI

Returns a new instance of PreReleaseCLI.

Parameters:

  • check_num (Integer) (defaults to: 1)

    1-based index to resume from



145
146
147
148
# File 'lib/kettle/dev/pre_release_cli.rb', line 145

def initialize(check_num: 1)
  @check_num = (check_num || 1).to_i
  @check_num = 1 if @check_num < 1
end

Instance Method Details

#check_markdown_images_http!void

This method returns an undefined value.

Check 2: Validate Markdown image links by HTTP HEAD (no rescue for parse failures)



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/kettle/dev/pre_release_cli.rb', line 221

def check_markdown_images_http!
  puts "[kettle-pre-release] Check 2: Validate Markdown image links (HTTP HEAD)"
  urls = [
    Markdown.extract_image_urls_from_files("**/*.md"),
    Markdown.extract_image_urls_from_files("**/*.md.example"),
  ].flatten.uniq
  puts "[kettle-pre-release] Found #{urls.size} unique image URL(s)."
  failures = []
  urls.each do |url|
    print("  -> #{url}")
    ok = HTTP.head_ok?(url)
    if ok
      puts "OK"
    else
      puts "FAIL"
      failures << url
    end
  end
  if failures.any?
    warn("[kettle-pre-release] #{failures.size} image URL(s) failed validation:")
    failures.each { |u| warn("  - #{u}") }
    Kettle::Dev::ExitAdapter.abort("Image link validation failed")
  else
    puts "[kettle-pre-release] All image links validated."
  end
  nil
end

#check_markdown_uri_normalization!void

This method returns an undefined value.

Check 1: Normalize Markdown image URLs
Compares URLs to Addressable-normalized form and rewrites Markdown when needed.



172
173
174
175
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
# File 'lib/kettle/dev/pre_release_cli.rb', line 172

def check_markdown_uri_normalization!
  puts "[kettle-pre-release] Check 1: Normalize Markdown image URLs"
  files = Dir.glob(["**/*.md", "**/*.md.example"])
  changed = []
  total_candidates = 0

  files.each do |file|
    begin
      original = File.read(file)
    rescue StandardError => e
      warn("[kettle-pre-release] Could not read #{file}: #{e.class}: #{e.message}")
      next
    end

    text = original.dup
    urls = Markdown.extract_image_urls_from_text(text)
    next if urls.empty?

    total_candidates += urls.size
    updated = text.dup
    modified = false

    urls.each do |url_str|
      addr = Addressable::URI.parse(url_str)
      normalized = addr.normalize.to_s
      next if normalized == url_str

      # Replace exact occurrences of the URL in the markdown content
      updated.gsub!(url_str, normalized)
      modified = true
      puts "  -> #{file}: normalized #{url_str} -> #{normalized}"
    end

    if modified && updated != original
      begin
        File.write(file, updated)
        changed << file
      rescue StandardError => e
        warn("[kettle-pre-release] Could not write #{file}: #{e.class}: #{e.message}")
      end
    end
  end

  puts "[kettle-pre-release] Normalization candidates: #{total_candidates}. Files changed: #{changed.uniq.size}."
  nil
end

#runvoid

This method returns an undefined value.

Execute configured checks starting from @check_num.

Raises:

  • (ArgumentError)


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/kettle/dev/pre_release_cli.rb', line 152

def run
  checks = []
  checks << method(:check_markdown_uri_normalization!)
  checks << method(:check_markdown_images_http!)

  start = @check_num
  raise ArgumentError, "check_num must be >= 1" if start < 1

  begin_idx = start - 1
  checks[begin_idx..-1].each_with_index do |check, i|
    idx = begin_idx + i + 1
    puts "[kettle-pre-release] Running check ##{idx} of #{checks.size}"
    check.call
  end
  nil
end