From 9a29a306cfd6b116a0cb696ce56bd7bc7679a8e3 Mon Sep 17 00:00:00 2001 From: Greg Nisbet Date: Wed, 10 Aug 2016 23:19:09 -0700 Subject: resolve conflict in diagnostic.rb --- Library/Homebrew/utils/shell.rb | 73 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Library/Homebrew/utils/shell.rb (limited to 'Library/Homebrew/utils/shell.rb') 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 -- cgit v1.2.3 From f0cc815d86aceec61adca10606124ad840b0398a Mon Sep 17 00:00:00 2001 From: Greg Nisbet Date: Sun, 22 May 2016 16:03:51 -0700 Subject: Multi-shell diagnostic check --- Library/Homebrew/utils/shell.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'Library/Homebrew/utils/shell.rb') diff --git a/Library/Homebrew/utils/shell.rb b/Library/Homebrew/utils/shell.rb index ca6eace8b..d2301345f 100644 --- a/Library/Homebrew/utils/shell.rb +++ b/Library/Homebrew/utils/shell.rb @@ -69,5 +69,16 @@ module Utils def self.shell_profile SHELL_PROFILE_MAP.fetch(preferred_shell, "~/.bash_profile") end + + def self.prepend_path_in_shell_profile(path) + case preferred_shell + when :bash, :ksh, :sh, :zsh + "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 $fish_user_paths >> #{sh_quote(path)}' >> #{shell_profile}" + end + end end end -- cgit v1.2.3 From bf63c08d50acb5fa79413325029e67e2c28a6023 Mon Sep 17 00:00:00 2001 From: Greg Nisbet Date: Sun, 22 May 2016 18:02:39 -0700 Subject: tests for shell-specific diagnostic message --- Library/Homebrew/utils/shell.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'Library/Homebrew/utils/shell.rb') diff --git a/Library/Homebrew/utils/shell.rb b/Library/Homebrew/utils/shell.rb index d2301345f..19e696795 100644 --- a/Library/Homebrew/utils/shell.rb +++ b/Library/Homebrew/utils/shell.rb @@ -10,8 +10,10 @@ module Utils }.freeze module Shell + UNSAFE_SHELL_CHAR = /([^A-Za-z0-9_\-.,:\/@\n])/ + # take a path and heuristically convert it - # to a shell, return nil if there's no match + # to a shell name, return nil if there's no match def self.path_to_shell(path) # we only care about the basename shell_name = File.basename(path) @@ -34,7 +36,8 @@ module Utils 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!(UNSAFE_SHELL_CHAR, "\\\\" + "\\1") + # newlines have to be specially quoted in csh str.gsub!(/\n/, "'\\\n'") str end @@ -45,7 +48,7 @@ module Utils 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!(UNSAFE_SHELL_CHAR, "\\\\" + "\\1") str.gsub!(/\n/, "'\n'") str end @@ -61,7 +64,7 @@ module Utils # and a literal \ can be included via \\ "set -gx #{key} \"#{sh_quote(value)}\"" when :csh, :tcsh - "setenv #{key} #{csh_quote(value)}" + "setenv #{key} #{csh_quote(value)};" end end @@ -72,12 +75,12 @@ module Utils def self.prepend_path_in_shell_profile(path) case preferred_shell - when :bash, :ksh, :sh, :zsh - "echo 'export PATH=\"#{sh_quote(path)}:$PATH >> #{shell_profile}" + 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 $fish_user_paths >> #{sh_quote(path)}' >> #{shell_profile}" + "echo 'set -g fish_user_paths \"#{sh_quote(path)}\" $fish_user_paths' >> #{shell_profile}" end end end -- cgit v1.2.3 From dcc3377aa30b34dfcced0df77696674f91a5a9f3 Mon Sep 17 00:00:00 2001 From: Greg Nisbet Date: Tue, 14 Jun 2016 22:31:48 -0700 Subject: move shell_profile to compat/utils.rb & deprecate --- Library/Homebrew/utils/shell.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Library/Homebrew/utils/shell.rb') diff --git a/Library/Homebrew/utils/shell.rb b/Library/Homebrew/utils/shell.rb index 19e696795..c0d6b90ae 100644 --- a/Library/Homebrew/utils/shell.rb +++ b/Library/Homebrew/utils/shell.rb @@ -76,7 +76,7 @@ module Utils def self.prepend_path_in_shell_profile(path) case preferred_shell when :bash, :ksh, :sh, :zsh, nil - "echo 'export PATH=\"#{sh_quote(path)}:$PATH'\" >> #{shell_profile}" + "echo 'export PATH=\"#{sh_quote(path)}:$PATH\"' >> #{shell_profile}" when :csh, :tcsh "echo 'setenv PATH #{csh_quote(path)}:$PATH' >> #{shell_profile}" when :fish -- cgit v1.2.3