Support the rc shell. Fixes #16264

(In my tests, changes to `shellenv.sh` weren't required, so I didn't bother.)
This commit is contained in:
Cthulhux 2023-11-29 02:22:49 +01:00 committed by GitHub
parent 0ed0d9bafc
commit d26a408870
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,7 +13,7 @@ module Utils
shell_name = File.basename(path) shell_name = File.basename(path)
# handle possible version suffix like `zsh-5.2` # handle possible version suffix like `zsh-5.2`
shell_name.sub!(/-.*\z/m, "") shell_name.sub!(/-.*\z/m, "")
shell_name.to_sym if %w[bash csh fish ksh mksh sh tcsh zsh].include?(shell_name) shell_name.to_sym if %w[bash csh fish ksh mksh rc sh tcsh zsh].include?(shell_name)
end end
sig { params(default: String).returns(String) } sig { params(default: String).returns(String) }
@ -42,6 +42,8 @@ module Utils
# a single quote can be included in a single-quoted string via \' # a single quote can be included in a single-quoted string via \'
# and a literal \ can be included via \\ # and a literal \ can be included via \\
"set -gx #{key} \"#{sh_quote(value)}\"" "set -gx #{key} \"#{sh_quote(value)}\""
when :rc
"#{key}=(#{rc_quote(value)})"
when :csh, :tcsh when :csh, :tcsh
"setenv #{key} #{csh_quote(value)};" "setenv #{key} #{csh_quote(value)};"
end end
@ -54,6 +56,9 @@ module Utils
when :bash when :bash
bash_profile = "#{Dir.home}/.bash_profile" bash_profile = "#{Dir.home}/.bash_profile"
return bash_profile if File.exist? bash_profile return bash_profile if File.exist? bash_profile
when :rc
rc_profile = "#{Dir.home}/.rcrc"
return rc_profile if File.exist? rc_profile
when :zsh when :zsh
return "#{ENV["ZDOTDIR"]}/.zshrc" if ENV["ZDOTDIR"].present? return "#{ENV["ZDOTDIR"]}/.zshrc" if ENV["ZDOTDIR"].present?
end end
@ -66,6 +71,8 @@ module Utils
case preferred case preferred
when :bash, :ksh, :sh, :zsh, nil when :bash, :ksh, :sh, :zsh, nil
"echo 'export #{variable}=#{sh_quote(value)}' >> #{profile}" "echo 'export #{variable}=#{sh_quote(value)}' >> #{profile}"
when :rc
"echo '#{variable}=(#{sh_quote(value)})' >> #{profile}"
when :csh, :tcsh when :csh, :tcsh
"echo 'setenv #{variable} #{csh_quote(value)}' >> #{profile}" "echo 'setenv #{variable} #{csh_quote(value)}' >> #{profile}"
when :fish when :fish
@ -78,6 +85,8 @@ module Utils
case preferred case preferred
when :bash, :ksh, :mksh, :sh, :zsh, nil when :bash, :ksh, :mksh, :sh, :zsh, nil
"echo 'export PATH=\"#{sh_quote(path)}:$PATH\"' >> #{profile}" "echo 'export PATH=\"#{sh_quote(path)}:$PATH\"' >> #{profile}"
when :rc
"echo 'path=(#{rc_quote(path)} $path)' >> #{profile}"
when :csh, :tcsh when :csh, :tcsh
"echo 'setenv PATH #{csh_quote(path)}:$PATH' >> #{profile}" "echo 'setenv PATH #{csh_quote(path)}:$PATH' >> #{profile}"
when :fish when :fish
@ -91,6 +100,7 @@ module Utils
fish: "~/.config/fish/config.fish", fish: "~/.config/fish/config.fish",
ksh: "~/.kshrc", ksh: "~/.kshrc",
mksh: "~/.kshrc", mksh: "~/.kshrc",
rc: "~/.rcrc",
sh: "~/.profile", sh: "~/.profile",
tcsh: "~/.tcshrc", tcsh: "~/.tcshrc",
zsh: "~/.zshrc", zsh: "~/.zshrc",
@ -124,5 +134,19 @@ module Utils
str.gsub!(/\n/, "'\n'") str.gsub!(/\n/, "'\n'")
str str
end end
sig { params(str: String).returns(String) }
def rc_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!(/'/, "''")
str.gsub!(/\n/, "'\n'")
str
end
end end
end end