Module: Homebrew::Livecheck Private

Includes:
Kernel
Defined in:
livecheck/livecheck.rb,
livecheck/error.rb,
livecheck/strategy.rb,
livecheck/constants.rb,
livecheck/strategy/git.rb,
livecheck/strategy/gnu.rb,
livecheck/strategy/npm.rb,
livecheck/strategy/xml.rb,
livecheck/strategy/cpan.rb,
livecheck/strategy/json.rb,
livecheck/strategy/pypi.rb,
livecheck/strategy/xorg.rb,
livecheck/strategy/yaml.rb,
livecheck/strategy/crate.rb,
livecheck/strategy/gnome.rb,
livecheck/skip_conditions.rb,
livecheck/strategy/apache.rb,
livecheck/strategy/hackage.rb,
livecheck/strategy/sparkle.rb,
livecheck/livecheck_version.rb,
livecheck/strategy/bitbucket.rb,
livecheck/strategy/launchpad.rb,
livecheck/strategy/page_match.rb,
livecheck/strategy/sourceforge.rb,
livecheck/strategy/header_match.rb,
livecheck/strategy/extract_plist.rb,
livecheck/strategy/github_latest.rb,
livecheck/strategy/github_releases.rb,
livecheck/strategy/electron_builder.rb,
livecheck/livecheck.rbi

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

The Livecheck module consists of methods used by the brew livecheck command. These methods print the requested livecheck information for formulae.

Defined Under Namespace

Modules: Constants, SkipConditions, Strategy Classes: Error, LivecheckVersion

Constant Summary collapse

GITEA_INSTANCES =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.let(%w[
  codeberg.org
  gitea.com
  opendev.org
  tildegit.org
].freeze, T::Array[String])
GOGS_INSTANCES =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.let(%w[
  lolg.it
].freeze, T::Array[String])
STRATEGY_SYMBOLS_TO_SKIP_PREPROCESS_URL =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.let([
  :extract_plist,
  :github_latest,
  :header_match,
  :json,
  :page_match,
  :sparkle,
  :xml,
  :yaml,
].freeze, T::Array[Symbol])
UNSTABLE_VERSION_KEYWORDS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.let(%w[
  alpha
  beta
  bpo
  dev
  experimental
  prerelease
  preview
  rc
].freeze, T::Array[String])
GitHubLatest =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

Homebrew::Livecheck::Strategy::GithubLatest
GitHubReleases =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

Homebrew::Livecheck::Strategy::GithubReleases

Class Method Summary collapse

Methods included from Kernel

#disk_usage_readable, #ensure_executable!, #ensure_formula_installed!, #exec_browser, #exec_editor, #ignore_interrupts, #interactive_shell, #number_readable, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #paths, #pretty_duration, #pretty_installed, #pretty_outdated, #pretty_uninstalled, #quiet_system, #redact_secrets, #redirect_stdout, #require?, #safe_system, #tap_and_name_comparison, #truncate_text_to_approximate_size, #which, #which_all, #which_editor, #with_custom_locale, #with_env, #with_homebrew_path

Class Method Details

.cask_name(cask, full_name: false) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns the fully-qualified name of a cask if the full_name argument is provided; returns the name otherwise.

Parameters:

  • cask (Cask::Cask)
  • full_name (Boolean) (defaults to: false)

Returns:



449
450
451
# File 'livecheck/livecheck.rb', line 449

def cask_name(cask, full_name: false)
  full_name ? cask.full_name : cask.token
end

.checkable_urls(package_or_resource) ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns an Array containing the formula/cask/resource URLs that can be used by livecheck.

Parameters:

Returns:



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'livecheck/livecheck.rb', line 549

def checkable_urls(package_or_resource)
  urls = []

  case package_or_resource
  when Formula
    if package_or_resource.stable
      urls << T.must(package_or_resource.stable).url
      urls.concat(T.must(package_or_resource.stable).mirrors)
    end
    urls << T.must(package_or_resource.head).url if package_or_resource.head
    urls << package_or_resource.homepage if package_or_resource.homepage
  when Cask::Cask
    urls << package_or_resource.url.to_s if package_or_resource.url
    urls << package_or_resource.homepage if package_or_resource.homepage
  when Resource
    urls << package_or_resource.url
  else
    T.absurd(package_or_resource)
  end

  urls.compact.uniq
end

.formula_name(formula, full_name: false) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns the fully-qualified name of a formula if the full_name argument is provided; returns the name otherwise.

Parameters:

  • formula (Formula)
  • full_name (Boolean) (defaults to: false)

Returns:



456
457
458
# File 'livecheck/livecheck.rb', line 456

def formula_name(formula, full_name: false)
  full_name ? formula.full_name : formula.name
end

.latest_version(formula_or_cask, referenced_formula_or_cask: nil, livecheck_references: [], json: false, full_name: false, verbose: false, debug: false) ⇒ Hash{Symbol => T.untyped}?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Identifies the latest version of the formula/cask and returns a Hash containing the version information. Returns nil if a latest version couldn’t be found.

