Inline type annotations for Utils::Shell.
This commit is contained in:
parent
ff653571b1
commit
8fabc56a22
@ -1,38 +0,0 @@
|
|||||||
# typed: strict
|
|
||||||
|
|
||||||
module Utils::Shell
|
|
||||||
include Kernel
|
|
||||||
|
|
||||||
sig{ params(path: String).returns(T.nilable(Symbol)) }
|
|
||||||
def from_path(path)
|
|
||||||
end
|
|
||||||
|
|
||||||
sig{ returns(T.nilable(Symbol)) }
|
|
||||||
def preferred
|
|
||||||
end
|
|
||||||
|
|
||||||
def parent
|
|
||||||
end
|
|
||||||
|
|
||||||
def export_value(key, value, shell = preferred)
|
|
||||||
end
|
|
||||||
|
|
||||||
sig{ returns(String) }
|
|
||||||
def profile
|
|
||||||
end
|
|
||||||
|
|
||||||
def set_variable_in_profile(variable, value)
|
|
||||||
end
|
|
||||||
|
|
||||||
sig{ params(path: String).returns(T.nilable(String)) }
|
|
||||||
def prepend_path_in_profile(path)
|
|
||||||
end
|
|
||||||
|
|
||||||
sig{ params(str: String).returns(T.nilable(String)) }
|
|
||||||
def csh_quote(str)
|
|
||||||
end
|
|
||||||
|
|
||||||
sig{ params(str: String).returns(T.nilable(String)) }
|
|
||||||
def sh_quote(str)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -3,10 +3,14 @@
|
|||||||
|
|
||||||
module Utils
|
module Utils
|
||||||
module Shell
|
module Shell
|
||||||
|
include Kernel
|
||||||
|
extend T::Sig
|
||||||
|
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
# take a path and heuristically convert it
|
# Take a path and heuristically convert it to a shell name,
|
||||||
# to a shell name, return nil if there's no match
|
# return `nil` if there's no match.
|
||||||
|
sig { params(path: String).returns(T.nilable(Symbol)) }
|
||||||
def from_path(path)
|
def from_path(path)
|
||||||
# we only care about the basename
|
# we only care about the basename
|
||||||
shell_name = File.basename(path)
|
shell_name = File.basename(path)
|
||||||
@ -15,15 +19,18 @@ module Utils
|
|||||||
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 sh tcsh zsh].include?(shell_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T.nilable(Symbol)) }
|
||||||
def preferred
|
def preferred
|
||||||
from_path(ENV.fetch("SHELL", ""))
|
from_path(ENV.fetch("SHELL", ""))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { returns(T.nilable(Symbol)) }
|
||||||
def parent
|
def parent
|
||||||
from_path(`ps -p #{Process.ppid} -o ucomm=`.strip)
|
from_path(`ps -p #{Process.ppid} -o ucomm=`.strip)
|
||||||
end
|
end
|
||||||
|
|
||||||
# quote values. quoting keys is overkill
|
# Quote values. Quoting keys is overkill.
|
||||||
|
sig { params(key: String, value: String, shell: T.nilable(Symbol)).returns(T.nilable(String)) }
|
||||||
def export_value(key, value, shell = preferred)
|
def export_value(key, value, shell = preferred)
|
||||||
case shell
|
case shell
|
||||||
when :bash, :ksh, :mksh, :sh, :zsh
|
when :bash, :ksh, :mksh, :sh, :zsh
|
||||||
@ -38,7 +45,8 @@ module Utils
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# return the shell profile file based on user's preferred shell
|
# Return the shell profile file based on user's preferred shell.
|
||||||
|
sig { returns(String) }
|
||||||
def profile
|
def profile
|
||||||
case preferred
|
case preferred
|
||||||
when :bash
|
when :bash
|
||||||
@ -51,6 +59,7 @@ module Utils
|
|||||||
SHELL_PROFILE_MAP.fetch(preferred, "~/.profile")
|
SHELL_PROFILE_MAP.fetch(preferred, "~/.profile")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { params(variable: String, value: String).returns(T.nilable(String)) }
|
||||||
def set_variable_in_profile(variable, value)
|
def set_variable_in_profile(variable, value)
|
||||||
case preferred
|
case preferred
|
||||||
when :bash, :ksh, :sh, :zsh, nil
|
when :bash, :ksh, :sh, :zsh, nil
|
||||||
@ -62,6 +71,7 @@ module Utils
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { params(path: String).returns(T.nilable(String)) }
|
||||||
def prepend_path_in_profile(path)
|
def prepend_path_in_profile(path)
|
||||||
case preferred
|
case preferred
|
||||||
when :bash, :ksh, :mksh, :sh, :zsh, nil
|
when :bash, :ksh, :mksh, :sh, :zsh, nil
|
||||||
@ -86,6 +96,7 @@ module Utils
|
|||||||
|
|
||||||
UNSAFE_SHELL_CHAR = %r{([^A-Za-z0-9_\-.,:/@~\n])}.freeze
|
UNSAFE_SHELL_CHAR = %r{([^A-Za-z0-9_\-.,:/@~\n])}.freeze
|
||||||
|
|
||||||
|
sig { params(str: String).returns(String) }
|
||||||
def csh_quote(str)
|
def csh_quote(str)
|
||||||
# ruby's implementation of shell_escape
|
# ruby's implementation of shell_escape
|
||||||
str = str.to_s
|
str = str.to_s
|
||||||
@ -99,6 +110,7 @@ module Utils
|
|||||||
str
|
str
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { params(str: String).returns(String) }
|
||||||
def sh_quote(str)
|
def sh_quote(str)
|
||||||
# ruby's implementation of shell_escape
|
# ruby's implementation of shell_escape
|
||||||
str = str.to_s
|
str = str.to_s
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user