Module: InstalledDependents Private

Defined in:
installed_dependents.rb

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.

Helper functions for installed dependents.

Class Method Summary collapse

Class Method Details

.find_some_installed_dependents(kegs, casks: [], formulae: Formula.installed) ⇒ Array<(Array<Keg>, 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.

Given an array of kegs, this method will try to find some other kegs or casks that depend on them. If it does, it returns:

  • some kegs in the passed array that have installed dependents
  • some installed dependents of those kegs.

If it doesn't, it returns nil.

Note that nil will be returned if the only installed dependents of the passed kegs are other kegs in the array or casks present in the casks parameter, or formulae excluded from the formulae parameter.

For efficiency, we don't bother trying to get complete data.

Parameters:

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'installed_dependents.rb', line 30

def find_some_installed_dependents(kegs, casks: [], formulae: Formula.installed)
  keg_names = kegs.select(&:optlinked?).map(&:name)
  keg_formulae = []
  kegs_by_source = kegs.group_by do |keg|
    # First, attempt to resolve the keg to a formula
    # to get up-to-date name and tap information.
    f = keg.to_formula
    keg_formulae << f
    [f.name, f.tap]
  rescue
    # If the formula for the keg can't be found,
    # fall back to the information in the tab.
    [keg.name, keg.tab.tap]
  end

  all_required_kegs = Set.new
  all_dependents = []

  # Don't include dependencies of kegs that were in the given array.
  dependents_to_check = (formulae - keg_formulae) + (Cask::Caskroom.casks - casks)

  dependents_to_check.each do |dependent|
    required_kegs = case dependent
    when Formula
      dependent.missing_dependencies(hide: keg_names).flat_map do |d|
        formula = begin
          d.to_installed_formula
        rescue FormulaUnavailableError
          next []
        rescue Homebrew::UntrustedTapError, TapFormulaAmbiguityError => e
          loaders = if e.is_a?(TapFormulaAmbiguityError)
            e.loaders
          else
            begin
              [Formulary.loader_for(d.name, warn: false)]
            rescue TapFormulaAmbiguityError => ambiguity
              ambiguity.loaders
            end
          end
          matching_kegs = loaders.filter_map do |loader|
            kegs_by_source[[loader.name, loader.tap]]&.max_by(&:scheme_and_version)
          end
          if matching_kegs.empty?
            fallback_names = [Utils.name_from_full_name(d.name)]
            fallback_names.concat(loaders.map { |loader| Utils.name_from_full_name(loader.name) })
            known_taps = loaders.filter_map(&:tap)
            dependency_tap = d.tap
            known_taps << dependency_tap if dependency_tap
            matching_kegs = kegs_by_source.filter_map do |(name, tap), source_kegs|
              next unless fallback_names.include?(name)
              next if tap && known_taps.any? && known_taps.exclude?(tap)

              source_kegs.max_by(&:scheme_and_version)
            end
          end
          next matching_kegs
        end
        f_kegs = kegs_by_source[[formula.name, formula.tap]]
        [f_kegs&.max_by(&:scheme_and_version)].compact
      end
    when Cask::Cask
      # When checking for cask dependents, we don't care about missing or non-runtime dependencies
      CaskDependent.new(dependent).runtime_dependencies.map(&:to_installed_formula).filter_map do |f|
        kegs_by_source[[f.name, f.tap]]&.max_by(&:scheme_and_version)
      end
    end

    next if required_kegs.empty?

    all_required_kegs += required_kegs
    all_dependents << dependent.to_s
  end

  return if all_required_kegs.empty?
  return if all_dependents.empty?

  [all_required_kegs.to_a, all_dependents.sort]
end