Module: Utils::Shell

Included in:
Formula
Defined in:
brew/Library/Homebrew/utils/shell.rb

Constant Summary collapse

SHELL_PROFILE_MAP =
{
  bash: "~/.bash_profile",
  csh:  "~/.cshrc",
  fish: "~/.config/fish/config.fish",
  ksh:  "~/.kshrc",
  mksh: "~/.kshrc",
  sh:   "~/.bash_profile",
  tcsh: "~/.tcshrc",
  zsh:  "~/.zshrc",
}.freeze
UNSAFE_SHELL_CHAR =
%r{([^A-Za-z0-9_\-.,:/@~\n])}.freeze

Class Method Summary collapse

Class Method Details

.csh_quote(str) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'brew/Library/Homebrew/utils/shell.rb', line 80

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) ⇒ Object

quote values. quoting keys is overkill



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'brew/Library/Homebrew/utils/shell.rb', line 26

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) ⇒ Object

take a path and heuristically convert it to a shell name, return nil if there’s no match



9
10
11
12
13
14
15
# File 'brew/Library/Homebrew/utils/shell.rb', line 9

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

.parentObject



21
22
23
# File 'brew/Library/Homebrew/utils/shell.rb', line 21

def parent
  from_path(`ps -p #{Process.ppid} -o ucomm=`.strip)
end

.preferredObject



17
18
19
# File 'brew/Library/Homebrew/utils/shell.rb', line 17

def preferred
  from_path(ENV.fetch("SHELL", ""))
end

.prepend_path_in_profile(path) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'brew/Library/Homebrew/utils/shell.rb', line 56

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
    "echo 'set -g fish_user_paths \"#{sh_quote(path)}\" $fish_user_paths' >> #{profile}"
  end
end

.profileObject

return the shell profile file based on user’s preferred shell



41
42
43
# File 'brew/Library/Homebrew/utils/shell.rb', line 41

def profile
  SHELL_PROFILE_MAP.fetch(preferred, "~/.bash_profile")
end

.set_variable_in_profile(variable, value) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'brew/Library/Homebrew/utils/shell.rb', line 45

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) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'brew/Library/Homebrew/utils/shell.rb', line 93

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