Parameters:

  • formula_or_cask (Formula, Cask::Cask)
  • referenced_formula_or_cask (Formula, Cask::Cask, nil) (defaults to: nil)
  • livecheck_references (Array<Formula, Cask::Cask>) (defaults to: [])
  • json (Boolean) (defaults to: false)
  • full_name (Boolean) (defaults to: false)
  • verbose (Boolean) (defaults to: false)
  • debug (Boolean) (defaults to: false)

Returns:



656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
# File 'livecheck/livecheck.rb', line 656

def latest_version(
  formula_or_cask,
  referenced_formula_or_cask: nil,
  livecheck_references: [],
  json: false, full_name: false, verbose: false, debug: false
)
  formula = formula_or_cask if formula_or_cask.is_a?(Formula)
  cask = formula_or_cask if formula_or_cask.is_a?(Cask::Cask)

  has_livecheckable = formula_or_cask.livecheckable?
  livecheck = formula_or_cask.livecheck
  referenced_livecheck = referenced_formula_or_cask&.livecheck

  livecheck_url = livecheck.url || referenced_livecheck&.url
  livecheck_regex = livecheck.regex || referenced_livecheck&.regex
  livecheck_strategy = livecheck.strategy || referenced_livecheck&.strategy
  livecheck_strategy_block = livecheck.strategy_block || referenced_livecheck&.strategy_block
  livecheck_throttle = livecheck.throttle || referenced_livecheck&.throttle

  livecheck_url_string = livecheck_url_to_string(
    livecheck_url,
    referenced_formula_or_cask || formula_or_cask,
  )

  urls = [livecheck_url_string] if livecheck_url_string
  urls ||= checkable_urls(referenced_formula_or_cask || formula_or_cask)

  if debug
    if formula
      puts "Formula:          #{formula_name(formula, full_name:)}"
      puts "Head only?:       true" if formula.head_only?
    elsif cask
      puts "Cask:             #{cask_name(formula_or_cask, full_name:)}"
    end
    puts "Livecheckable?:   #{has_livecheckable ? "Yes" : "No"}"
    puts "Throttle:         #{livecheck_throttle}" if livecheck_throttle

    livecheck_references.each do |ref_formula_or_cask|
      case ref_formula_or_cask
      when Formula
        puts "Formula Ref:      #{formula_name(ref_formula_or_cask, full_name:)}"
      when Cask::Cask
        puts "Cask Ref:         #{cask_name(ref_formula_or_cask, full_name:)}"
      end
    end
  end

  checked_urls = []
  urls.each_with_index do |original_url, i|
    # Only preprocess the URL when it's appropriate
    url = if STRATEGY_SYMBOLS_TO_SKIP_PREPROCESS_URL.include?(livecheck_strategy)
      original_url
    else
      preprocess_url(original_url)
    end
    next if checked_urls.include?(url)

    strategies = Strategy.from_url(
      url,
      livecheck_strategy:,
      url_provided:       livecheck_url.present?,
      regex_provided:     livecheck_regex.present?,
      block_provided:     livecheck_strategy_block.present?,
    )
    strategy = Strategy.from_symbol(livecheck_strategy) || strategies.first
    strategy_name = livecheck_strategy_names[strategy]

    if debug
      puts
      if livecheck_url.is_a?(Symbol)
        # This assumes the URL symbol will fit within the available space
        puts "URL (#{livecheck_url}):".ljust(18, " ") + original_url
      else
        puts "URL:              #{original_url}"
      end
      puts "URL (processed):  #{url}" if url != original_url
      if strategies.present? && verbose
        puts "Strategies:       #{strategies.map { |s| livecheck_strategy_names[s] }.join(", ")}"
      end
      puts "Strategy:         #{strategy.blank? ? "None" : strategy_name}"
      puts "Regex:            #{livecheck_regex.inspect}" if livecheck_regex.present?
    end

    if livecheck_strategy.present?
      if livecheck_url.blank? && strategy.method(:find_versions).parameters.include?([:keyreq, :url])
        odebug "#{strategy_name} strategy requires a URL"
        next
      elsif livecheck_strategy != :page_match && strategies.exclude?(strategy)
        odebug "#{strategy_name} strategy does not apply to this URL"
        next
      end
    end

    next if strategy.blank?

    homebrew_curl = case strategy_name
    when "PageMatch", "HeaderMatch"
      use_homebrew_curl?((referenced_formula_or_cask || formula_or_cask), url)
    end
    puts "Homebrew curl?:   Yes" if debug && homebrew_curl.present?

    strategy_args = {
      regex:         livecheck_regex,
      homebrew_curl:,
    }
    # TODO: Set `cask`/`url` args based on the presence of the keyword arg
    # in the strategy's `#find_versions` method once we figure out why
    # `strategy.method(:find_versions).parameters` isn't working as
    # expected.
    if strategy_name == "ExtractPlist"
      strategy_args[:cask] = cask if cask.present?
    else
      strategy_args[:url] = url
    end
    strategy_args.compact!

    strategy_data = strategy.find_versions(**strategy_args, &livecheck_strategy_block)
    match_version_map = strategy_data[:matches]
    regex = strategy_data[:regex]
    messages = strategy_data[:messages]
    checked_urls << url

    if messages.is_a?(Array) && match_version_map.blank?
      puts messages unless json
      next if i + 1 < urls.length

      return status_hash(formula_or_cask, "error", messages, full_name:, verbose:)
    end

    if debug
      if strategy_data[:url].present? && strategy_data[:url] != url
        puts "URL (strategy):   #{strategy_data[:url]}"
      end
      puts "URL (final):      #{strategy_data[:final_url]}" if strategy_data[:final_url].present?
      if strategy_data[:regex].present? && strategy_data[:regex] != livecheck_regex
        puts "Regex (strategy): #{strategy_data[:regex].inspect}"
      end
      puts "Cached?:          Yes" if strategy_data[:cached] == true
    end

    match_version_map.delete_if do |_match, version|
      next true if version.blank?
      next false if has_livecheckable

      UNSTABLE_VERSION_KEYWORDS.any? do |rejection|
        version.to_s.include?(rejection)
      end
    end
    next if match_version_map.blank?

    if debug
      puts
      puts "Matched Versions:"

      if verbose
        match_version_map.each do |match, version|
          puts "#{match} => #{version.inspect}"
        end
      else
        puts match_version_map.values.join(", ")
      end
    end

    version_info = {
      latest: Version.new(match_version_map.values.max_by { |v| LivecheckVersion.create(formula_or_cask, v) }),
    }

    if livecheck_throttle
      match_version_map.keep_if { |_match, version| version.patch.to_i.modulo(livecheck_throttle).zero? }
      version_info[:latest_throttled] = if match_version_map.blank?
        nil
      else
        Version.new(match_version_map.values.max_by { |v| LivecheckVersion.create(formula_or_cask, v) })
      end

      if debug
        puts
        puts "Matched Throttled Versions:"

        if verbose
          match_version_map.each do |match, version|
            puts "#{match} => #{version.inspect}"
          end
        else
          puts match_version_map.values.join(", ")
        end
      end
    end

    if json && verbose
      version_info[:meta] = {}

      if livecheck_references.present?
        version_info[:meta][:references] = livecheck_references.map do |ref_formula_or_cask|
          case ref_formula_or_cask
          when Formula
            { formula: formula_name(ref_formula_or_cask, full_name:) }
          when Cask::Cask
            { cask: cask_name(ref_formula_or_cask, full_name:) }
          end
        end
      end

      version_info[:meta][:url] = {}
      version_info[:meta][:url][:symbol] = livecheck_url if livecheck_url.is_a?(Symbol) && livecheck_url_string
      version_info[:meta][:url][:original] = original_url
      version_info[:meta][:url][:processed] = url if url != original_url
      if strategy_data[:url].present? && strategy_data[:url] != url
        version_info[:meta][:url][:strategy] = strategy_data[:url]
      end
      version_info[:meta][:url][:final] = strategy_data[:final_url] if strategy_data[:final_url]
      version_info[:meta][:url][:homebrew_curl] = homebrew_curl if homebrew_curl.present?

      version_info[:meta][:strategy] = strategy.present? ? strategy_name : nil
      version_info[:meta][:strategies] = strategies.map { |s| livecheck_strategy_names[s] } if strategies.present?
      version_info[:meta][:regex] = regex.inspect if regex.present?
      version_info[:meta][:cached] = true if strategy_data[:cached] == true
      version_info[:meta][:throttle] = livecheck_throttle if livecheck_throttle
    end

    return version_info
  end
  nil
