<commit_before># typed: false
# frozen_string_literal: true

require "cask"
require "cli/parser"
require "utils/tar"

module Homebrew
  module_function

  def bump_cask_pr_args
    Homebrew::CLI::Parser.new do
      usage_banner <<~EOS
        `bump-cask-pr` [<options>] [<cask>]

        Create a pull request to update <cask> with a new version.

        A best effort to determine the <SHA-256> will be made if the value is not
        supplied by the user.
      EOS
      switch "-n", "--dry-run",
             description: "Print what would be done rather than doing it."
      switch "--write",
             description: "Make the expected file modifications without taking any Git actions."
      switch "--commit",
             depends_on:  "--write",
             description: "When passed with `--write`, generate a new commit after writing changes "\
                          "to the cask file."
      switch "--no-audit",
             description: "Don't run `brew cask audit` before opening the PR."
      switch "--online",
             description: "Run `brew cask audit --online` before opening the PR."
      switch "--no-style",
             description: "Don't run `brew cask style --fix` before opening the PR."
      switch "--no-browse",
             description: "Print the pull request URL instead of opening in a browser."
      switch "--no-fork",
             description: "Don't try to fork the repository."
      flag   "--version=",
             description: "Specify the new <version> for the cask."
      flag   "--message=",
             description: "Append <message> to the default pull request message."
      flag   "--url=",
             description: "Specify the <URL> for the new download."
      flag   "--sha256=",
             description: "Specify the <SHA-256> checksum of the new download."
      switch "-f", "--force",
             description: "Ignore duplicate open PRs."

      conflicts "--dry-run", "--write"
      conflicts "--no-audit", "--online"
      named 1
    end
  end

  def bump_cask_pr
    args = bump_cask_pr_args.parse

    # As this command is simplifying user-run commands then let's just use a
    # user path, too.
    ENV["PATH"] = ENV["HOMEBREW_PATH"]

    # Use the user's browser, too.
    ENV["BROWSER"] = Homebrew::EnvConfig.browser

    cask = args.named.to_casks.first
    new_version = args.version
    new_version = "latest" if new_version == ":latest"
    new_version = Cask::DSL::Version.new(new_version)
    new_base_url = args.url
    new_hash = args.sha256

    old_version = cask.version
    old_hash = cask.sha256

    tap_full_name = cask.tap&.full_name
    origin_branch = Utils::Git.origin_branch(cask.tap.path) if cask.tap
    origin_branch ||= "origin/master"
    previous_branch = "-"

    check_open_pull_requests(cask, tap_full_name, args: args)

    odie "#{cask}: no --version= argument specified!" if new_version.empty?

    check_closed_pull_requests(cask, tap_full_name, version: new_version, args: args) unless new_version.latest?

    if new_version == old_version
      odie <<~EOS
        You need to bump this cask manually since the new version
        and old version are both #{new_version}.
      EOS
    elsif old_version.latest?
      opoo "No --url= argument specified!" unless new_base_url
    elsif new_version.latest?
      opoo "Ignoring specified --sha256= argument." if new_hash
    elsif Version.new(new_version) < Version.new(old_version)
      odie <<~EOS
        You need to bump this cask manually since changing the
        version from #{old_version} to #{new_version} would be a downgrade.
      EOS
    end

    old_contents = File.read(cask.sourcefile_path)

    replacement_pairs = []

    replacement_pairs << if old_version.latest?
      [
        "version :latest",
        "version \"#{new_version}\"",
      ]
    elsif new_version.latest?
      [
        "version \"#{old_version}\"",
        "version :latest",
      ]
    else
      [
        old_version,
        new_version,
      ]
    end

    if new_base_url.present?
      m = /^ +url "(.+?)"\n/m.match(old_contents)
      odie "Could not find old URL in cask!" if m.nil?

      old_base_url = m.captures.first

      replacement_pairs << [
        /#{Regexp.escape(old_base_url)}/,
        new_base_url,
      ]
    end

    if !new_version.latest? && (new_hash.nil? || cask.languages.present?)
      tmp_contents = Utils::Inreplace.inreplace_pairs(cask.sourcefile_path,
                                                      replacement_pairs.uniq.compact,
                                                      read_only_run: true,
                                                      silent:        true)

      tmp_cask = Cask::CaskLoader.load(tmp_contents)
      tmp_url = tmp_cask.url.to_s

      if new_hash.nil?
        resource_path = fetch_resource(cask, new_version, tmp_url)
        Utils::Tar.validate_file(resource_path)
        new_hash = resource_path.sha256
      end

      cask.languages.each do |language|
        next if language == cask.language

        tmp_cask.config.languages = [language]

        lang_cask = Cask::CaskLoader.load(tmp_contents)
        lang_url = lang_cask.url.to_s
        lang_old_hash = lang_cask.sha256

        resource_path = fetch_resource(cask, new_version, lang_url)
        Utils::Tar.validate_file(resource_path)
        lang_new_hash = resource_path.sha256
<commit_msg>bump-cask-pr: fix for casks that have multiple languages<commit_after># typed: false
# frozen_string_literal: true

require "cask"
require "cli/parser"
require "utils/tar"

