Module: Utils::Shell

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

Constant Summary

UNSAFE_SHELL_CHAR =
%r{([^A-Za-z0-9_\-.,:/@\n])}

Class Method Summary collapse

Class Method Details

.csh_quote(str) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'Library/Homebrew/utils/shell.rb', line 36

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(shell, key, value) ⇒ Object

quote values. quoting keys is overkill



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'Library/Homebrew/utils/shell.rb', line 62

def export_value(shell, key, value)
  case shell
  when :bash, :ksh, :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

.parent_shellObject



31
32
33
# File 'Library/Homebrew/utils/shell.rb', line 31

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

.path_to_shell(path) ⇒ Object

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



17
18
19
20
21
22
23
# File 'Library/Homebrew/utils/shell.rb', line 17

def path_to_shell(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 sh tcsh zsh].include?(shell_name)
end

.preferred_shellObject



26
27
28
# File 'Library/Homebrew/utils/shell.rb', line 26

def preferred_shell
  path_to_shell(ENV.fetch("SHELL", ""))
end

.prepend_path_in_shell_profile(path) ⇒ Object



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

def prepend_path_in_shell_profile(path)
  case preferred_shell
  when :bash, :ksh, :sh, :zsh, nil
    "echo 'export PATH=\"#{sh_quote(path)}:$PATH\"' >> #{shell_profile}"
  when :csh, :tcsh
    "echo 'setenv PATH #{csh_quote(path)}:$PATH' >> #{shell_profile}"
  when :fish
    "echo 'set -g fish_user_paths \"#{sh_quote(path)}\" $fish_user_paths' >> #{shell_profile}"
  end
end

.sh_quote(str) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'Library/Homebrew/utils/shell.rb', line 49

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_profileObject

return the shell profile file based on users' preferred shell



78
79
80
# File 'Library/Homebrew/utils/shell.rb', line 78

def shell_profile
  SHELL_PROFILE_MAP.fetch(preferred_shell, "~/.bash_profile")
end