end

.livecheck_strategy_namesHash{T::Class[T.anything] => String}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'livecheck/livecheck.rb', line 53

def livecheck_strategy_names
  return T.must(@livecheck_strategy_names) if defined?(@livecheck_strategy_names)

  # Cache demodulized strategy names, to avoid repeating this work
  @livecheck_strategy_names = T.let({}, T.nilable(T::Hash[T::Class[T.anything], String]))
  Strategy.constants.sort.each do |const_symbol|
    constant = Strategy.const_get(const_symbol)
    next unless constant.is_a?(Class)

    T.must(@livecheck_strategy_names)[constant] = Utils.demodulize(T.must(constant.name))
  end
  T.must(@livecheck_strategy_names).freeze
end

.livecheck_url_to_string(livecheck_url, package_or_resource) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



534
535
536
537
538
539
540
541
542
543
544
545
# File 'livecheck/livecheck.rb', line 534

def livecheck_url_to_string(livecheck_url, package_or_resource)
  case livecheck_url
  when String
    livecheck_url
  when :url
    package_or_resource.url&.to_s if package_or_resource.is_a?(Cask::Cask) || package_or_resource.is_a?(Resource)
  when :head, :stable
    package_or_resource.send(livecheck_url)&.url if package_or_resource.is_a?(Formula)
  when :homepage
    package_or_resource.homepage unless package_or_resource.is_a?(Resource)
  end
