Class: Homebrew::Bundle::Brew Private

Inherits:
PackageType show all
Extended by:
Utils::Output::Mixin
Defined in:
bundle/brew.rb,
bundle/brew_services.rb

Overview

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

TODO:

refactor into multiple modules

Direct Known Subclasses

Services

Defined Under Namespace

Classes: Services, Topo

Constant Summary collapse

PACKAGE_TYPE =

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.

:brew
PACKAGE_TYPE_NAME =

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.

"Formula"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Output::Mixin

odebug, odeprecated, odie, odisabled, ofail, oh1, oh1_title, ohai, ohai_title, onoe, opoo, opoo_outside_github_actions, pretty_deprecated, pretty_disabled, pretty_duration, pretty_installed, pretty_outdated, pretty_uninstalled

Methods inherited from PackageType

check, check_supported?, dump_supported?, install_supported?, type

Methods inherited from Checker::Base

#checkable_entries, #exit_early_check, #failure_reason, #find_actionable, #format_checkable, #full_check

Constructor Details

#initialize(name = "", options = {}) ⇒ Brew

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 a new instance of Brew.



333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'bundle/brew.rb', line 333

def initialize(name = "", options = {})
  super()
  @full_name = name
  @name = name.split("/").last
  @args = options.fetch(:args, []).map { |arg| "--#{arg}" }
  @conflicts_with_arg = options.fetch(:conflicts_with, [])
  @restart_service = options[:restart_service]
  @start_service = options.fetch(:start_service, @restart_service)
  @link = options.fetch(:link, nil)
  @postinstall = options.fetch(:postinstall, nil)
  @version_file = options.fetch(:version_file, nil)
  @changed = nil
end

Class Method Details

.dump(describe: false, no_restart: false) ⇒ Object

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.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'bundle/brew.rb', line 141

def dump(describe: false, no_restart: false)
  require "bundle/brew_services"

  requested_formula = formulae.select do |f|
    f[:installed_on_request?] || !f[:installed_as_dependency?]
  end
  requested_formula.map do |f|
    brewline = if describe && f[:desc].present?
      f[:desc].split("\n").map { |s| "# #{s}\n" }.join
    else
      ""
    end
    brewline += "brew \"#{f[:full_name]}\""

    args = f[:args].map { |arg| "\"#{arg}\"" }.sort.join(", ")
    brewline += ", args: [#{args}]" unless f[:args].empty?
    brewline += ", restart_service: :changed" if !no_restart && BrewServices.started?(f[:full_name])
    brewline += ", link: #{f[:link?]}" unless f[:link?].nil?
    brewline
  end.join("\n")
end

.dump_output(describe: false, no_restart: false) ⇒ Object

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.



163
164
165
# File 'bundle/brew.rb', line 163

def dump_output(describe: false, no_restart: false)
  dump(describe:, no_restart:)
end

.fetchable_name(name, options = {}, no_upgrade: false) ⇒ Object

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.



167
168
169
170
171
172
173
174
175
176
# File 'bundle/brew.rb', line 167

def fetchable_name(name, options = {}, no_upgrade: false)
  _ = options

  user, repository, = name.split("/", 3)
  return if user.present? && repository.present? &&
            Homebrew::Bundle::Tap.installed_taps.exclude?("#{user}/#{repository}")
  return if formula_installed_and_up_to_date?(name, no_upgrade:)

  name
end

.formula_aliasesObject

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.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'bundle/brew.rb', line 178

def formula_aliases
  return @formula_aliases if @formula_aliases

  @formula_aliases = {}
  formulae.each do |f|
    aliases = f[:aliases]
    next if aliases.blank?

    aliases.each do |a|
      @formula_aliases[a] = f[:full_name]
      if f[:full_name].include? "/" # tap formula
        tap_name = f[:full_name].rpartition("/").first
        @formula_aliases["#{tap_name}/#{a}"] = f[:full_name]
      end
    end
  end
  @formula_aliases
end

.formula_in_array?(formula, array) ⇒ 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.

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'bundle/brew.rb', line 66

def formula_in_array?(formula, array)
  return true if array.include?(formula)
  return true if array.include?(formula.split("/").last)

  old_name = formula_oldnames[formula]
  old_name ||= formula_oldnames[formula.split("/").last]
  return true if old_name && array.include?(old_name)

  resolved_full_name = formula_aliases[formula]
  return false unless resolved_full_name
  return true if array.include?(resolved_full_name)
  return true if array.include?(resolved_full_name.split("/").last)

  false
end

.formula_installed?(formula) ⇒ 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.

Returns:

  • (Boolean)


82
83
84
# File 'bundle/brew.rb', line 82

def formula_installed?(formula)
  formula_in_array?(formula, installed_formulae)
end

.formula_installed_and_up_to_date?(formula, no_upgrade: false) ⇒ 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.

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'bundle/brew.rb', line 55

