<commit_before>module RubyInstaller
module Build
class Release
  def hfile(ver)
    "CHANGELOG-#{ver}.md"
  end

  def version_regex
    '[\w]+-(?<ver>\d+\.\d+\.\d+(\.[a-z]\w*)?-[\d\w]+)'
  end

  def headline_regex(rel=nil)
    "^(?<pre>[^\\w]*)(?<release>#{rel ? Regexp.escape(rel) : version_regex})(?<sp1>[^\\w]+)(?<date>[2Y][0Y][0-9Y][0-9Y]-[0-1M][0-9M]-[0-3D][0-9D])(?<sp2>[ \\w]*)$"
  end

  def reldate
    Time.now.strftime("%Y-%m-%d")
  end

  def release_text(rel)
    ver = rel_to_ver(rel)
    m = File.read(hfile(ver)).match(/(?<annotation>#{headline_regex(rel)}.*?)(#{headline_regex}|\z)/m) || raise("Unable to find release #{rel.inspect} in #{hfile(ver)}")
    m[:annotation]
  end

  def release_name(rel)
    ver = rel_to_ver(rel)
    m = File.read(hfile(ver)).match(/#{headline_regex(rel)}/)
    m[:release]
  end

  def rel_to_ver(rel)
    m = rel.match(/^#{version_regex}$/)
    raise "invalid version string #{rel.inspect}" unless m
    m[:ver][0, 3] # Extract major and minor version "2.4" etc.
  end

  def update_history(rel)
    hfile = hfile(rel_to_ver(rel))
    hin = File.read(hfile)
    hout = hin.sub(/#{headline_regex(rel)}/) do
      $1 + $2 + $3 + reldate + $5
    end
    if hout != hin
      $stderr.puts "Updating #{hfile} for release."
      File.write(hfile, hout)
      Rake::FileUtilsExt.sh "git", "commit", hfile, "-m", "Update release date in #{hfile}"
    end
  end

  def tag_version(rel)
    $stderr.puts "Tag release #{rel} with annotation:"
    rt = release_text(rel).gsub(/\A[# ]+/, "")
    $stderr.puts(rt.gsub(/^/, "    "))
    IO.popen(["git", "tag", "--file=-", rel, "--cleanup=whitespace"], "w") do |fd|
      fd.write rt
    end
  end

  CONTENT_TYPE_FOR_EXT = {
    ".exe" => "application/vnd.microsoft.portable-executable",
    ".asc" => "application/pgp-signature",
    ".7z" => "application/zip",
    ".yml" => "application/x-yaml",
  }

  def upload_to_github(tag:, repo:, token: nil, files:)
    require "octokit"

    client = Octokit::Client.new(access_token: token)
    release = client.releases(repo).find{|r| r.tag_name==tag }
    $stderr.puts "#{ release ? "Add to" : "Create" } github release #{tag}"<commit_msg>Fix finding the "rubyinstaller-head" release on github<commit_after>module RubyInstaller
module Build
class Release
  def hfile(ver)
    "CHANGELOG-#{ver}.md"
  end

  def version_regex
    '[\w]+-(?<ver>\d+\.\d+\.\d+(\.[a-z]\w*)?-[\d\w]+)'
  end

  def headline_regex(rel=nil)
    "^(?<pre>[^\\w]*)(?<release>#{rel ? Regexp.escape(rel) : version_regex})(?<sp1>[^\\w]+)(?<date>[2Y][0Y][0-9Y][0-9Y]-[0-1M][0-9M]-[0-3D][0-9D])(?<sp2>[ \\w]*)$"
  end

  def reldate
    Time.now.strftime("%Y-%m-%d")
  end

  def release_text(rel)
    ver = rel_to_ver(rel)
    m = File.read(hfile(ver)).match(/(?<annotation>#{headline_regex(rel)}.*?)(#{headline_regex}|\z)/m) || raise("Unable to find release #{rel.inspect} in #{hfile(ver)}")
    m[:annotation]
  end

  def release_name(rel)
    ver = rel_to_ver(rel)
    m = File.read(hfile(ver)).match(/#{headline_regex(rel)}/)
    m[:release]
  end

  def rel_to_ver(rel)
    m = rel.match(/^#{version_regex}$/)
    raise "invalid version string #{rel.inspect}" unless m
    m[:ver][0, 3] # Extract major and minor version "2.4" etc.
  end

  def update_history(rel)
    hfile = hfile(rel_to_ver(rel))
    hin = File.read(hfile)
    hout = hin.sub(/#{headline_regex(rel)}/) do
      $1 + $2 + $3 + reldate + $5
    end
    if hout != hin
      $stderr.puts "Updating #{hfile} for release."
      File.write(hfile, hout)
      Rake::FileUtilsExt.sh "git", "commit", hfile, "-m", "Update release date in #{hfile}"
    end
  end

  def tag_version(rel)
    $stderr.puts "Tag release #{rel} with annotation:"
    rt = release_text(rel).gsub(/\A[# ]+/, "")
    $stderr.puts(rt.gsub(/^/, "    "))
    IO.popen(["git", "tag", "--file=-", rel, "--cleanup=whitespace"], "w") do |fd|
      fd.write rt
    end
  end

  CONTENT_TYPE_FOR_EXT = {
    ".exe" => "application/vnd.microsoft.portable-executable",
    ".asc" => "application/pgp-signature",
    ".7z" => "application/zip",
    ".yml" => "application/x-yaml",
  }

  def upload_to_github(tag:, repo:, token: nil, files:)
    require "octokit"

    client = Octokit::Client.new(access_token: token)
    release = nil
    (1..100).find do |page|
      release = client.releases(repo, page: page).find{|r| r.tag_name==tag }
    end
    $stderr.puts "#{ release ? "Add to" : "Create" } github release #{tag}"
{'fraction_non_alphanumeric': 0.2143337066069429, 'fraction_numerical': 0.012094064949608062, 'mean_word_length': 3.2695984703632885, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6420456', 'n_tokens_mistral': 1833, 'n_tokens_neox': 1770, 'n_words': 333}