end

.load_other_tap_strategies(formulae_and_casks_to_check) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Uses formulae_and_casks_to_check to identify taps in use other than homebrew/core and homebrew/cask and loads strategies from them.

Parameters:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'livecheck/livecheck.rb', line 70

def load_other_tap_strategies(formulae_and_casks_to_check)
  other_taps = {}
  formulae_and_casks_to_check.each do |formula_or_cask|
    next if formula_or_cask.tap.blank?
    next if formula_or_cask.tap.core_tap?
    next if formula_or_cask.tap.core_cask_tap?
    next if other_taps[formula_or_cask.tap.name]

    other_taps[formula_or_cask.tap.name] = formula_or_cask.tap
  end
  other_taps = other_taps.sort.to_h

  other_taps.each_value do |tap|
    tap_strategy_path = "#{tap.path}/livecheck/strategy"
    Dir["#{tap_strategy_path}/*.rb"].each { require(_1) } if Dir.exist?(tap_strategy_path)
  end
end

.package_or_resource_name(package_or_resource, full_name: false) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



433
434
435
436
437
438
439
440
441
442
443
444
# File 'livecheck/livecheck.rb', line 433

def package_or_resource_name(package_or_resource, full_name: false)
  case package_or_resource
  when Formula
    formula_name(package_or_resource, full_name:)
  when Cask::Cask
    cask_name(package_or_resource, full_name:)
  when Resource
    package_or_resource.name
  else
    T.absurd(package_or_resource)
  end
end

.preprocess_url(url) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Preprocesses and returns the URL used by livecheck.

Parameters:

Returns:



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# File 'livecheck/livecheck.rb', line 574

def preprocess_url(url)
  begin
    uri = Addressable::URI.parse url
  rescue Addressable::URI::InvalidURIError
    return url
  end

  host = uri.host
  path = uri.path
  return url if host.nil? || path.nil?

  host = "github.com" if host == "github.s3.amazonaws.com"
  path = path.delete_prefix("/").delete_suffix(".git")
  scheme = uri.scheme

  if host == "github.com"
    return url if path.match? %r{/releases/latest/?$}

    owner, repo = path.delete_prefix("downloads/").split("/")
    url = "#{scheme}://#{host}/#{owner}/#{repo}.git"
  elsif GITEA_INSTANCES.include?(host)
    return url if path.match? %r{/releases/latest/?$}

    owner, repo = path.split("/")
    url = "#{scheme}://#{host}/#{owner}/#{repo}.git"
  elsif GOGS_INSTANCES.include?(host)
    owner, repo = path.split("/")
    url = "#{scheme}://#{host}/#{owner}/#{repo}.git"
  # sourcehut
  elsif host == "git.sr.ht"
    owner, repo = path.split("/")
    url = "#{scheme}://#{host}/#{owner}/#{repo}"
  # GitLab (gitlab.com or self-hosted)
  elsif path.include?("/-/archive/")
    url = url.sub(%r{/-/archive/.*$}i, ".git")
  end

  url
end

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Formats and prints the livecheck result for a formula/cask/resource.

Parameters:

  • info (Hash{Symbol => T.untyped})
  • verbose (Boolean) (defaults to: false)
  • ambiguous_cask (Boolean) (defaults to: false)


495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# File 'livecheck/livecheck.rb', line 495

def print_latest_version(info, verbose: false, ambiguous_cask: false)
  package_or_resource_s = info[:resource].present? ? "  " : ""
  package_or_resource_s += "#{Tty.blue}#{info[:formula] || info[:cask] || info[:resource]}#{Tty.reset}"
  package_or_resource_s += " (cask)" if ambiguous_cask
  package_or_resource_s += " (guessed)" if verbose && !info[:meta][:livecheckable]

  current_s = if info[:version][:newer_than_upstream]
    "#{Tty.red}#{info[:version][:current]}#{Tty.reset}"
  else
    info[:version][:current]
  end

  latest_s = if info[:version][:outdated]
    "#{Tty.green}#{info[:version][:latest]}#{Tty.reset}"
  else
    info[:version][:latest]
  end

  puts "#{package_or_resource_s}: #{current_s} ==> #{latest_s}"
end

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Prints the livecheck result for the resources of a given Formula.

Parameters:

  • info (Array<Hash{Symbol => T.untyped}>)
  • verbose (Boolean) (defaults to: false)


518
519
520
521
522
523
524
525
526
# File 'livecheck/livecheck.rb', line 518

def print_resources_info(info, verbose: false)
  info.each do |r_info|
    if r_info[:status] && r_info[:messages]
      SkipConditions.print_skip_information(r_info)
    else
      print_latest_version(r_info, verbose:)
    end
  end
end

