Module: Utils::Shell Private
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.
Constant Summary collapse
- SHELL_PROFILE_MAP =
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( { bash: "~/.profile", csh: "~/.cshrc", fish: "~/.config/fish/config.fish", ksh: "~/.kshrc", mksh: "~/.kshrc", pwsh: "~/.config/powershell/Microsoft.PowerShell_profile.ps1", rc: "~/.rcrc", sh: "~/.profile", tcsh: "~/.tcshrc", zsh: "~/.zshrc", }.freeze, T::Hash[Symbol, String], )
- UNSAFE_SHELL_CHAR =
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.
%r{([^A-Za-z0-9_\-.,:/@~+\n])}
Class Method Summary collapse
- .csh_quote(str) ⇒ String private
-
.export_value(key, value, shell = preferred) ⇒ String?
private
Quote values.
-
.from_path(path) ⇒ Symbol?
private
Take a path and heuristically convert it to a shell name, return
nilif there's no match. - .interactive(formula = nil) ⇒ void private
- .parent ⇒ Symbol? private
- .preferred ⇒ Symbol? private
- .preferred_path(default: "") ⇒ String private
- .prepend_path_in_profile(path) ⇒ String? private
-
.profile ⇒ String
private
Return the shell profile file based on user's preferred shell.
- .set_variable_in_profile(variable, value) ⇒ String? private
- .sh_quote(str) ⇒ String private
- .shell_with_prompt(type, preferred_path:, notice:, home: Dir.home) ⇒ String private
- .which(cmd, path = ENV.fetch("PATH")) ⇒ Pathname? private
- .with_env(hash, &_block) ⇒ T.type_parameter(:U) private
- .with_homebrew_path(&block) ⇒ T.type_parameter(:U) private
Class Method Details
.csh_quote(str) ⇒ 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.
196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'utils/shell.rb', line 196 def csh_quote(str) # Ruby's implementation of `shell_escape`. str = str.to_s return "''" if str.empty? str = str.dup # Anything that isn't a known safe character is padded. str.gsub!(UNSAFE_SHELL_CHAR, '\\\\\\1') # Newlines have to be specially quoted in `csh`. str.gsub!("\n", "'\\\n'") str end |
.export_value(key, value, shell = preferred) ⇒ 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.
Quote values. Quoting keys is overkill.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'utils/shell.rb', line 106 def export_value(key, value, shell = preferred) case shell when :bash, :ksh, :mksh, :sh, :zsh "export #{key}=\"#{sh_quote(value)}\"" when :fish # fish quoting is mostly Bourne compatible except that # a single quote can be included in a single-quoted string via \' # and a literal \ can be included via \\ "set -gx #{key} \"#{sh_quote(value)}\"" when :rc "#{key}=(#{sh_quote(value)})" when :csh, :tcsh "setenv #{key} #{csh_quote(value)};" end end |
.from_path(path) ⇒ Symbol?
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.
Take a path and heuristically convert it to a shell name,
return nil if there's no match.
81 82 83 84 85 86 87 |
# File 'utils/shell.rb', line 81 def from_path(path) # we only care about the basename shell_name = File.basename(path) # handle possible version suffix like `zsh-5.2` shell_name.sub!(/-.*\z/m, "") shell_name.to_sym if %w[bash csh fish ksh mksh pwsh rc sh tcsh zsh].include?(shell_name) end |
.interactive(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.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'utils/shell.rb', line 56 def interactive(formula = nil) unless formula.nil? ENV["HOMEBREW_DEBUG_PREFIX"] = formula.prefix.to_s ENV["HOMEBREW_DEBUG_INSTALL"] = formula.full_name end if preferred == :zsh && (home = Dir.home).start_with?(Utils::Path.resolved_path(HOMEBREW_TEMP).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 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 |
.parent ⇒ Symbol?
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.
100 101 102 |
# File 'utils/shell.rb', line 100 def parent from_path(Utils.popen_read_text("ps", "-p", Process.ppid.to_s, "-o", "ucomm=", err: :err).strip) end |
.preferred ⇒ Symbol?
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.
95 96 97 |
# File 'utils/shell.rb', line 95 def preferred from_path(preferred_path) end |
.preferred_path(default: "") ⇒ 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.
90 91 92 |
# File 'utils/shell.rb', line 90 def preferred_path(default: "") ENV.fetch("SHELL", default) end |
.prepend_path_in_profile(path) ⇒ 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.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'utils/shell.rb', line 162 def prepend_path_in_profile(path) case preferred when :bash, :ksh, :mksh, :sh, :zsh, nil "echo 'export PATH=\"#{sh_quote(path)}:$PATH\"' >> #{profile}" when :pwsh "$env:PATH = '#{path}' + \":${env:PATH}\" >> #{profile}" when :rc "echo 'path=(#{sh_quote(path)} $path)' >> #{profile}" when :csh, :tcsh "echo 'setenv PATH #{csh_quote(path)}:$PATH' >> #{profile}" when :fish "fish_add_path #{sh_quote(path)}" end end |
.profile ⇒ 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.
Return the shell profile file based on user's preferred shell.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'utils/shell.rb', line 124 def profile case preferred when :bash bash_profile = "#{Dir.home}/.bash_profile" return bash_profile if File.exist? bash_profile when :pwsh pwsh_profile = "#{Dir.home}/.config/powershell/Microsoft.PowerShell_profile.ps1" return pwsh_profile if File.exist? pwsh_profile when :rc rc_profile = "#{Dir.home}/.rcrc" return rc_profile if File.exist? rc_profile when :zsh return "#{ENV["HOMEBREW_ZDOTDIR"]}/.zshrc" if ENV["HOMEBREW_ZDOTDIR"].present? end shell = preferred return "~/.profile" if shell.nil? SHELL_PROFILE_MAP.fetch(shell, "~/.profile") end |
.set_variable_in_profile(variable, value) ⇒ 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.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'utils/shell.rb', line 146 def set_variable_in_profile(variable, value) case preferred when :bash, :ksh, :mksh, :sh, :zsh, nil "echo 'export #{variable}=#{sh_quote(value)}' >> #{profile}" when :pwsh "$env:#{variable}='#{value}' >> #{profile}" when :rc "echo '#{variable}=(#{sh_quote(value)})' >> #{profile}" when :csh, :tcsh "echo 'setenv #{variable} #{csh_quote(value)}' >> #{profile}" when :fish "echo 'set -gx #{variable} #{sh_quote(value)}' >> #{profile}" end end |
.sh_quote(str) ⇒ 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.
210 211 212 213 214 215 216 217 218 219 220 |
# File 'utils/shell.rb', line 210 def sh_quote(str) # Ruby's implementation of `shell_escape`. str = str.to_s return "''" if str.empty? str = str.dup # Anything that isn't a known safe character is padded. str.gsub!(UNSAFE_SHELL_CHAR, '\\\\\\1') str.gsub!("\n", "'\n'") str end |
.shell_with_prompt(type, preferred_path:, notice:, home: Dir.home) ⇒ 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.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'utils/shell.rb', line 223 def shell_with_prompt(type, preferred_path:, notice:, home: Dir.home) preferred = from_path(preferred_path) path = ENV.fetch("PATH") subshell = case preferred when :zsh zdotdir = Pathname.new(HOMEBREW_TEMP/"brew-zsh-prompt-#{Process.euid}") zdotdir.mkpath FileUtils.chmod_R(0700, zdotdir) FileUtils.cp(HOMEBREW_LIBRARY_PATH/"utils/zsh/brew-sh-prompt-zshrc.zsh", zdotdir/".zshrc") %w[.zshenv .zcompdump .zsh_history .zsh_sessions].each do |file| FileUtils.ln_sf("#{home}/#{file}", zdotdir/file) end <<~ZSH.strip BREW_PROMPT_PATH="#{path}" BREW_PROMPT_TYPE="#{type}" ZDOTDIR="#{zdotdir}" #{preferred_path} ZSH when :bash <<~BASH.strip BREW_PROMPT_PATH="#{path}" BREW_PROMPT_TYPE="#{type}" #{preferred_path} --rcfile "#{HOMEBREW_LIBRARY_PATH}/utils/bash/brew-sh-prompt-bashrc.bash" BASH else "PS1=\"\\[\\033[1;32m\\]#{type} \\[\\033[1;31m\\]\\w \\[\\033[1;34m\\]$\\[\\033[0m\\] \" #{preferred_path}" end puts notice if notice.present? $stdout.flush subshell end |
.which(cmd, path = ENV.fetch("PATH")) ⇒ 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.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'utils/shell.rb', line 16 def which(cmd, path = ENV.fetch("PATH")) PATH.new(path).each do |entry| begin executable = File.(cmd, entry) rescue ArgumentError next end return Pathname.new(executable) if File.file?(executable) && File.executable?(executable) end nil end |
.with_env(hash, &_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.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'utils/shell.rb', line 35 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.
51 52 53 |
# File 'utils/shell.rb', line 51 def with_homebrew_path(&block) with_env(PATH: PATH.new(ORIGINAL_PATHS).to_s, &block) end |