aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/utils/shell.rb
diff options
context:
space:
mode:
authorGreg Nisbet2016-08-10 23:19:09 -0700
committerGreg Nisbet2016-08-10 23:19:09 -0700
commit9a29a306cfd6b116a0cb696ce56bd7bc7679a8e3 (patch)
tree19eefeaea0baf5c39e2ea0795d2774ae852a8022 /Library/Homebrew/utils/shell.rb
parent06fe347de97975dc01e726f87bf07a56a6fb713e (diff)
downloadbrew-9a29a306cfd6b116a0cb696ce56bd7bc7679a8e3.tar.bz2
resolve conflict in diagnostic.rb
Diffstat (limited to 'Library/Homebrew/utils/shell.rb')
-rw-r--r--Library/Homebrew/utils/shell.rb73
1 files changed, 73 insertions, 0 deletions
diff --git a/Library/Homebrew/utils/shell.rb b/Library/Homebrew/utils/shell.rb
new file mode 100644
index 000000000..ca6eace8b
--- /dev/null
+++ b/Library/Homebrew/utils/shell.rb
@@ -0,0 +1,73 @@
+module Utils
+ SHELL_PROFILE_MAP = {
+ :bash => "~/.bash_profile",
+ :csh => "~/.cshrc",
+ :fish => "~/.config/fish/config.fish",
+ :ksh => "~/.kshrc",
+ :sh => "~/.bash_profile",
+ :tcsh => "~/.tcshrc",
+ :zsh => "~/.zshrc",
+ }.freeze
+
+ module Shell
+ # take a path and heuristically convert it
+ # to a shell, return nil if there's no match
+ def self.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
+
+ def self.preferred_shell
+ path_to_shell(ENV.fetch("SHELL", ""))
+ end
+
+ def self.parent_shell
+ path_to_shell(`ps -p #{Process.ppid} -o ucomm=`.strip)
+ end
+
+ def self.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!(/([^A-Za-z0-9_\-.,:\/@\n])/, "\\\\" + "\\1")
+ str.gsub!(/\n/, "'\\\n'")
+ str
+ end
+
+ def self.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!(/([^A-Za-z0-9_\-.,:\/@\n])/, "\\\\" + "\\1")
+ str.gsub!(/\n/, "'\n'")
+ str
+ end
+
+ # quote values. quoting keys is overkill
+ def self.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
+
+ # return the shell profile file based on users' preferred shell
+ def self.shell_profile
+ SHELL_PROFILE_MAP.fetch(preferred_shell, "~/.bash_profile")
+ end
+ end
+end