Module: Homebrew::Upgrade Private

Defined in:
brew/Library/Homebrew/upgrade.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Helper functions for upgrading formulae.

Class Method Summary collapse

Class Method Details

.check_broken_dependents(installed_formulae) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'brew/Library/Homebrew/upgrade.rb', line 123

def check_broken_dependents(installed_formulae)
  CacheStoreDatabase.use(:linkage) do |db|
    installed_formulae.flat_map(&:runtime_installed_formula_dependents)
                      .uniq
                      .select do |f|
      keg = f.any_installed_keg
      next unless keg

      LinkageChecker.new(keg, cache_db: db)
                    .broken_library_linkage?
    end.compact
  end
end

.check_installed_dependents(args:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
# File 'brew/Library/Homebrew/upgrade.rb', line 137

def check_installed_dependents(args:)
  installed_formulae = FormulaInstaller.installed.to_a
  return if installed_formulae.empty?

  already_broken_dependents = check_broken_dependents(installed_formulae)

  outdated_dependents =
    installed_formulae.flat_map(&:runtime_installed_formula_dependents)
                      .uniq
                      .select(&:outdated?)
  return if outdated_dependents.blank? && already_broken_dependents.blank?

  outdated_dependents -= installed_formulae if args.dry_run?

  upgradeable_dependents =
    outdated_dependents.reject(&:pinned?)
                       .sort { |a, b| depends_on(a, b) }
  pinned_dependents =
    outdated_dependents.select(&:pinned?)
                       .sort { |a, b| depends_on(a, b) }

  if pinned_dependents.present?
    plural = "dependent".pluralize(pinned_dependents.count)
    ohai "Not upgrading #{pinned_dependents.count} pinned #{plural}:"
    puts(pinned_dependents.map do |f|
      "#{f.full_specified_name} #{f.pkg_version}"
    end.join(", "))
  end

  # Print the upgradable dependents.
  if upgradeable_dependents.blank?
    ohai "No outdated dependents to upgrade!" unless args.dry_run?
  else
    plural = "dependent".pluralize(upgradeable_dependents.count)
    verb = args.dry_run? ? "Would upgrade" : "Upgrading"
    ohai "#{verb} #{upgradeable_dependents.count} #{plural}:"
    formulae_upgrades = upgradeable_dependents.map do |f|
      name = f.full_specified_name
      if f.optlinked?
        "#{name} #{Keg.new(f.opt_prefix).version} -> #{f.pkg_version}"
      else
        "#{name} #{f.pkg_version}"
      end
    end
    puts formulae_upgrades.join(", ")
  end

  upgrade_formulae(upgradeable_dependents, args: args)

  # Assess the dependents tree again now we've upgraded.
  oh1 "Checking for dependents of upgraded formulae..." unless args.dry_run?
  broken_dependents = check_broken_dependents(installed_formulae)
  if broken_dependents.blank?
    if args.dry_run?
      ohai "No currently broken dependents found!"
      opoo "If they are broken by the upgrade they will also be upgraded or reinstalled."
    else
      ohai "No broken dependents found!"
    end
    return
  end

  reinstallable_broken_dependents =
    broken_dependents.reject(&:outdated?)
                     .reject(&:pinned?)
                     .sort { |a, b| depends_on(a, b) }
  outdated_pinned_broken_dependents =
    broken_dependents.select(&:outdated?)
                     .select(&:pinned?)
                     .sort { |a, b| depends_on(a, b) }

  # Print the pinned dependents.
  if outdated_pinned_broken_dependents.present?
    count = outdated_pinned_broken_dependents.count
    plural = "dependent".pluralize(outdated_pinned_broken_dependents.count)
    onoe "Not reinstalling #{count} broken and outdated, but pinned #{plural}:"
    $stderr.puts(outdated_pinned_broken_dependents.map do |f|
      "#{f.full_specified_name} #{f.pkg_version}"
    end.join(", "))
  end

  # Print the broken dependents.
  if reinstallable_broken_dependents.blank?
    ohai "No broken dependents to reinstall!"
  else
    count = reinstallable_broken_dependents.count
    plural = "dependent".pluralize(reinstallable_broken_dependents.count)
    ohai "Reinstalling #{count} broken #{plural} from source:"
    puts reinstallable_broken_dependents.map(&:full_specified_name)
                                        .join(", ")
  end

  return if args.dry_run?

  reinstallable_broken_dependents.each do |f|
    Homebrew.reinstall_formula(f, build_from_source: true, args: args)
  rescue FormulaInstallationAlreadyAttemptedError
    # We already attempted to reinstall f as part of the dependency tree of
    # another formula. In that case, don't generate an error, just move on.
    nil
  rescue CannotInstallFormulaError => e
    ofail e
  rescue BuildError => e
    e.dump(verbose: args.verbose?)
    puts
    Homebrew.failed = true
  rescue DownloadError => e
    ofail e
  end
end

.upgrade_formulae(formulae_to_install, args:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'brew/Library/Homebrew/upgrade.rb', line 16

def upgrade_formulae(formulae_to_install, args:)
  return if formulae_to_install.empty?
  return if args.dry_run?

  # Sort keg-only before non-keg-only formulae to avoid any needless conflicts
  # with outdated, non-keg-only versions of formulae being upgraded.
  formulae_to_install.sort! do |a, b|
    if !a.keg_only? && b.keg_only?
      1
    elsif a.keg_only? && !b.keg_only?
      -1
    else
      0
    end
  end

  formulae_to_install.each do |f|
    Migrator.migrate_if_needed(f, force: args.force?)
    begin
      upgrade_formula(f, args: args)
      Cleanup.install_formula_clean!(f)
    rescue UnsatisfiedRequirements => e
      Homebrew.failed = true
      onoe "#{f}: #{e}"
    end
  end
end