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.
-
.fish_quote(str) ⇒ String
private
Inside fish single quotes
\'and\\are escapes, so both have to be escaped rather than closing and reopening the string as in POSIX shells. -
.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.
-
.pwsh_quote(str) ⇒ String
private
PowerShell single-quoted strings take a literal
'as''and expand nothing else, so one pass covers$,"and backticks too. -
.rc_quote(str) ⇒ String
private
rc has no backslash escapes: only a single-quoted string is literal, and an embedded
'is written''. - .set_variable_in_profile(variable, value) ⇒ String? private
- .sh_quote(str) ⇒ String private
-
.sh_single_quote(str) ⇒ String
private
A single-quoted string ends at the first
', so an embedded one has to close the string, escape the quote and reopen it. - .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.
198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'utils/shell.rb', line 198 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 121 122 |
# 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 :pwsh "$env:#{key} = #{pwsh_quote(value)}" when :rc "#{key}=(#{rc_quote(value)})" when :csh, :tcsh "setenv #{key} #{csh_quote(value)};" end end |
.fish_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.
Inside fish single quotes \' and \\ are escapes, so both have to be
escaped rather than closing and reopening the string as in POSIX shells.
222 223 224 |
# File 'utils/shell.rb', line 222 def fish_quote(str) "'#{str.gsub("\\", "\\\\\\\\").gsub("'", "\\\\'")}'" 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.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'utils/shell.rb', line 164 def prepend_path_in_profile(path) case preferred when :bash, :ksh, :mksh, :sh, :zsh, nil "echo #{sh_single_quote("export PATH=#{sh_quote(path)}:$PATH")} >> #{profile}" when :pwsh "#{pwsh_quote("$env:PATH = #{pwsh_quote(path)} + \":$env:PATH\"")} >> #{profile}" when :rc "echo #{rc_quote("path=(#{rc_quote(path)} $path)")} >> #{profile}" when :csh, :tcsh "echo #{sh_single_quote("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.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'utils/shell.rb', line 126 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 |
.pwsh_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.
PowerShell single-quoted strings take a literal ' as '' and expand
nothing else, so one pass covers $, " and backticks too.
229 230 231 |
# File 'utils/shell.rb', line 229 def pwsh_quote(str) "'#{str.gsub("'", "''")}'" end |
.rc_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.
rc has no backslash escapes: only a single-quoted string is literal, and
an embedded ' is written ''.
236 237 238 |
# File 'utils/shell.rb', line 236 def rc_quote(str) "'#{str.gsub("'", "''")}'" 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.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'utils/shell.rb', line 148 def set_variable_in_profile(variable, value) case preferred when :bash, :ksh, :mksh, :sh, :zsh, nil "echo #{sh_single_quote("export #{variable}=#{sh_quote(value)}")} >> #{profile}" when :pwsh "#{pwsh_quote("$env:#{variable} = #{pwsh_quote(value)}")} >> #{profile}" when :rc "echo #{rc_quote("#{variable}=(#{rc_quote(value)})")} >> #{profile}" when :csh, :tcsh "echo #{sh_single_quote("setenv #{variable} #{csh_quote(value)}")} >> #{profile}" when :fish "echo #{fish_quote("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.
241 242 243 244 245 246 247 248 249 250 251 |
# File 'utils/shell.rb', line 241 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 |
.sh_single_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.
A single-quoted string ends at the first ', so an embedded one has to
close the string, escape the quote and reopen it. Nothing else is special
inside single quotes, so one pass is enough.
215 216 217 |
# File 'utils/shell.rb', line 215 def sh_single_quote(str) "'#{str.gsub("'", "'\\\\''")}'" 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.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'utils/shell.rb', line 254 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 |