module Homebrew
  module_function

  def bump_cask_pr_args
    Homebrew::CLI::Parser.new do
      usage_banner <<~EOS
        `bump-cask-pr` [<options>] [<cask>]

        Create a pull request to update <cask> with a new version.

        A best effort to determine the <SHA-256> will be made if the value is not
        supplied by the user.
      EOS
      switch "-n", "--dry-run",
             description: "Print what would be done rather than doing it."
      switch "--write",
             description: "Make the expected file modifications without taking any Git actions."
      switch "--commit",
             depends_on:  "--write",
             description: "When passed with `--write`, generate a new commit after writing changes "\
                          "to the cask file."
      switch "--no-audit",
             description: "Don't run `brew cask audit` before opening the PR."
      switch "--online",
             description: "Run `brew cask audit --online` before opening the PR."
      switch "--no-style",
             description: "Don't run `brew cask style --fix` before opening the PR."
      switch "--no-browse",
             description: "Print the pull request URL instead of opening in a browser."
      switch "--no-fork",
             description: "Don't try to fork the repository."
      flag   "--version=",
             description: "Specify the new <version> for the cask."
      flag   "--message=",
             description: "Append <message> to the default pull request message."
      flag   "--url=",
             description: "Specify the <URL> for the new download."
      flag   "--sha256=",
             description: "Specify the <SHA-256> checksum of the new download."
      switch "-f", "--force",
             description: "Ignore duplicate open PRs."

      conflicts "--dry-run", "--write"
      conflicts "--no-audit", "--online"
      named 1
    end
  end

  def bump_cask_pr
    args = bump_cask_pr_args.parse

    # As this command is simplifying user-run commands then let's just use a
    # user path, too.
    ENV["PATH"] = ENV["HOMEBREW_PATH"]

    # Use the user's browser, too.
    ENV["BROWSER"] = Homebrew::EnvConfig.browser

    cask = args.named.to_casks.first
    new_version = args.version
    new_version = "latest" if new_version == ":latest"
    new_version = Cask::DSL::Version.new(new_version)
    new_base_url = args.url
    new_hash = args.sha256

    old_version = cask.version
    old_hash = cask.sha256

    tap_full_name = cask.tap&.full_name
    origin_branch = Utils::Git.origin_branch(cask.tap.path) if cask.tap
    origin_branch ||= "origin/master"
    previous_branch = "-"

    check_open_pull_requests(cask, tap_full_name, args: args)

    odie "#{cask}: no --version= argument specified!" if new_version.empty?

    check_closed_pull_requests(cask, tap_full_name, version: new_version, args: args) unless new_version.latest?

    if new_version == old_version
      odie <<~EOS
        You need to bump this cask manually since the new version
        and old version are both #{new_version}.
      EOS
    elsif old_version.latest?
      opoo "No --url= argument specified!" unless new_base_url
    elsif new_version.latest?
      opoo "Ignoring specified --sha256= argument." if new_hash
    elsif Version.new(new_version) < Version.new(old_version)
      odie <<~EOS
        You need to bump this cask manually since changing the
        version from #{old_version} to #{new_version} would be a downgrade.
      EOS
    end

    old_contents = File.read(cask.sourcefile_path)

    replacement_pairs = []

    replacement_pairs << if old_version.latest?
      [
        "version :latest",
        "version \"#{new_version}\"",
      ]
    elsif new_version.latest?
      [
        "version \"#{old_version}\"",
        "version :latest",
      ]
    else
      [
        old_version,
        new_version,
      ]
    end

    if new_base_url.present?
      m = /^ +url "(.+?)"\n/m.match(old_contents)
      odie "Could not find old URL in cask!" if m.nil?

      old_base_url = m.captures.first

      replacement_pairs << [
        /#{Regexp.escape(old_base_url)}/,
        new_base_url,
      ]
    end

    if !new_version.latest? && (new_hash.nil? || cask.languages.present?)
      tmp_contents = Utils::Inreplace.inreplace_pairs(cask.sourcefile_path,
                                                      replacement_pairs.uniq.compact,
                                                      read_only_run: true,
                                                      silent:        true)

      tmp_cask = Cask::CaskLoader.load(tmp_contents)
      tmp_config = cask.config
      tmp_url = tmp_cask.url.to_s

      if new_hash.nil?
        resource_path = fetch_resource(cask, new_version, tmp_url)
        Utils::Tar.validate_file(resource_path)
        new_hash = resource_path.sha256
      end

      cask.languages.each do |language|
        next if language == cask.language

        lang_config = tmp_config.merge(Cask::Config.new(explicit: { languages: [language] }))
        lang_cask = Cask::CaskLoader.load(tmp_contents)
        lang_cask.config = lang_config
        lang_url = lang_cask.url.to_s
        lang_old_hash = lang_cask.sha256

        resource_path = fetch_resource(cask, new_version, lang_url)
        Utils::Tar.validate_file(resource_path)
        lang_new_hash = resource_path.sha256

{'fraction_non_alphanumeric': 0.10297191674997729, 'fraction_numerical': 0.005089521039716441, 'mean_word_length': 2.214723926380368, 'pattern_counts': {'":': 2, '<': 41, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1774357', 'n_tokens_mistral': 3379, 'n_tokens_neox': 3250, 'n_words': 915}