diff --git a/Library/Homebrew/test/language/perl/shebang_spec.rb b/Library/Homebrew/test/language/perl/shebang_spec.rb index 4d6a1fd579..89b9b7dc56 100644 --- a/Library/Homebrew/test/language/perl/shebang_spec.rb +++ b/Library/Homebrew/test/language/perl/shebang_spec.rb @@ -35,7 +35,7 @@ describe Language::Perl::Shebang do it "can be used to replace Perl shebangs" do allow(Formulary).to receive(:factory) allow(Formulary).to receive(:factory).with(perl_f.name).and_return(perl_f) - Utils::Shebang.rewrite_shebang described_class.detected_perl_shebang(f), file + Utils::Shebang.rewrite_shebang described_class.detected_perl_shebang(f), file.path expected_shebang = if OS.mac? "/usr/bin/perl#{MacOS.preferred_perl_version}" diff --git a/Library/Homebrew/test/language/python/shebang_spec.rb b/Library/Homebrew/test/language/python/shebang_spec.rb index 9d191f1b92..0f729a70fa 100644 --- a/Library/Homebrew/test/language/python/shebang_spec.rb +++ b/Library/Homebrew/test/language/python/shebang_spec.rb @@ -35,7 +35,9 @@ describe Language::Python::Shebang do it "can be used to replace Python shebangs" do allow(Formulary).to receive(:factory) allow(Formulary).to receive(:factory).with(python_f.name).and_return(python_f) - Utils::Shebang.rewrite_shebang described_class.detected_python_shebang(f, use_python_from_path: false), file + Utils::Shebang.rewrite_shebang( + described_class.detected_python_shebang(f, use_python_from_path: false), file.path + ) expect(File.read(file)).to eq <<~EOS #!#{HOMEBREW_PREFIX}/opt/python@3.11/bin/python3.11 @@ -46,7 +48,9 @@ describe Language::Python::Shebang do end it "can be pointed to a `python3` in PATH" do - Utils::Shebang.rewrite_shebang described_class.detected_python_shebang(f, use_python_from_path: true), file + Utils::Shebang.rewrite_shebang( + described_class.detected_python_shebang(f, use_python_from_path: true), file.path + ) expect(File.read(file)).to eq <<~EOS #!/usr/bin/env python3 diff --git a/Library/Homebrew/utils/inreplace.rb b/Library/Homebrew/utils/inreplace.rb index 354b11c8f7..50ff78f80b 100644 --- a/Library/Homebrew/utils/inreplace.rb +++ b/Library/Homebrew/utils/inreplace.rb @@ -20,6 +20,8 @@ module Utils end end + module_function + # Sometimes we have to change a bit before we install. Mostly we # prefer a patch, but if you need the {Formula#prefix prefix} of # this formula in the patch you have to resort to `inreplace`, @@ -45,7 +47,7 @@ module Utils audit_result: T::Boolean, ).void } - def self.inreplace(paths, before = nil, after = nil, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter + def inreplace(paths, before = nil, after = nil, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter after = after.to_s if after.is_a? Symbol errors = {} @@ -71,7 +73,7 @@ module Utils end # @api private - def self.inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false) + def inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false) str = File.binread(path) contents = StringInreplaceExtension.new(str) replacement_pairs.each do |old, new| diff --git a/Library/Homebrew/utils/inreplace.rbi b/Library/Homebrew/utils/inreplace.rbi new file mode 100644 index 0000000000..02542091d2 --- /dev/null +++ b/Library/Homebrew/utils/inreplace.rbi @@ -0,0 +1,7 @@ +# typed: strict + +module Utils + module Inreplace + include Kernel + end +end diff --git a/Library/Homebrew/utils/shebang.rb b/Library/Homebrew/utils/shebang.rb index 22cf7f2c62..7ce5a56284 100644 --- a/Library/Homebrew/utils/shebang.rb +++ b/Library/Homebrew/utils/shebang.rb @@ -8,6 +8,8 @@ module Utils module Shebang extend T::Sig + module_function + # Specification on how to rewrite a given shebang. # # @api private @@ -30,8 +32,8 @@ module Utils # rewrite_shebang detected_python_shebang, bin/"script.py" # # @api public - sig { params(rewrite_info: RewriteInfo, paths: T::Array[T.any(String, Pathname)]).void } - def self.rewrite_shebang(rewrite_info, *paths) + sig { params(rewrite_info: RewriteInfo, paths: T.any(String, Pathname)).void } + def rewrite_shebang(rewrite_info, *paths) paths.each do |f| f = Pathname(f) next unless f.file? diff --git a/Library/Homebrew/utils/shebang.rbi b/Library/Homebrew/utils/shebang.rbi new file mode 100644 index 0000000000..9cdb0b2007 --- /dev/null +++ b/Library/Homebrew/utils/shebang.rbi @@ -0,0 +1,7 @@ +# typed: strict + +module Utils + module Shebang + include Kernel + end +end diff --git a/Library/Homebrew/utils/shell.rb b/Library/Homebrew/utils/shell.rb index c27dac0562..4fe444be02 100644 --- a/Library/Homebrew/utils/shell.rb +++ b/Library/Homebrew/utils/shell.rb @@ -5,10 +5,12 @@ module Utils module Shell extend T::Sig + module_function + # 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)) } - def self.from_path(path) + def from_path(path) # we only care about the basename shell_name = File.basename(path) # handle possible version suffix like `zsh-5.2` @@ -17,23 +19,23 @@ module Utils end sig { params(default: String).returns(String) } - def self.preferred_path(default: "") + def preferred_path(default: "") ENV.fetch("SHELL", default) end sig { returns(T.nilable(Symbol)) } - def self.preferred + def preferred from_path(preferred_path) end sig { returns(T.nilable(Symbol)) } - def self.parent + def parent from_path(`ps -p #{Process.ppid} -o ucomm=`.strip) end # Quote values. Quoting keys is overkill. sig { params(key: String, value: String, shell: T.nilable(Symbol)).returns(T.nilable(String)) } - def self.export_value(key, value, shell = preferred) + def export_value(key, value, shell = preferred) case shell when :bash, :ksh, :mksh, :sh, :zsh "export #{key}=\"#{sh_quote(value)}\"" @@ -49,7 +51,7 @@ module Utils # Return the shell profile file based on user's preferred shell. sig { returns(String) } - def self.profile + def profile case preferred when :bash bash_profile = "#{Dir.home}/.bash_profile" @@ -62,7 +64,7 @@ module Utils end sig { params(variable: String, value: String).returns(T.nilable(String)) } - def self.set_variable_in_profile(variable, value) + def set_variable_in_profile(variable, value) case preferred when :bash, :ksh, :sh, :zsh, nil "echo 'export #{variable}=#{sh_quote(value)}' >> #{profile}" @@ -74,7 +76,7 @@ module Utils end sig { params(path: String).returns(T.nilable(String)) } - def self.prepend_path_in_profile(path) + def prepend_path_in_profile(path) case preferred when :bash, :ksh, :mksh, :sh, :zsh, nil "echo 'export PATH=\"#{sh_quote(path)}:$PATH\"' >> #{profile}" @@ -99,7 +101,7 @@ module Utils UNSAFE_SHELL_CHAR = %r{([^A-Za-z0-9_\-.,:/@~\n])}.freeze sig { params(str: String).returns(String) } - def self.csh_quote(str) + def csh_quote(str) # ruby's implementation of shell_escape str = str.to_s return "''" if str.empty? @@ -113,7 +115,7 @@ module Utils end sig { params(str: String).returns(String) } - def self.sh_quote(str) + def sh_quote(str) # ruby's implementation of shell_escape str = str.to_s return "''" if str.empty? diff --git a/Library/Homebrew/utils/shell.rbi b/Library/Homebrew/utils/shell.rbi new file mode 100644 index 0000000000..b860bc1190 --- /dev/null +++ b/Library/Homebrew/utils/shell.rbi @@ -0,0 +1,7 @@ +# typed: strict + +module Utils + module Shell + include Kernel + end +end