Module: Kernel
- Included in:
- Cask::DSL::DependsOn, Dependencies, DependenciesHelpers, Homebrew::TestBot, Homebrew::TestBot::TestFormulae, Homebrew::TestBot::TestRunner, Ignorable, Requirements, Utils::AST
- Defined in:
- extend/kernel.rb
Constant Summary collapse
- IGNORE_INTERRUPTS_MUTEX =
T.let(Thread::Mutex.new.freeze, Thread::Mutex)
Instance Method Summary collapse
-
#ensure_executable!(name, formula_name = nil, reason: "", latest: false) ⇒ Pathname
private
Ensure the given executable exists otherwise install the brewed version.
- #exec_browser(*args) ⇒ void private
- #exec_editor(*filenames) ⇒ void private
- #ignore_interrupts(&_block) ⇒ T.type_parameter(:U) private
- #interactive_shell(formula = nil) ⇒ void private
-
#quiet_system(cmd, argv0 = nil, *args) ⇒ Boolean
internal
Run a system command without any output.
- #redirect_stdout(file, &_block) ⇒ T.type_parameter(:U) private
-
#safe_system(cmd, argv0 = nil, *args, **options) ⇒ void
private
Kernel.system but with exceptions.
-
#which(cmd, path = ENV.fetch("PATH")) ⇒ Pathname?
Find a command.
- #which_editor(silent: false) ⇒ String private
-
#with_env(hash, &_block) ⇒ T.type_parameter(:U)
Calls the given block with the passed environment variables added to
ENV, then restoresENVafterwards. - #with_homebrew_path(&block) ⇒ T.type_parameter(:U) private
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
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'extend/kernel.rb', line 181 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" Formulary.factory_stub(formula_name).ensure_installed!(reason:, latest:).opt_bin/name 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.
130 131 132 133 134 135 136 137 138 139 140 |
# File 'extend/kernel.rb', line 130 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.
124 125 126 127 |
# File 'extend/kernel.rb', line 124 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.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'extend/kernel.rb', line 145 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.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'extend/kernel.rb', line 16 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.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'extend/kernel.rb', line 71 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.
170 171 172 173 174 175 176 177 |
# File 'extend/kernel.rb', line 170 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.
52 53 54 55 56 57 58 59 |
# File 'extend/kernel.rb', line 52 def safe_system(cmd, argv0 = nil, *args, **) # TODO: migrate to utils.rb Homebrew.safe_system require "utils" return if Homebrew.system(cmd, argv0, *args, **) raise ErrorDuringExecution.new([cmd, argv0, *args], status: $CHILD_STATUS) end |
#which(cmd, path = ENV.fetch("PATH")) ⇒ Pathname?
Find a command.
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'extend/kernel.rb', line 87 def which(cmd, path = ENV.fetch("PATH")) PATH.new(path).each do |p| begin pcmd = File.(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.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'extend/kernel.rb', line 102 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)
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
221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'extend/kernel.rb', line 221 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.
39 40 41 |
# File 'extend/kernel.rb', line 39 def with_homebrew_path(&block) with_env(PATH: PATH.new(ORIGINAL_PATHS).to_s, &block) end |