.resolve_livecheck_reference(formula_or_cask, first_formula_or_cask = formula_or_cask, references = [], full_name: false, debug: false) ⇒ Array<T.untyped>?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Resolve formula/cask references in livecheck blocks to a final formula or cask.

Parameters:

Returns:

  • (Array<T.untyped>, nil)


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
# File 'livecheck/livecheck.rb', line 99

def resolve_livecheck_reference(
  formula_or_cask,
  first_formula_or_cask = formula_or_cask,
  references = [],
  full_name: false,
  debug: false
)
  # Check the livecheck block for a formula or cask reference
  livecheck = formula_or_cask.livecheck
  livecheck_formula = livecheck.formula
  livecheck_cask = livecheck.cask
  return [nil, references] if livecheck_formula.blank? && livecheck_cask.blank?

  # Load the referenced formula or cask
  referenced_formula_or_cask = Homebrew.with_no_api_env do
    if livecheck_formula
      Formulary.factory(livecheck_formula)
    elsif livecheck_cask
      Cask::CaskLoader.load(livecheck_cask)
    end
  end

  # Error if a `livecheck` block references a formula/cask that was already
  # referenced (or itself)
  if referenced_formula_or_cask == first_formula_or_cask ||
     referenced_formula_or_cask == formula_or_cask ||
     references.include?(referenced_formula_or_cask)
    if debug
      # Print the chain of references for debugging
      puts "Reference Chain:"
      puts package_or_resource_name(first_formula_or_cask, full_name:)

      references << referenced_formula_or_cask
      references.each do |ref_formula_or_cask|
        puts package_or_resource_name(ref_formula_or_cask, full_name:)
      end
    end

    raise "Circular formula/cask reference encountered"
  end
  references << referenced_formula_or_cask

  # Check the referenced formula/cask for a reference
  next_referenced_formula_or_cask, next_references = resolve_livecheck_reference(
    referenced_formula_or_cask,
    first_formula_or_cask,
    references,
    full_name:,
    debug:,
  )

  # Returning references along with the final referenced formula/cask
  # allows us to print the chain of references in the debug output
  [
    next_referenced_formula_or_cask || referenced_formula_or_cask,
    next_references,
  ]
end

.resource_version(resource, formula_latest, json: false, debug: false, quiet: false, verbose: false) ⇒ Hash{Symbol => T.untyped}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Identifies the latest version of a resource and returns a Hash containing the version information. Returns nil if a latest version couldn’t be found.

Parameters:

  • resource (Resource)
  • formula_latest (String)
  • json (Boolean) (defaults to: false)
  • debug (Boolean) (defaults to: false)
  • quiet (Boolean) (defaults to: false)
  • verbose (Boolean) (defaults to: false)

Returns:



893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
# File 'livecheck/livecheck.rb', line 893

