Class: Homebrew::Bundle::FormulaInstaller Private

Inherits:
Object
  • Object
show all
Defined in:
bundle/formula_installer.rb

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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ FormulaInstaller

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 FormulaInstaller.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'bundle/formula_installer.rb', line 21

def initialize(name, options = {})
  @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

.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)


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'bundle/formula_installer.rb', line 183

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

  require "bundle/formula_dumper"
  old_names = Homebrew::Bundle::FormulaDumper.formula_oldnames
  old_name = old_names[formula]
  old_name ||= old_names[formula.split("/").last]
  return true if old_name && array.include?(old_name)

  resolved_full_name = Homebrew::Bundle::FormulaDumper.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)


201
202
203
# File 'bundle/formula_installer.rb', line 201

def self.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)


172
173
174
175
176
177
# File 'bundle/formula_installer.rb', line 172

def self.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_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)


205
206
207
208
# File 'bundle/formula_installer.rb', line 205

def self.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.



226
227
228
229
# File 'bundle/formula_installer.rb', line 226

def self.formulae
  require "bundle/formula_dumper"
  Homebrew::Bundle::FormulaDumper.formulae
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.



17
18
19
# File 'bundle/formula_installer.rb', line 17

def self.install!(name, preinstall: true, no_upgrade: false, verbose: false, force: false, **options)
  new(name, options).install!(preinstall:, no_upgrade:, verbose:, force:)
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.



210
211
212
# File 'bundle/formula_installer.rb', line 210

def self.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)


179
180
181
# File 'bundle/formula_installer.rb', line 179

def self.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.



218
219
220
# File 'bundle/formula_installer.rb', line 218

def self.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.



222
223
224
# File 'bundle/formula_installer.rb', line 222

def self.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.



13
14
15
# File 'bundle/formula_installer.rb', line 13

def self.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.



7
8
9
10
11
# File 'bundle/formula_installer.rb', line 7

def self.reset!
  @installed_formulae = nil
  @outdated_formulae = nil
  @pinned_formulae = 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.



214
215
216
# File 'bundle/formula_installer.rb', line 214

def self.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)


114
115
116
# File 'bundle/formula_installer.rb', line 114

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.



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

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.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'bundle/formula_installer.rb', line 79

def install_change_state!(no_upgrade:, verbose:, force:)
  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

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.



134
135
136
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
# File 'bundle/formula_installer.rb', line 134

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.



164
165
166
167
168
169
170
# File 'bundle/formula_installer.rb', line 164

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.



34
35
36
37
38
39
40
41
42
# File 'bundle/formula_installer.rb', line 34

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)


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

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)


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

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.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'bundle/formula_installer.rb', line 118

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

  file = Bundle::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)


94
95
96
# File 'bundle/formula_installer.rb', line 94

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)


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

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