<commit_before># frozen_string_literal: true

require "env_config"
require "cask/config"

module Cask
  class Cmd
    class Upgrade < AbstractCommand
      option "--greedy", :greedy, false
      option "--quiet",  :quiet, false
      option "--force", :force, false
      option "--skip-cask-deps", :skip_cask_deps, false
      option "--dry-run", :dry_run, false

      def initialize(*)
        super
        self.verbose = ($stdout.tty? || verbose?) && !quiet?
      end

      def run
        self.class.upgrade_casks(
          *casks,
          force:          force?,
          greedy:         greedy?,
          dry_run:        dry_run?,
          binaries:       binaries?,
          quarantine:     quarantine?,
          require_sha:    require_sha?,
          skip_cask_deps: skip_cask_deps?,
          verbose:        verbose?,
        )
      end

      def self.upgrade_casks(
        *casks,
        force: false,
        greedy: false,
        dry_run: false,
        skip_cask_deps: false,
        verbose: false,
        binaries: nil,
        quarantine: nil,
        require_sha: nil
      )
        # TODO: Handle this in `CLI::Parser`.
        binaries = Homebrew::EnvConfig.cask_opts_binaries? if binaries.nil?
        quarantine = Homebrew::EnvConfig.cask_opts_quarantine? if quarantine.nil?
        require_sha = Homebrew::EnvConfig.cask_opts_require_sha? if require_sha.nil?

        outdated_casks = if casks.empty?
          Caskroom.casks.select do |cask|
            cask.outdated?(greedy)
          end
        else
          casks.select do |cask|
            raise CaskNotInstalledError, cask unless cask.installed? || force

            cask.outdated?(true)
          end
        end

        return if outdated_casks.empty?

        ohai "Casks with `auto_updates` or `version :latest` will not be upgraded" if casks.empty? && !greedy

        verb = dry_run ? "Would upgrade" : "Upgrading"
        oh1 "#{verb} #{outdated_casks.count} #{"outdated package".pluralize(outdated_casks.count)}:"

        caught_exceptions = []

        upgradable_casks = outdated_casks.map { |c| [CaskLoader.load(c.installed_caskfile), c] }

        puts upgradable_casks
          .map { |(old_cask, new_cask)| "#{new_cask.full_name} #{old_cask.version} -> #{new_cask.version}" }
          .join(", ")
        return if dry_run

        upgradable_casks.each do |(old_cask, new_cask)|
          upgrade_cask(
            old_cask, new_cask,
            binaries: binaries, force: force, skip_cask_deps: skip_cask_deps, verbose: verbose,
            quarantine: quarantine, require_sha: require_sha
          )
        rescue => e
          caught_exceptions << e.exception("#{new_cask.full_name}: #{e}")
          next<commit_msg>cask/cmd/upgrade: output cask upgrades on new line<commit_after># frozen_string_literal: true

require "env_config"
require "cask/config"

module Cask
  class Cmd
    class Upgrade < AbstractCommand
      option "--greedy", :greedy, false
      option "--quiet",  :quiet, false
      option "--force", :force, false
      option "--skip-cask-deps", :skip_cask_deps, false
      option "--dry-run", :dry_run, false

      def initialize(*)
        super
        self.verbose = ($stdout.tty? || verbose?) && !quiet?
      end

      def run
        self.class.upgrade_casks(
          *casks,
          force:          force?,
          greedy:         greedy?,
          dry_run:        dry_run?,
          binaries:       binaries?,
          quarantine:     quarantine?,
          require_sha:    require_sha?,
          skip_cask_deps: skip_cask_deps?,
          verbose:        verbose?,
        )
      end

      def self.upgrade_casks(
        *casks,
        force: false,
        greedy: false,
        dry_run: false,
        skip_cask_deps: false,
        verbose: false,
        binaries: nil,
        quarantine: nil,
        require_sha: nil
      )
        # TODO: Handle this in `CLI::Parser`.
        binaries = Homebrew::EnvConfig.cask_opts_binaries? if binaries.nil?
        quarantine = Homebrew::EnvConfig.cask_opts_quarantine? if quarantine.nil?
        require_sha = Homebrew::EnvConfig.cask_opts_require_sha? if require_sha.nil?

        outdated_casks = if casks.empty?
          Caskroom.casks.select do |cask|
            cask.outdated?(greedy)
          end
        else
          casks.select do |cask|
            raise CaskNotInstalledError, cask unless cask.installed? || force

            cask.outdated?(true)
          end
        end

        return if outdated_casks.empty?

        ohai "Casks with `auto_updates` or `version :latest` will not be upgraded" if casks.empty? && !greedy

        verb = dry_run ? "Would upgrade" : "Upgrading"
        oh1 "#{verb} #{outdated_casks.count} #{"outdated package".pluralize(outdated_casks.count)}:"

        caught_exceptions = []

        upgradable_casks = outdated_casks.map { |c| [CaskLoader.load(c.installed_caskfile), c] }

        puts upgradable_casks
          .map { |(old_cask, new_cask)| "#{new_cask.full_name} #{old_cask.version} -> #{new_cask.version}" }
          .join("\n")
        return if dry_run

        upgradable_casks.each do |(old_cask, new_cask)|
          upgrade_cask(
            old_cask, new_cask,
            binaries: binaries, force: force, skip_cask_deps: skip_cask_deps, verbose: verbose,
            quarantine: quarantine, require_sha: require_sha
          )
        rescue => e
          caught_exceptions << e.exception("#{new_cask.full_name}: #{e}")
          next
{'fraction_non_alphanumeric': 0.10569105691056911, 'fraction_numerical': 0.0003613369467028004, 'mean_word_length': 2.060254284134881, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '2630909', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1751, 'n_words': 385}