Module: Kernel
- Included in:
- Cask::DSL::DependsOn, Dependencies, DependenciesHelpers, 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
- #disk_usage_readable(size_in_bytes) ⇒ String private
-
#ensure_executable!(name, formula_name = nil, reason: "", latest: false) ⇒ Pathname?
private
Ensure the given executable is exist 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
- #number_readable(number) ⇒ String private
-
#quiet_system(cmd, argv0 = nil, *args) ⇒ Boolean
internal
Run a system command without any output.
- #redact_secrets(input, secrets) ⇒ String private
- #redirect_stdout(file, &_block) ⇒ T.type_parameter(:U) private
-
#safe_system(cmd, argv0 = nil, *args, **options) ⇒ void
private
Kernel.system but with exceptions.
- #tap_and_name_comparison ⇒ T.proc.params(a: String, b: String).returns(Integer) private
-
#truncate_text_to_approximate_size(str, max_bytes, options = {}) ⇒ String
private
Truncates a text string to fit within a byte size constraint, preserving character encoding validity.
-
#which(cmd, path = ENV.fetch("PATH")) ⇒ Pathname?
Find a command.
- #which_editor(silent: false) ⇒ String private
- #with_custom_locale(locale, &block) ⇒ T.type_parameter(:U) private
-
#with_env(hash, &_block) ⇒ T.type_parameter(:U)
Calls the given block with the passed environment variables added to
ENV
, then restoresENV
afterwards. - #with_homebrew_path(&block) ⇒ T.type_parameter(:U) private
Instance Method Details
#disk_usage_readable(size_in_bytes) ⇒ 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.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'extend/kernel.rb', line 205 def disk_usage_readable(size_in_bytes) if size_in_bytes.abs >= 1_073_741_824 size = size_in_bytes.to_f / 1_073_741_824 unit = "GB" elsif size_in_bytes.abs >= 1_048_576 size = size_in_bytes.to_f / 1_048_576 unit = "MB" elsif size_in_bytes.abs >= 1_024 size = size_in_bytes.to_f / 1_024 unit = "KB" else size = size_in_bytes unit = "B" end # avoid trailing zero after decimal point if ((size * 10).to_i % 10).zero? "#{size.to_i}#{unit}" else "#{format("%<size>.1f", size:)}#{unit}" end end |
#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 is exist otherwise install the brewed version
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'extend/kernel.rb', line 187 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.first return executable if executable.exist? require "formula" Formula[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.
136 137 138 139 140 141 142 143 144 145 146 |
# File 'extend/kernel.rb', line 136 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.
130 131 132 133 |
# File 'extend/kernel.rb', line 130 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.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'extend/kernel.rb', line 151 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 |
# 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 Process.wait fork { exec Utils::Shell.preferred_path(default: "/bin/bash") } 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 |
#number_readable(number) ⇒ 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.
229 230 231 232 233 |
# File 'extend/kernel.rb', line 229 def number_readable(number) numstr = number.to_i.to_s (numstr.size - 3).step(1, -3) { |i| numstr.insert(i.to_i, ",") } numstr 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.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'extend/kernel.rb', line 77 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 |
#redact_secrets(input, secrets) ⇒ 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.
318 319 320 321 322 |
# File 'extend/kernel.rb', line 318 def redact_secrets(input, secrets) secrets.compact .reduce(input) { |str, secret| str.gsub secret, "******" } .freeze 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.
176 177 178 179 180 181 182 183 |
# File 'extend/kernel.rb', line 176 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.
58 59 60 61 62 63 64 65 |
# File 'extend/kernel.rb', line 58 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 |
#tap_and_name_comparison ⇒ T.proc.params(a: String, b: String).returns(Integer)
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.
305 306 307 308 309 310 311 312 313 314 315 |
# File 'extend/kernel.rb', line 305 def tap_and_name_comparison proc do |a, b| if a.include?("/") && b.exclude?("/") 1 elsif a.exclude?("/") && b.include?("/") -1 else a <=> b end end end |
#truncate_text_to_approximate_size(str, max_bytes, options = {}) ⇒ 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.
Truncates a text string to fit within a byte size constraint, preserving character encoding validity. The returned string will be not much longer than the specified max_bytes, though the exact shortfall or overrun may vary.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'extend/kernel.rb', line 240 def truncate_text_to_approximate_size(str, max_bytes, = {}) front_weight = .fetch(:front_weight, 0.5) raise "opts[:front_weight] must be between 0.0 and 1.0" if front_weight < 0.0 || front_weight > 1.0 return str if str.bytesize <= max_bytes glue = "\n[...snip...]\n" max_bytes_in = [max_bytes - glue.bytesize, 1].max bytes = str.dup.force_encoding("BINARY") glue_bytes = glue.encode("BINARY") n_front_bytes = (max_bytes_in * front_weight).floor n_back_bytes = max_bytes_in - n_front_bytes if n_front_bytes.zero? front = bytes[1..0] back = bytes[-max_bytes_in..] elsif n_back_bytes.zero? front = bytes[0..(max_bytes_in - 1)] back = bytes[1..0] else front = bytes[0..(n_front_bytes - 1)] back = bytes[-n_back_bytes..] end out = T.must(front) + glue_bytes + T.must(back) out.force_encoding("UTF-8") out.encode!("UTF-16", invalid: :replace) out.encode!("UTF-8") out end |
#which(cmd, path = ENV.fetch("PATH")) ⇒ Pathname?
Find a command.
93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'extend/kernel.rb', line 93 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.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'extend/kernel.rb', line 108 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_custom_locale(locale, &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.
45 46 47 |
# File 'extend/kernel.rb', line 45 def with_custom_locale(locale, &block) with_env(LC_ALL: locale, &block) 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
289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'extend/kernel.rb', line 289 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 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.
36 37 38 |
# File 'extend/kernel.rb', line 36 def with_homebrew_path(&block) with_env(PATH: PATH.new(ORIGINAL_PATHS), &block) end |