Module: Kernel Private

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.

TODO:

move all of these to other modules e.g. Utils.

Homebrew extends Ruby's Kernel to make our code more readable. Extending Kernel makes these methods available globally.

Constant Summary collapse

IGNORE_INTERRUPTS_MUTEX =

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.

T.let(Thread::Mutex.new.freeze, Thread::Mutex)

Instance Method Summary collapse

Instance Method Details

#ensure_executable!(name, formula_name = nil, reason: "", latest: false) ⇒ Pathname

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.

Ensure the given executable exists otherwise install the brewed version

Parameters:

  • name (String)
  • formula_name (String, nil) (defaults to: nil)
  • reason (String) (defaults to: "")
  • latest (Boolean) (defaults to: false)

Returns:



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'extend/kernel.rb', line 185

def ensure_executable!(name, formula_name = nil, reason: "", latest: false)
  formula_name ||= name

  executable = [
    which(name),
    which(name, ORIGINAL_PATHS),
    # We prefer the opt_bin path to a formula's executable over the prefix
    # path where available, since the former is stable during upgrades.
    HOMEBREW_PREFIX/"opt/#{formula_name}/bin/#{name}",
    HOMEBREW_PREFIX/"bin/#{name}",
  ].compact.find(&:exist?)
  return executable if executable

  require "formula"
  T.cast(Formula[formula_name].ensure_installed!(reason:, latest:, executable: name), Pathname)
end

#exec_browser(*args) ⇒ void

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.

This method returns an undefined value.

Parameters:



134
135
136
137
138
139
140
141
142
143
144
# File 'extend/kernel.rb', line 134

def exec_browser(*args)
  browser = Homebrew::EnvConfig.browser
  browser ||= OS::PATH_OPEN if defined?(OS::PATH_OPEN)
  return unless browser

  ENV["DISPLAY"] = Homebrew::EnvConfig.display

  with_env(DBUS_SESSION_BUS_ADDRESS: ENV.fetch("HOMEBREW_DBUS_SESSION_BUS_ADDRESS", nil)) do
    safe_system(browser, *args)
  end
end

#exec_editor(*filenames) ⇒ void

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.

This method returns an undefined value.

Parameters:



128
129
130
131
# File 'extend/kernel.rb', line 128

def exec_editor(*filenames)
  puts "Editing #{filenames.join "\n"}"
  with_homebrew_path { safe_system(*which_editor.shellsplit, *filenames) }
end

#ignore_interrupts(&_block) ⇒ T.type_parameter(:U)

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.

Parameters:

  • _block (T.proc.returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U))


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'extend/kernel.rb', line 149

def ignore_interrupts(&_block)
  IGNORE_INTERRUPTS_MUTEX.synchronize do
    interrupted = T.let(false, T::Boolean)
    old_sigint_handler = trap(:INT) do
      interrupted = true

      $stderr.print "\n"
      $stderr.puts "One sec, cleaning up..."
    end

    begin
      yield
    ensure
      trap(:INT, old_sigint_handler)

      raise Interrupt if interrupted
    end
  end
end

#interactive_shell(formula = nil) ⇒ void

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.

This method returns an undefined value.

Parameters:

  • formula (Formula, nil) (defaults to: nil)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'extend/kernel.rb', line 19

def interactive_shell(formula = nil)
  unless formula.nil?
    ENV["HOMEBREW_DEBUG_PREFIX"] = formula.prefix.to_s
    ENV["HOMEBREW_DEBUG_INSTALL"] = formula.full_name
  end

  if Utils::Shell.preferred == :zsh && (home = Dir.home).start_with?(HOMEBREW_TEMP.resolved_path.to_s)
    FileUtils.mkdir_p home
    FileUtils.touch "#{home}/.zshrc"
  end

  term = ENV.fetch("HOMEBREW_TERM", ENV.fetch("TERM", nil))
  with_env(TERM: term) do
    Process.wait fork { exec Utils::Shell.preferred_path(default: "/bin/bash") }
  end

  return if $CHILD_STATUS.success?
  raise "Aborted due to non-zero exit status (#{$CHILD_STATUS.exitstatus})" if $CHILD_STATUS.exited?

  raise $CHILD_STATUS.inspect
end