def resource_version(
  resource,
  formula_latest,
  json: false,
  debug: false,
  quiet: false,
  verbose: false
)
  has_livecheckable = resource.livecheckable?

  if debug
    puts "\n\n"
    puts "Resource:         #{resource.name}"
    puts "Livecheckable?:   #{has_livecheckable ? "Yes" : "No"}"
  end

  resource_version_info = {}

  livecheck = resource.livecheck
  livecheck_url = livecheck.url
  livecheck_regex = livecheck.regex
  livecheck_strategy = livecheck.strategy
  livecheck_strategy_block = livecheck.strategy_block

  livecheck_url_string = livecheck_url_to_string(livecheck_url, resource)

  urls = [livecheck_url_string] if livecheck_url_string
  urls ||= checkable_urls(resource)

  checked_urls = []
  urls.each_with_index do |original_url, i|
    url = original_url.gsub(Constants::LATEST_VERSION, formula_latest)

    # Only preprocess the URL when it's appropriate
    url = preprocess_url(url) unless STRATEGY_SYMBOLS_TO_SKIP_PREPROCESS_URL.include?(livecheck_strategy)

    next if checked_urls.include?(url)

    strategies = Strategy.from_url(
      url,
      livecheck_strategy:,
      url_provided:       livecheck_url.present?,
      regex_provided:     livecheck_regex.present?,
      block_provided:     livecheck_strategy_block.present?,
    )
    strategy = Strategy.from_symbol(livecheck_strategy) || strategies.first
    strategy_name = livecheck_strategy_names[strategy]

    if debug
      puts
      if livecheck_url.is_a?(Symbol)
        # This assumes the URL symbol will fit within the available space
        puts "URL (#{livecheck_url}):".ljust(18, " ") + original_url
      else
        puts "URL:              #{original_url}"
      end
      puts "URL (processed):  #{url}" if url != original_url
      if strategies.present? && verbose
        puts "Strategies:       #{strategies.map { |s| livecheck_strategy_names[s] }.join(", ")}"
      end
      puts "Strategy:         #{strategy.blank? ? "None" : strategy_name}"
      puts "Regex:            #{livecheck_regex.inspect}" if livecheck_regex.present?
    end

    if livecheck_strategy.present?
      if livecheck_url.blank? && strategy.method(:find_versions).parameters.include?([:keyreq, :url])
        odebug "#{strategy_name} strategy requires a URL"
        next
      elsif livecheck_strategy != :page_match && strategies.exclude?(strategy)
        odebug "#{strategy_name} strategy does not apply to this URL"
        next
      end
    end
    puts if debug && strategy.blank?
    next if strategy.blank?

    strategy_args = {
      url:,
      regex:         livecheck_regex,
      homebrew_curl: false,
    }.compact

    strategy_data = strategy.find_versions(**strategy_args, &livecheck_strategy_block)
    match_version_map = strategy_data[:matches]
    regex = strategy_data[:regex]
    messages = strategy_data[:messages]
    checked_urls << url

    if messages.is_a?(Array) && match_version_map.blank?
      puts messages unless json
      next if i + 1 < urls.length

      return status_hash(resource, "error", messages, verbose:)
    end

    if debug
      if strategy_data[:url].present? && strategy_data[:url] != url
        puts "URL (strategy):   #{strategy_data[:url]}"
      end
      puts "URL (final):      #{strategy_data[:final_url]}" if strategy_data[:final_url].present?
      if strategy_data[:regex].present? && strategy_data[:regex] != livecheck_regex
        puts "Regex (strategy): #{strategy_data[:regex].inspect}"
      end
      puts "Cached?:          Yes" if strategy_data[:cached] == true
    end

    match_version_map.delete_if do |_match, version|
      next true if version.blank?
      next false if has_livecheckable

      UNSTABLE_VERSION_KEYWORDS.any? do |rejection|
        version.to_s.include?(rejection)
      end
    end
    next if match_version_map.blank?

    if debug
      puts
      puts "Matched Versions:"

      if verbose
        match_version_map.each do |match, version|
          puts "#{match} => #{version.inspect}"
        end
      else
        puts match_version_map.values.join(", ")
      end
    end

    res_current = T.must(resource.version)
    res_latest = Version.new(match_version_map.values.max_by { |v| LivecheckVersion.create(resource, v) })

    return status_hash(resource, "error", ["Unable to get versions"], verbose:) if res_latest.blank?

    is_outdated = res_current < res_latest
    is_newer_than_upstream = res_current > res_latest

    resource_version_info = {
      resource: resource.name,
      version:  {
        current:             res_current.to_s,
        latest:              res_latest.to_s,
        outdated:            is_outdated,
        newer_than_upstream: is_newer_than_upstream,
      },
    }

    resource_version_info[:meta] = { livecheckable: has_livecheckable, url: {} }
    if livecheck_url.is_a?(Symbol) && livecheck_url_string
      resource_version_info[:meta][:url][:symbol] = livecheck_url
    end
    resource_version_info[:meta][:url][:original] = original_url
    resource_version_info[:meta][:url][:processed] = url if url != original_url
    if strategy_data[:url].present? && strategy_data[:url] != url
      resource_version_info[:meta][:url][:strategy] = strategy_data[:url]
    end
    resource_version_info[:meta][:url][:final] = strategy_data[:final_url] if strategy_data[:final_url]
    resource_version_info[:meta][:strategy] = strategy.present? ? strategy_name : nil
    if strategies.present?
      resource_version_info[:meta][:strategies] = strategies.map { |s| livecheck_strategy_names[s] }
    end
    resource_version_info[:meta][:regex] = regex.inspect if regex.present?
    resource_version_info[:meta][:cached] = true if strategy_data[:cached] == true
  rescue => e
    Homebrew.failed = true
    if json
      status_hash(resource, "error", [e.to_s], verbose:)
    elsif !quiet
      onoe "#{Tty.blue}#{resource.name}#{Tty.reset}: #{e}"
      $stderr.puts Utils::Backtrace.clean(e) if debug && !e.is_a?(Livecheck::Error)
      nil
    end
  end
  resource_version_info
end

.run_checks(formulae_and_casks_to_check, full_name: false, handle_name_conflict: false, check_resources: false, json: false, newer_only: false, extract_plist: false, debug: false, quiet: false, verbose: false) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Executes the livecheck logic for each formula/cask in the formulae_and_casks_to_check array and prints the results.

Parameters:

  • formulae_and_casks_to_check (Array<Formula, Cask::Cask>)
  • full_name (Boolean) (defaults to: false)
  • handle_name_conflict (Boolean) (defaults to: false)
  • check_resources (Boolean) (defaults to: false)
  • json (Boolean) (defaults to: false)
  • newer_only (Boolean) (defaults to: false)
  • extract_plist (Boolean) (defaults to: false)
  • debug (Boolean) (defaults to: false)
  • quiet (Boolean) (defaults to: false)
  • verbose (Boolean) (defaults to: false)


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
218
219
220
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'livecheck/livecheck.rb', line 174

