diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index e97ebd9d52..00b55f5a00 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -2559,10 +2559,11 @@ class Formula before: T.nilable(T.any(Pathname, Regexp, String)), after: T.nilable(T.any(Pathname, String, Symbol)), audit_result: T::Boolean, + blk: T.nilable(T.proc.params(s: StringInreplaceExtension).void), ).void } - def inreplace(paths, before = nil, after = nil, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter - Utils::Inreplace.inreplace(paths, before, after, audit_result: audit_result) + def inreplace(paths, before = nil, after = nil, audit_result = true, &blk) # rubocop:disable Style/OptionalBooleanParameter + Utils::Inreplace.inreplace(paths, before, after, audit_result: audit_result, &blk) rescue Utils::Inreplace::Error => e onoe e.to_s raise BuildError.new(self, "inreplace", Array(paths), {}) diff --git a/Library/Homebrew/utils/inreplace.rb b/Library/Homebrew/utils/inreplace.rb index 9743c1608a..1cd7da37e9 100644 --- a/Library/Homebrew/utils/inreplace.rb +++ b/Library/Homebrew/utils/inreplace.rb @@ -41,9 +41,10 @@ module Utils before: T.nilable(T.any(Pathname, Regexp, String)), after: T.nilable(T.any(Pathname, String, Symbol)), audit_result: T::Boolean, + blk: T.nilable(T.proc.params(s: StringInreplaceExtension).void), ).void } - def self.inreplace(paths, before = nil, after = nil, audit_result: true) + def self.inreplace(paths, before = nil, after = nil, audit_result: true, &blk) paths = Array(paths) after &&= after.to_s before = before.to_s if before.is_a?(Pathname) @@ -57,6 +58,8 @@ module Utils s = StringInreplaceExtension.new(str) if before.nil? && after.nil? + raise ArgumentError, "Must supply a block or before/after params" unless blk + yield s else s.gsub!(T.must(before), T.must(after), audit_result)