2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-10 23:19:09 -07:00
|
|
|
module Utils
|
|
|
|
module Shell
|
2017-04-22 16:28:07 +01:00
|
|
|
module_function
|
2016-05-22 18:02:39 -07:00
|
|
|
|
2016-08-10 23:19:09 -07:00
|
|
|
# take a path and heuristically convert it
|
2016-05-22 18:02:39 -07:00
|
|
|
# to a shell name, return nil if there's no match
|
2017-04-22 16:28:07 +01:00
|
|
|
def from_path(path)
|
2016-08-10 23:19:09 -07:00
|
|
|
# we only care about the basename
|
|
|
|
shell_name = File.basename(path)
|
|
|
|
# handle possible version suffix like `zsh-5.2`
|
|
|
|
shell_name.sub!(/-.*\z/m, "")
|
2019-05-12 20:45:09 +02:00
|
|
|
shell_name.to_sym if %w[bash csh fish ksh mksh sh tcsh zsh].include?(shell_name)
|
2016-08-10 23:19:09 -07:00
|
|
|
end
|
|
|
|
|
2017-04-22 16:28:07 +01:00
|
|
|
def preferred
|
|
|
|
from_path(ENV.fetch("SHELL", ""))
|
2016-08-10 23:19:09 -07:00
|
|
|
end
|
|
|
|
|
2017-04-22 16:28:07 +01:00
|
|
|
def parent
|
|
|
|
from_path(`ps -p #{Process.ppid} -o ucomm=`.strip)
|
2016-08-10 23:19:09 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
# quote values. quoting keys is overkill
|
2018-07-26 10:48:25 +01:00
|
|
|
def export_value(key, value, shell = preferred)
|
2016-08-10 23:19:09 -07:00
|
|
|
case shell
|
2019-05-12 20:45:09 +02:00
|
|
|
when :bash, :ksh, :mksh, :sh, :zsh
|
2016-08-10 23:19:09 -07:00
|
|
|
"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
|
2016-05-22 18:02:39 -07:00
|
|
|
"setenv #{key} #{csh_quote(value)};"
|
2016-08-10 23:19:09 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-18 21:42:43 -04:00
|
|
|
# return the shell profile file based on user's preferred shell
|
2017-04-22 16:28:07 +01:00
|
|
|
def profile
|
2019-10-07 10:25:41 -04:00
|
|
|
return "#{ENV["ZDOTDIR"]}/.zshrc" if preferred == "zsh" && ENV["ZDOTDIR"].present?
|
|
|
|
|
2017-04-22 16:28:07 +01:00
|
|
|
SHELL_PROFILE_MAP.fetch(preferred, "~/.bash_profile")
|
2016-08-10 23:19:09 -07:00
|
|
|
end
|
2016-05-22 16:03:51 -07:00
|
|
|
|
2018-05-12 11:47:12 -05:00
|
|
|
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
|
|
|
|
|
2017-04-22 16:28:07 +01:00
|
|
|
def prepend_path_in_profile(path)
|
|
|
|
case preferred
|
2019-05-12 20:45:09 +02:00
|
|
|
when :bash, :ksh, :mksh, :sh, :zsh, nil
|
2017-04-22 16:28:07 +01:00
|
|
|
"echo 'export PATH=\"#{sh_quote(path)}:$PATH\"' >> #{profile}"
|
2016-05-22 16:03:51 -07:00
|
|
|
when :csh, :tcsh
|
2017-04-22 16:28:07 +01:00
|
|
|
"echo 'setenv PATH #{csh_quote(path)}:$PATH' >> #{profile}"
|
2016-05-22 16:03:51 -07:00
|
|
|
when :fish
|
2017-04-22 16:28:07 +01:00
|
|
|
"echo 'set -g fish_user_paths \"#{sh_quote(path)}\" $fish_user_paths' >> #{profile}"
|
2016-05-22 16:03:51 -07:00
|
|
|
end
|
|
|
|
end
|
2017-04-22 16:28:07 +01:00
|
|
|
|
|
|
|
SHELL_PROFILE_MAP = {
|
|
|
|
bash: "~/.bash_profile",
|
2018-11-02 17:18:07 +00:00
|
|
|
csh: "~/.cshrc",
|
2017-04-22 16:28:07 +01:00
|
|
|
fish: "~/.config/fish/config.fish",
|
2018-11-02 17:18:07 +00:00
|
|
|
ksh: "~/.kshrc",
|
2019-05-12 20:45:09 +02:00
|
|
|
mksh: "~/.kshrc",
|
2018-11-02 17:18:07 +00:00
|
|
|
sh: "~/.bash_profile",
|
2017-04-22 16:28:07 +01:00
|
|
|
tcsh: "~/.tcshrc",
|
2018-11-02 17:18:07 +00:00
|
|
|
zsh: "~/.zshrc",
|
2017-04-22 16:28:07 +01:00
|
|
|
}.freeze
|
|
|
|
|
2018-11-02 17:18:07 +00:00
|
|
|
UNSAFE_SHELL_CHAR = %r{([^A-Za-z0-9_\-.,:/@~\n])}.freeze
|
2017-04-22 16:28:07 +01:00
|
|
|
|
|
|
|
def csh_quote(str)
|
|
|
|
# ruby's implementation of shell_escape
|
|
|
|
str = str.to_s
|
|
|
|
return "''" if str.empty?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-04-22 16:28:07 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
def sh_quote(str)
|
|
|
|
# ruby's implementation of shell_escape
|
|
|
|
str = str.to_s
|
|
|
|
return "''" if str.empty?
|
2018-09-17 02:45:00 +02:00
|
|
|
|
2017-04-22 16:28:07 +01:00
|
|
|
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
|
2016-08-10 23:19:09 -07:00
|
|
|
end
|
|
|
|
end
|