def run_checks(
  formulae_and_casks_to_check,
  full_name: false, handle_name_conflict: false, check_resources: false, json: false, newer_only: false,
  extract_plist: false, debug: false, quiet: false, verbose: false
)
  load_other_tap_strategies(formulae_and_casks_to_check)

  ambiguous_casks = []
  if handle_name_conflict
    ambiguous_casks = formulae_and_casks_to_check
                      .group_by { |item| package_or_resource_name(item, full_name: true) }
                      .values
                      .select { |items| items.length > 1 }
                      .flatten
                      .select { |item| item.is_a?(Cask::Cask) }
  end

  ambiguous_names = []
  unless full_name
    ambiguous_names =
      (formulae_and_casks_to_check - ambiguous_casks).group_by { |item| package_or_resource_name(item) }
                                                     .values
                                                     .select { |items| items.length > 1 }
                                                     .flatten
  end

  has_a_newer_upstream_version = T.let(false, T::Boolean)

  formulae_and_casks_total = formulae_and_casks_to_check.count
  if json && !quiet && $stderr.tty?
    Tty.with($stderr) do |stderr|
      stderr.puts Formatter.headline("Running checks", color: :blue)
    end

    require "ruby-progressbar"
    progress = ProgressBar.create(
      total:          formulae_and_casks_total,
      progress_mark:  "#",
      remainder_mark: ".",
      format:         " %t: [%B] %c/%C ",
      output:         $stderr,
    )
  end

  # Allow ExtractPlist strategy if only one formula/cask is being checked.
  extract_plist = true if formulae_and_casks_total == 1

  formulae_checked = formulae_and_casks_to_check.map.with_index do |formula_or_cask, i|
    formula = formula_or_cask if formula_or_cask.is_a?(Formula)
    cask = formula_or_cask if formula_or_cask.is_a?(Cask::Cask)

    use_full_name = full_name || ambiguous_names.include?(formula_or_cask)
    name = package_or_resource_name(formula_or_cask, full_name: use_full_name)

    referenced_formula_or_cask, livecheck_references =
      resolve_livecheck_reference(formula_or_cask, full_name: use_full_name, debug:)

    if debug && i.positive?
      puts <<~EOS

        ----------

      EOS
    elsif debug
      puts
    end

    # Check skip conditions for a referenced formula/cask
    if referenced_formula_or_cask
      skip_info = SkipConditions.referenced_skip_information(
        referenced_formula_or_cask,
        name,
        full_name:     use_full_name,
        verbose:,
        extract_plist:,
      )
    end

    skip_info ||= SkipConditions.skip_information(
      formula_or_cask,
      full_name:     use_full_name,
      verbose:,
      extract_plist:,
    )
    if skip_info.present?
      next skip_info if json && !newer_only

      SkipConditions.print_skip_information(skip_info) if !newer_only && !quiet
      next
    end

    formula&.head&.downloader&.quiet!

    # Use the `stable` version for comparison except for installed
    # head-only formulae. A formula with `stable` and `head` that's
    # installed using `--head` will still use the `stable` version for
    # comparison.
    current = if formula
      if formula.head_only?
        formula.any_installed_version.version.commit
      else
        T.must(formula.stable).version
      end
    else
      Version.new(formula_or_cask.version)
    end

    current_str = current.to_s
    current = LivecheckVersion.create(formula_or_cask, current)

    latest = if formula&.head_only?
      T.must(formula.head).downloader.fetch_last_commit
    else
      version_info = latest_version(
        formula_or_cask,
        referenced_formula_or_cask:,
        livecheck_references:,
        json:, full_name: use_full_name, verbose:, debug:
      )
      version_info[:latest] if version_info.present?
    end

    check_for_resources = check_resources && formula_or_cask.is_a?(Formula) && formula_or_cask.resources.present?
    if check_for_resources
      resource_version_info = formula_or_cask.resources.map do |resource|
        res_skip_info ||= SkipConditions.skip_information(resource, verbose:)
        if res_skip_info.present?
          res_skip_info
        else
          res_version_info = resource_version(
            resource,
            latest.to_s,
            json:,
            debug:,
            quiet:,
            verbose:,
          )
          if res_version_info.empty?
            status_hash(resource, "error", ["Unable to get versions"], verbose:)
          else
            res_version_info
          end
        end
      end.compact_blank
      Homebrew.failed = true if resource_version_info.any? { |info| info[:status] == "error" }
    end

    if latest.blank?
      no_versions_msg = "Unable to get versions"
      raise Livecheck::Error, no_versions_msg unless json
      next if quiet

      next version_info if version_info.is_a?(Hash) && version_info[:status] && version_info[:messages]

      latest_info = status_hash(formula_or_cask, "error", [no_versions_msg], full_name: use_full_name,
                                                                             verbose:)
      if check_for_resources
        unless verbose
          resource_version_info.map! do |info|
            info.delete(:meta)
            info
          end
        end
        latest_info[:resources] = resource_version_info
      end

      next latest_info
    end

    if (m = latest.to_s.match(/(.*)-release$/)) && !current.to_s.match(/.*-release$/)
      latest = Version.new(m[1])
    end

    latest_str = latest.to_s
    latest = LivecheckVersion.create(formula_or_cask, latest)

    is_outdated = if formula&.head_only?
      # A HEAD-only formula is considered outdated if the latest upstream
      # commit hash is different than the installed version's commit hash
      (current != latest)
    else
      (current < latest)
    end

    is_newer_than_upstream = (formula&.stable? || cask) && (current > latest)

    info = {}
    info[:formula] = name if formula
    info[:cask] = name if cask
    info[:version] = {
      current:             current_str,
      latest:              latest_str,
      latest_throttled:    version_info&.dig(:latest_throttled),
      outdated:            is_outdated,
      newer_than_upstream: is_newer_than_upstream,
    }.compact
    info[:meta] = {
      livecheckable: formula_or_cask.livecheckable?,
    }
    info[:meta][:head_only] = true if formula&.head_only?
    info[:meta].merge!(version_info[:meta]) if version_info.present? && version_info.key?(:meta)

    info[:resources] = resource_version_info if check_for_resources

    next if newer_only && !info[:version][:outdated]

    has_a_newer_upstream_version ||= true

    if json
      progress&.increment
      info.delete(:meta) unless verbose
      if check_for_resources && !verbose
        resource_version_info.map! do |info|
          info.delete(:meta)
          info
        end
      end
      next info
    end
    puts if debug
    print_latest_version(info, verbose:, ambiguous_cask: ambiguous_casks.include?(formula_or_cask))
    print_resources_info(resource_version_info, verbose:) if check_for_resources
    nil
  rescue => e
    Homebrew.failed = true
    use_full_name = full_name || ambiguous_names.include?(formula_or_cask)

    if json
      progress&.increment
      unless quiet
        status_hash(formula_or_cask, "error", [e.to_s], full_name: use_full_name,
                                                        verbose:)
      end
    elsif !quiet
      name = package_or_resource_name(formula_or_cask, full_name: use_full_name)
      name += " (cask)" if ambiguous_casks.include?(formula_or_cask)

      onoe "#{Tty.blue}#{name}#{Tty.reset}: #{e}"
      $stderr.puts Utils::Backtrace.clean(e) if debug && !e.is_a?(Livecheck::Error)
      print_resources_info(resource_version_info, verbose:) if check_for_resources
      nil
    end
  end

  puts "No newer upstream versions." if newer_only && !has_a_newer_upstream_version && !debug && !json && !quiet

  return unless json

  if progress
    progress.finish
    Tty.with($stderr) do |stderr|
      stderr.print "#{Tty.up}#{Tty.erase_line}" * 2
    end
  end

  puts JSON.pretty_generate(formulae_checked.compact)
