2024-08-12 10:30:59 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								# typed: true # rubocop:todo Sorbet/StrictSigil
							 | 
						
					
						
							
								
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								# frozen_string_literal: true
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-10 23:19:09 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								module Utils
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  module Shell
							 | 
						
					
						
							
								
									
										
										
										
											2024-08-20 19:10:14 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    extend T::Helpers
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    requires_ancestor { Kernel }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-22 16:28:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    module_function
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-22 18:02:39 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-10 15:31:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    # Take a path and heuristically convert it to a shell name,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    # return `nil` if there's no match.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    sig { params(path: String).returns(T.nilable(Symbol)) }
							 | 
						
					
						
							
								
									
										
										
										
											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, "")
							 | 
						
					
						
							
								
									
										
										
										
											2023-11-29 02:22:49 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      shell_name.to_sym if %w[bash csh fish ksh mksh rc sh tcsh zsh].include?(shell_name)
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-10 23:19:09 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2023-02-28 15:02:06 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    sig { params(default: String).returns(String) }
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    def preferred_path(default: "")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      ENV.fetch("SHELL", default)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-10 15:31:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    sig { returns(T.nilable(Symbol)) }
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-22 16:28:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def preferred
							 | 
						
					
						
							
								
									
										
										
										
											2023-02-28 15:02:06 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      from_path(preferred_path)
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-10 23:19:09 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-10 15:31:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    sig { returns(T.nilable(Symbol)) }
							 | 
						
					
						
							
								
									
										
										
										
											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
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-10 15:31:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    # Quote values. Quoting keys is overkill.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    sig { params(key: String, value: String, shell: T.nilable(Symbol)).returns(T.nilable(String)) }
							 | 
						
					
						
							
								
									
										
										
										
											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)}\""
							 | 
						
					
						
							
								
									
										
										
										
											2023-11-29 02:22:49 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      when :rc
							 | 
						
					
						
							
								
									
										
										
										
											2023-11-29 21:40:51 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        "#{key}=(#{sh_quote(value)})"
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-10 23:19:09 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      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
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-10 15:31:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    # Return the shell profile file based on user's preferred shell.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    sig { returns(String) }
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-22 16:28:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def profile
							 | 
						
					
						
							
								
									
										
										
										
											2020-05-11 21:26:45 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      case preferred
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      when :bash
							 | 
						
					
						
							
								
									
										
										
										
											2022-05-30 04:48:54 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        bash_profile = "#{Dir.home}/.bash_profile"
							 | 
						
					
						
							
								
									
										
										
										
											2020-05-11 21:26:45 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return bash_profile if File.exist? bash_profile
							 | 
						
					
						
							
								
									
										
										
										
											2023-11-29 02:22:49 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      when :rc
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        rc_profile = "#{Dir.home}/.rcrc"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        return rc_profile if File.exist? rc_profile
							 | 
						
					
						
							
								
									
										
										
										
											2020-05-11 21:26:45 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      when :zsh
							 | 
						
					
						
							
								
									
										
										
										
											2024-06-12 16:24:45 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return "#{ENV["HOMEBREW_ZDOTDIR"]}/.zshrc" if ENV["HOMEBREW_ZDOTDIR"].present?
							 | 
						
					
						
							
								
									
										
										
										
											2020-05-11 21:26:45 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      end
							 | 
						
					
						
							
								
									
										
										
										
											2019-10-08 12:10:31 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-05-11 21:26:45 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      SHELL_PROFILE_MAP.fetch(preferred, "~/.profile")
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-10 23:19:09 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    end
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-22 16:03:51 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-10 15:31:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    sig { params(variable: String, value: String).returns(T.nilable(String)) }
							 | 
						
					
						
							
								
									
										
										
										
											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}"
							 | 
						
					
						
							
								
									
										
										
										
											2023-11-29 02:22:49 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      when :rc
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        "echo '#{variable}=(#{sh_quote(value)})' >> #{profile}"
							 | 
						
					
						
							
								
									
										
										
										
											2018-05-12 11:47:12 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      when :csh, :tcsh
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        "echo 'setenv #{variable} #{csh_quote(value)}' >> #{profile}"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      when :fish
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        "echo 'set -gx #{variable} #{sh_quote(value)}' >> #{profile}"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								      end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-10 15:31:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    sig { params(path: String).returns(T.nilable(String)) }
							 | 
						
					
						
							
								
									
										
										
										
											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}"
							 | 
						
					
						
							
								
									
										
										
										
											2023-11-29 02:22:49 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      when :rc
							 | 
						
					
						
							
								
									
										
										
										
											2023-11-29 21:40:51 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        "echo '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
							 | 
						
					
						
							
								
									
										
										
										
											2022-02-05 16:29:09 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        "fish_add_path #{sh_quote(path)}"
							 | 
						
					
						
							
								
									
										
										
										
											2016-05-22 16:03:51 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    end
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-22 16:28:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    SHELL_PROFILE_MAP = {
							 | 
						
					
						
							
								
									
										
										
										
											2020-05-11 21:26:45 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      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",
							 | 
						
					
						
							
								
									
										
										
										
											2023-11-29 02:22:49 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      rc:   "~/.rcrc",
							 | 
						
					
						
							
								
									
										
										
										
											2020-05-11 21:26:45 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      sh:   "~/.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
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2024-05-18 16:01:07 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    UNSAFE_SHELL_CHAR = %r{([^A-Za-z0-9_\-.,:/@~+\n])}
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-22 16:28:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-10 15:31:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    sig { params(str: String).returns(String) }
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-22 16:28:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def csh_quote(str)
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-30 11:10:23 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      # Ruby's implementation of `shell_escape`.
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-22 16:28:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      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
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-30 11:10:23 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      # Anything that isn't a known safe character is padded.
							 | 
						
					
						
							
								
									
										
										
										
											2020-08-19 17:12:32 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      str.gsub!(UNSAFE_SHELL_CHAR, "\\\\" + "\\1") # rubocop:disable Style/StringConcatenation
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-30 11:10:23 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      # Newlines have to be specially quoted in `csh`.
							 | 
						
					
						
							
								
									
										
										
										
											2023-12-14 02:52:30 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      str.gsub!("\n", "'\\\n'")
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-22 16:28:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      str
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2020-10-10 15:31:32 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    sig { params(str: String).returns(String) }
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-22 16:28:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    def sh_quote(str)
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-30 11:10:23 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      # Ruby's implementation of `shell_escape`.
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-22 16:28:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      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
							 | 
						
					
						
							
								
									
										
										
										
											2024-04-30 11:10:23 +02:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      # Anything that isn't a known safe character is padded.
							 | 
						
					
						
							
								
									
										
										
										
											2020-08-19 17:12:32 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      str.gsub!(UNSAFE_SHELL_CHAR, "\\\\" + "\\1") # rubocop:disable Style/StringConcatenation
							 | 
						
					
						
							
								
									
										
										
										
											2023-12-14 02:52:30 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      str.gsub!("\n", "'\n'")
							 | 
						
					
						
							
								
									
										
										
										
											2017-04-22 16:28:07 +01:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								      str
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    end
							 | 
						
					
						
							
								
									
										
										
										
											2016-08-10 23:19:09 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								end
							 |