def formula_installed_and_up_to_date?(formula, no_upgrade: false)
  return false unless formula_installed?(formula)
  return true if no_upgrade_with_args?(no_upgrade, formula)

  !formula_upgradable?(formula)
end

.formula_oldnamesObject

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.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'bundle/brew.rb', line 197

def formula_oldnames
  return @formula_oldnames if @formula_oldnames

  @formula_oldnames = {}
  formulae.each do |f|
    oldnames = f[:oldnames]
    next if oldnames.blank?

    oldnames.each do |oldname|
      @formula_oldnames[oldname] = f[:full_name]
      if f[:full_name].include? "/" # tap formula
        tap_name = f[:full_name].rpartition("/").first
        @formula_oldnames["#{tap_name}/#{oldname}"] = f[:full_name]
      end
    end
  end
  @formula_oldnames
end

.formula_upgradable?(formula) ⇒ 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.

Returns:

  • (Boolean)


86
87
88
89
# File 'bundle/brew.rb', line 86

def formula_upgradable?(formula)
  # Check local cache first and then authoritative Homebrew source.
  formula_in_array?(formula, upgradable_formulae) && Formula[formula].outdated?
end

.formulaeObject

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.



107
108
109
110
111
112
# File 'bundle/brew.rb', line 107

def formulae
  return @formulae if @formulae

  formulae_by_full_name
  @formulae
end

.formulae_by_full_name(name = nil) ⇒ Object

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.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'bundle/brew.rb', line 114

def formulae_by_full_name(name = nil)
  return @formulae_by_full_name[name] if name.present? && @formulae_by_full_name&.key?(name)

  require "formula"
  require "formulary"
  Formulary.enable_factory_cache!

  @formulae_by_name ||= {}
  @formulae_by_full_name ||= {}

  if name.nil?
    formulae = Formula.installed.map { add_formula(it) }
    sort!(formulae)
    return @formulae_by_full_name
  end

  formula = Formula[name]
  add_formula(formula)
rescue FormulaUnavailableError => e
  opoo "'#{name}' formula is unreadable: #{e}"
  {}
end

.formulae_by_name(name) ⇒ Object

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.



137
138
139
# File 'bundle/brew.rb', line 137

def formulae_by_name(name)
  formulae_by_full_name(name) || @formulae_by_name[name]
end

.install!(name, preinstall: true, no_upgrade: false, verbose: false, force: false, **options) ⇒ Object

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.



43
44
45
# File 'bundle/brew.rb', line 43

def install!(name, preinstall: true, no_upgrade: false, verbose: false, force: false, **options)
  new(name, options).install!(preinstall:, no_upgrade:, verbose:, force:)
end

.install_verb(name, options = {}) ⇒ Object

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.



47
48
49
50
51
52
53
# File 'bundle/brew.rb', line 47

def install_verb(name, options = {})
  _ = options

  return "Installing" unless formula_upgradable?(name)

  "Upgrading"
end

.installed_formulaeObject

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.



91
92
93
# File 'bundle/brew.rb', line 91

def installed_formulae
  @installed_formulae ||= formulae.map { |f| f[:name] }
end

.no_upgrade_with_args?(no_upgrade, formula_name) ⇒ 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.

Returns:

  • (Boolean)


62
63
64
# File 'bundle/brew.rb', line 62

def no_upgrade_with_args?(no_upgrade, formula_name)
  no_upgrade && Bundle.upgrade_formulae.exclude?(formula_name)
end

.outdated_formulaeObject

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.



99
100
101
# File 'bundle/brew.rb', line 99

def outdated_formulae
  @outdated_formulae ||= formulae.filter_map { |f| f[:name] if f[:outdated?] }
end

.pinned_formulaeObject

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.



103
104
105
# File 'bundle/brew.rb', line 103

def pinned_formulae
  @pinned_formulae ||= formulae.filter_map { |f| f[:name] if f[:pinned?] }
end

.preinstall!(name, no_upgrade: false, verbose: false, **options) ⇒ Object

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.



39
40
41
# File 'bundle/brew.rb', line 39

def preinstall!(name, no_upgrade: false, verbose: false, **options)
  new(name, options).preinstall!(no_upgrade:, verbose:)
end

.reset!Object

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.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'bundle/brew.rb', line 25

def reset!
  require "bundle/brew_services"

  Homebrew::Bundle::Brew::Services.reset!
  @installed_formulae = nil
  @outdated_formulae = nil
  @pinned_formulae = nil
  @formulae = nil
  @formulae_by_full_name = nil
  @formulae_by_name = nil
  @formula_aliases = nil
  @formula_oldnames = nil
end

.upgradable_formulaeObject

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.



95
96
97
# File 'bundle/brew.rb', line 95

def upgradable_formulae
  outdated_formulae - pinned_formulae
end

Instance Method Details

#changed?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.

Returns:

  • (Boolean)


432
433
434
# File 'bundle/brew.rb', line 432

def changed?
  @changed.present?
end