#quiet_system(cmd, argv0 = nil, *args) ⇒ Boolean

This method is part of an internal API. This method may only be used internally in repositories owned by Homebrew, except in casks or formulae. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Run a system command without any output.

Parameters:

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
# File 'extend/kernel.rb', line 74

def quiet_system(cmd, argv0 = nil, *args)
  # TODO: migrate to utils.rb Homebrew.quiet_system
  require "utils"

  Homebrew._system(cmd, argv0, *args) do
    # Redirect output streams to `/dev/null` instead of closing as some programs
    # will fail to execute if they can't write to an open stream.
    $stdout.reopen(File::NULL)
    $stderr.reopen(File::NULL)
  end
end

#redirect_stdout(file, &_block) ⇒ T.type_parameter(:U)

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.

Parameters:

Returns:

  • (T.type_parameter(:U))


174
175
176
177
178
179
180
181
# File 'extend/kernel.rb', line 174

def redirect_stdout(file, &_block)
  out = $stdout.dup
  $stdout.reopen(file)
  yield
ensure
  $stdout.reopen(out)
  out.close
end

#safe_system(cmd, argv0 = nil, *args, **options) ⇒ void

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.

This method returns an undefined value.

Kernel.system but with exceptions.

Parameters:

Raises:



55
56
57
58
59
60
61
62
# File 'extend/kernel.rb', line 55

def safe_system(cmd, argv0 = nil, *args, **options)
  # TODO: migrate to utils.rb Homebrew.safe_system
  require "utils"

  return if Homebrew.system(cmd, argv0, *args, **options)

  raise ErrorDuringExecution.new([cmd, argv0, *args], status: $CHILD_STATUS)
end

#which(cmd, path = ENV.fetch("PATH")) ⇒ Pathname?

Find a command.

Keep in sync with which in Library/Homebrew/utils.sh.

Parameters:

Returns:



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'extend/kernel.rb', line 91

def which(cmd, path = ENV.fetch("PATH"))
  PATH.new(path).each do |p|
    begin
      pcmd = File.expand_path(cmd, p)
    rescue ArgumentError
      # File.expand_path will raise an ArgumentError if the path is malformed.
      # See https://github.com/Homebrew/legacy-homebrew/issues/32789
      next
    end
    return Pathname.new(pcmd) if File.file?(pcmd) && File.executable?(pcmd)
  end
  nil
end

#which_editor(silent: false) ⇒ 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.

Parameters:

  • silent (Boolean) (defaults to: false)

Returns:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'extend/kernel.rb', line 106

def which_editor(silent: false)
  editor = Homebrew::EnvConfig.editor
  return editor if editor

  # Find VS Code variants, Sublime Text, Textmate, BBEdit, or vim
  editor = %w[code codium cursor code-insiders subl mate bbedit vim].find do |candidate|
    candidate if which(candidate, ORIGINAL_PATHS)
  end
  editor ||= "vim"

  unless silent
    Utils::Output.opoo <<~EOS
      Using #{editor} because no editor was set in the environment.
      This may change in the future, so we recommend setting `$EDITOR`
      or `$HOMEBREW_EDITOR` to your preferred text editor.
    EOS
  end

  editor
end

#with_env(hash, &_block) ⇒ T.type_parameter(:U)

Note:

This method is not thread-safe – other threads which happen to be scheduled during the block will also see these environment variables.

Calls the given block with the passed environment variables added to ENV, then restores ENV afterwards.

Example

with_env(PATH: "/bin") do
  system "echo $PATH"
end

Parameters:

Returns:

  • (T.type_parameter(:U))


225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'extend/kernel.rb', line 225

def with_env(hash, &_block)
  old_values = {}
  begin
    hash.each do |key, value|
      key = key.to_s
      old_values[key] = ENV.delete(key)
      ENV[key] = value&.to_s
    end

    yield
  ensure
    ENV.update(old_values)
  end
end

#with_homebrew_path(&block) ⇒ T.type_parameter(:U)

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.

Parameters:

  • block (T.proc.returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U))


42
43
44
# File 'extend/kernel.rb', line 42

def with_homebrew_path(&block)
  with_env(PATH: PATH.new(ORIGINAL_PATHS).to_s, &block)
end