end

.status_hash(package_or_resource, status_str, messages = nil, full_name: false, verbose: false) ⇒ Hash{Symbol => T.untyped}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'livecheck/livecheck.rb', line 469

def status_hash(package_or_resource, status_str, messages = nil, full_name: false, verbose: false)
  formula = package_or_resource if package_or_resource.is_a?(Formula)
  cask = package_or_resource if package_or_resource.is_a?(Cask::Cask)
  resource = package_or_resource if package_or_resource.is_a?(Resource)

  status_hash = {}
  if formula
    status_hash[:formula] = formula_name(formula, full_name:)
  elsif cask
    status_hash[:cask] = cask_name(cask, full_name:)
  elsif resource
    status_hash[:resource] = resource.name
  end
  status_hash[:status] = status_str
  status_hash[:messages] = messages if messages.is_a?(Array)

  status_hash[:meta] = {
    livecheckable: package_or_resource.livecheckable?,
  }
  status_hash[:meta][:head_only] = true if formula&.head_only?

  status_hash
end

.use_homebrew_curl?(formula_or_cask, url) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

livecheck should fetch a URL using brewed curl if the formula/cask contains a stable/url or head URL using: :homebrew_curl that shares the same root domain.

Parameters:

Returns:

  • (Boolean)


618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File 'livecheck/livecheck.rb', line 618

def use_homebrew_curl?(formula_or_cask, url)
  url_root_domain = Addressable::URI.parse(url)&.domain
  return false if url_root_domain.blank?

  # Collect root domains of URLs with `using: :homebrew_curl`
  homebrew_curl_root_domains = []
  case formula_or_cask
  when Formula
    [:stable, :head].each do |spec_name|
      next unless (spec = formula_or_cask.send(spec_name))
      next if spec.using != :homebrew_curl

      domain = Addressable::URI.parse(spec.url)&.domain
      homebrew_curl_root_domains << domain if domain.present?
    end
  when Cask::Cask
    return false if formula_or_cask.url.using != :homebrew_curl

    domain = Addressable::URI.parse(formula_or_cask.url.to_s)&.domain
    homebrew_curl_root_domains << domain if domain.present?
  end

  homebrew_curl_root_domains.include?(url_root_domain)
end