Class: Homebrew::Bundle::BrewInstaller Private

Inherits:
Object
  • Object
show all
Defined in:
bundle/brew_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 = {}) ⇒ BrewInstaller

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

[View source]

21
22
23
24
25
26
27
28
29
30
31
32
# File 'bundle/brew_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)
[View source]

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

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

  require "bundle/brew_dumper"
  old_names = Homebrew::Bundle::BrewDumper.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::BrewDumper.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)
[View source]

196
197
198
# File 'bundle/brew_installer.rb', line 196

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)
[View source]

167
168
169
170
171
172
# File 'bundle/brew_installer.rb', line 167

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)
[View source]

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

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.

[View source]

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

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

[View source]

17
18
19
# File 'bundle/brew_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.

[View source]

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

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)
[View source]

174
175
176
# File 'bundle/brew_installer.rb', line 174

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.

[View source]

213
214
215
# File 'bundle/brew_installer.rb', line 213

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.

[View source]

217
218
219
# File 'bundle/brew_installer.rb', line 217

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.

[View source]

13
14
15
# File 'bundle/brew_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.

[View source]

7
8
9
10
11
# File 'bundle/brew_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.

[View source]

209
210
211
# File 'bundle/brew_installer.rb', line 209

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)
[View source]

109
110
111
# File 'bundle/brew_installer.rb', line 109

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.

[View source]

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/brew_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.

[View source]

79
80
81
82
83
84
85
86
87
# File 'bundle/brew_installer.rb', line 79

def install_change_state!(no_upgrade:, verbose:, force:)
  return false unless resolve_conflicts!(verbose:)

  if installed?
    upgrade!(verbose:, force:)
  else
    install!(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.

[View source]

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
157
# File 'bundle/brew_installer.rb', line 129

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.

[View source]

159
160
161
162
163
164
165
# File 'bundle/brew_installer.rb', line 159

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.

[View source]

34
35
36
37
38
39
40
41
42
# File 'bundle/brew_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)
[View source]

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

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)
[View source]

102
103
104
105
106
107
# File 'bundle/brew_installer.rb', line 102

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.

[View source]

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'bundle/brew_installer.rb', line 113

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)
[View source]

89
90
91
# File 'bundle/brew_installer.rb', line 89

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)
[View source]

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

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