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], )
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.
-
.profile_redirect_target ⇒ String
private
Return Shell.profile quoted for redirection in the user's shell.
-
.pwsh_quote(str) ⇒ String
private
PowerShell and rc single-quoted strings escape
'as''. - .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.
206 207 208 209 |
# File 'utils/shell.rb', line 206 def csh_quote(str) # Newlines have to be specially quoted in `csh`. sh_quote(str).gsub("'\n'", "'\\\n'") 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.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'utils/shell.rb', line 105 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}=(#{pwsh_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.
82 83 84 85 86 |
# File 'utils/shell.rb', line 82 def from_path(path) # handle possible version suffix like `zsh-5.2` shell = File.basename(path).sub(/-.*\z/m, "").to_sym shell if SHELL_PROFILE_MAP.key?(shell) 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.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'utils/shell.rb', line 58 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 Process.wait Process.spawn( { "TERM" => ENV.fetch("HOMEBREW_TERM", ENV.fetch("TERM", nil)) }, 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 |
.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.
99 100 101 |
# File 'utils/shell.rb', line 99 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.
94 95 96 |
# File 'utils/shell.rb', line 94 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.
89 90 91 |
# File 'utils/shell.rb', line 89 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.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'utils/shell.rb', line 174 def prepend_path_in_profile(path) case preferred when :bash, :ksh, :mksh, :sh, :zsh, nil "printf '%s\\n' #{sh_quote("export PATH=#{sh_quote(path)}:$PATH")} >> #{profile_redirect_target}" when :pwsh "#{pwsh_quote("$env:PATH = #{pwsh_quote(path)} + \":$env:PATH\"")} >> #{profile_redirect_target}" when :rc "echo #{pwsh_quote("path=(#{pwsh_quote(path)} $path)")} >> #{profile_redirect_target}" when :csh, :tcsh "printf '%s\\n' #{csh_quote("setenv PATH #{csh_quote(path)}:$PATH")} >> #{profile_redirect_target}" 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.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'utils/shell.rb', line 125 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_PROFILE_MAP.fetch(preferred || :sh, "~/.profile") end |
.profile_redirect_target ⇒ 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 profile quoted for redirection in the user's shell.
145 146 147 148 149 150 151 152 153 154 155 |
# File 'utils/shell.rb', line 145 def profile_redirect_target target = profile case preferred when :rc, :pwsh target.start_with?("~/") ? target : pwsh_quote(target) when :csh, :tcsh csh_quote(target) else sh_quote(target) end 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 and rc single-quoted strings escape ' as ''.
213 214 215 |
# File 'utils/shell.rb', line 213 def pwsh_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.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'utils/shell.rb', line 158 def set_variable_in_profile(variable, value) case preferred when :bash, :ksh, :mksh, :sh, :zsh, nil "printf '%s\\n' #{sh_quote("export #{variable}=#{sh_quote(value)}")} >> #{profile_redirect_target}" when :pwsh "#{pwsh_quote("$env:#{variable} = #{pwsh_quote(value)}")} >> #{profile_redirect_target}" when :rc "echo #{pwsh_quote("#{variable}=(#{pwsh_quote(value)})")} >> #{profile_redirect_target}" when :csh, :tcsh "printf '%s\\n' #{csh_quote("setenv #{variable} #{csh_quote(value)}")} >> #{profile_redirect_target}" when :fish "printf '%s\\n' #{sh_quote("set -gx #{variable} #{sh_quote(value)}")} >> #{profile_redirect_target}" 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.
218 219 220 221 |
# File 'utils/shell.rb', line 218 def sh_quote(str) # Allow tilde expansion in paths such as ~/tmp. Shellwords.escape(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.
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 224 def shell_with_prompt(type, preferred_path:, notice:, home: Dir.home) path = ENV.fetch("PATH") subshell = case from_path(preferred_path) 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.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'utils/shell.rb', line 18 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.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'utils/shell.rb', line 37 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.
53 54 55 |
# File 'utils/shell.rb', line 53 def with_homebrew_path(&block) with_env(PATH: PATH.new(ORIGINAL_PATHS).to_s, &block) end |