Module: Utils::Shell
- Included in:
- Formula
- Defined in:
- utils/shell.rb
Constant Summary collapse
- SHELL_PROFILE_MAP =
{ bash: "~/.profile", csh: "~/.cshrc", fish: "~/.config/fish/config.fish", ksh: "~/.kshrc", mksh: "~/.kshrc", sh: "~/.profile", tcsh: "~/.tcshrc", zsh: "~/.zshrc", }.freeze
- UNSAFE_SHELL_CHAR =
%r{([^A-Za-z0-9_\-.,:/@~\n])}.freeze
Class Method Summary collapse
-
.csh_quote(str) ⇒ String
-
.export_value(key, value, shell = preferred) ⇒ String?
Quote values.
-
.from_path(path) ⇒ Symbol?
Take a path and heuristically convert it to a shell name, return
nil
if there’s no match. -
.parent ⇒ Symbol?
-
.preferred ⇒ Symbol?
-
.preferred_path(default: "") ⇒ String
-
.prepend_path_in_profile(path) ⇒ String?
-
.profile ⇒ String
Return the shell profile file based on user’s preferred shell.
-
.set_variable_in_profile(variable, value) ⇒ String?
-
.sh_quote(str) ⇒ String
Class Method Details
.csh_quote(str) ⇒ String
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'utils/shell.rb', line 102 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") # rubocop:disable Style/StringConcatenation # newlines have to be specially quoted in csh str.gsub!(/\n/, "'\\\n'") str end |
.export_value(key, value, shell = preferred) ⇒ String?
Quote values. Quoting keys is overkill.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'utils/shell.rb', line 36 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 :csh, :tcsh "setenv #{key} #{csh_quote(value)};" end end |
.from_path(path) ⇒ Symbol?
Take a path and heuristically convert it to a shell name,
return nil
if there’s no match.
11 12 13 14 15 16 17 |
# File 'utils/shell.rb', line 11 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 sh tcsh zsh].include?(shell_name) end |
.parent ⇒ Symbol?
30 31 32 |
# File 'utils/shell.rb', line 30 def parent from_path(`ps -p #{Process.ppid} -o ucomm=`.strip) end |
.preferred ⇒ Symbol?
25 26 27 |
# File 'utils/shell.rb', line 25 def preferred from_path(preferred_path) end |
.preferred_path(default: "") ⇒ String
20 21 22 |
# File 'utils/shell.rb', line 20 def preferred_path(default: "") ENV.fetch("SHELL", default) end |
.prepend_path_in_profile(path) ⇒ String?
77 78 79 80 81 82 83 84 85 86 |
# File 'utils/shell.rb', line 77 def prepend_path_in_profile(path) case preferred when :bash, :ksh, :mksh, :sh, :zsh, nil "echo 'export 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
Return the shell profile file based on user’s preferred shell.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'utils/shell.rb', line 52 def profile case preferred when :bash bash_profile = "#{Dir.home}/.bash_profile" return bash_profile if File.exist? bash_profile when :zsh return "#{ENV["ZDOTDIR"]}/.zshrc" if ENV["ZDOTDIR"].present? end SHELL_PROFILE_MAP.fetch(preferred, "~/.profile") end |
.set_variable_in_profile(variable, value) ⇒ String?
65 66 67 68 69 70 71 72 73 74 |
# File 'utils/shell.rb', line 65 def set_variable_in_profile(variable, value) case preferred when :bash, :ksh, :sh, :zsh, nil "echo 'export #{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
116 117 118 119 120 121 122 123 124 125 126 |
# File 'utils/shell.rb', line 116 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") # rubocop:disable Style/StringConcatenation str.gsub!(/\n/, "'\n'") str end |