#install!(preinstall: true, no_upgrade: false, verbose: false, force: false) ⇒ Object

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.



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
# File 'bundle/brew.rb', line 361

def install!(preinstall: true, no_upgrade: false, verbose: false, force: false)
  install_result = if preinstall
    install_change_state!(no_upgrade:, verbose:, force:)
  else
    true
  end
  result = install_result

  if installed?
    service_result = service_change_state!(verbose:)
    result &&= service_result

    link_result = link_change_state!(verbose:)
    result &&= link_result

    postinstall_result = postinstall_change_state!(verbose:)
    result &&= postinstall_result

    if result && @version_file.present?
      # Use the version from the environment if it hasn't changed.
      # Strip the revision number because it's not part of the non-Homebrew version.
      version = if !changed? && (env_version = Bundle.formula_versions_from_env(@name))
        PkgVersion.parse(env_version).version
      else
        Formula[@full_name].version
      end.to_s
      File.write(@version_file, "#{version}\n")

      puts "Wrote #{@name} version #{version} to #{@version_file}" if verbose
    end
  end

  result
end

#install_change_state!(no_upgrade:, verbose:, force:) ⇒ Object

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.



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'bundle/brew.rb', line 396

def install_change_state!(no_upgrade:, verbose:, force:)
  require "tap"
  if (tap_with_name = ::Tap.with_formula_name(@full_name))
    tap, = tap_with_name
    tap.ensure_installed!
  end

  return false unless resolve_conflicts!(verbose:)

  if installed?
    upgrade_formula!(verbose:, force:)
  else
    install_formula!(verbose:, force:)
  end
end

#installed_and_up_to_date?(formula, no_upgrade: false) ⇒ 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.

Returns:

  • (Boolean)


347
348
349
# File 'bundle/brew.rb', line 347

def installed_and_up_to_date?(formula, no_upgrade: false)
  self.class.formula_installed_and_up_to_date?(formula, no_upgrade:)
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.



452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'bundle/brew.rb', line 452

def link_change_state!(verbose: false)
  link_args = []
  link_args << "--force" if unlinked_and_keg_only?

  cmd = case @link
  when :overwrite
    link_args << "--overwrite"
    "link" unless linked?
  when true
    "link" unless linked?
  when false
    "unlink" if linked?
  when nil
    if keg_only?
      "unlink" if linked?
    else
      "link" unless linked?
    end
  end

  if cmd.present?
    verb = "#{cmd}ing".capitalize
    with_args = " with #{link_args.join(" ")}" if link_args.present?
    puts "#{verb} #{@name} formula#{with_args}." if verbose
    return Bundle.brew(cmd, *link_args, @name, verbose:)
  end

  true
end

#postinstall_change_state!(verbose:) ⇒ Object

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.



482
483
484
485
486
487
488
# File 'bundle/brew.rb', line 482

def postinstall_change_state!(verbose:)
  return true if @postinstall.blank?
  return true unless changed?

  puts "Running postinstall for #{@name}: #{@postinstall}" if verbose
  Kernel.system(@postinstall)
end

#preinstall!(no_upgrade: false, verbose: false) ⇒ Object

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.



351
352
353
354
355
356
357
358
359
# File 'bundle/brew.rb', line 351

def preinstall!(no_upgrade: false, verbose: false)
  if installed? && (self.class.no_upgrade_with_args?(no_upgrade, @name) || !upgradable?)
    puts "Skipping install of #{@name} formula. It is already installed." if verbose
    @changed = nil
    return false
  end

  true
end

#restart_service?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.

Returns:

  • (Boolean)


421
422
423
# File 'bundle/brew.rb', line 421

def restart_service?
  @restart_service.present?
end

#restart_service_needed?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.

Returns:

  • (Boolean)


425
426
427
428
429
430
# File 'bundle/brew.rb', line 425

def restart_service_needed?
  return false unless restart_service?

  # Restart if `restart_service: :always`, or if the formula was installed or upgraded
  @restart_service.to_s == "always" || changed?
end

#service_change_state!(verbose:) ⇒ Object

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.



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'bundle/brew.rb', line 436

def service_change_state!(verbose:)
  require "bundle/brew_services"

  file = BrewServices.versioned_service_file(@name)

  if restart_service_needed?
    puts "Restarting #{@name} service." if verbose
    BrewServices.restart(@full_name, file:, verbose:)
  elsif start_service_needed?
    puts "Starting #{@name} service." if verbose
    BrewServices.start(@full_name, file:, verbose:)
  else
    true
  end
end

#start_service?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.

Returns:

  • (Boolean)


412
413
414
# File 'bundle/brew.rb', line 412

def start_service?
  @start_service.present?
end

#start_service_needed?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.

Returns:

  • (Boolean)


416
417
418
419
# File 'bundle/brew.rb', line 416

def start_service_needed?
  require "bundle/brew_services"
  start_service? && !BrewServices.started?(@full_name)
end