From 03a489bf78709f9361109d65817ae8821eeef864 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Sat, 30 Jul 2022 19:10:40 +0200 Subject: [PATCH 1/4] brew.rb: tell users to fix head issues with inreplace --- Library/Homebrew/formula.rb | 9 ++++++--- Library/Homebrew/test/formula_spec.rb | 10 ++++++++++ Library/Homebrew/utils/inreplace.rb | 6 +++--- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index d2f9ea56f2..cd67b45278 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -69,9 +69,6 @@ class Formula extend Cachable extend Predicable - # @!method inreplace(paths, before = nil, after = nil) - # @see Utils::Inreplace.inreplace - # The name of this {Formula}. # e.g. `this-formula` attr_reader :name @@ -2169,6 +2166,12 @@ class Formula # end def install; end + def inreplace(paths, before = nil, after = nil, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter + super(paths, before, after, audit_result) + rescue Utils::Inreplace::Error + raise BuildError.new(self, "inreplace", paths, nil) + end + protected def setup_home(home) diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb index 0883985623..582596d29a 100644 --- a/Library/Homebrew/test/formula_spec.rb +++ b/Library/Homebrew/test/formula_spec.rb @@ -483,6 +483,16 @@ describe Formula do end end + describe "::inreplace" do + specify "raises build error on failure" do + f = formula do + url "https://brew.sh/test-1.0.tbz" + end + + expect { f.inreplace([]) }.to raise_error(BuildError) + end + end + describe "::installed_with_alias_path" do specify "with alias path with nil" do expect(described_class.installed_with_alias_path(nil)).to be_empty diff --git a/Library/Homebrew/utils/inreplace.rb b/Library/Homebrew/utils/inreplace.rb index 35f7b19ae9..fc54572fce 100644 --- a/Library/Homebrew/utils/inreplace.rb +++ b/Library/Homebrew/utils/inreplace.rb @@ -10,7 +10,7 @@ module Utils module Inreplace extend T::Sig - # Error during replacement. + # Error during text replacement. class Error < RuntimeError def initialize(errors) formatted_errors = errors.reduce(+"inreplace failed\n") do |s, (path, errs)| @@ -70,7 +70,7 @@ module Utils Pathname(path).atomic_write(s.inreplace_string) end - raise Error, errors unless errors.empty? + raise Utils::Inreplace::Error, errors unless errors.empty? end # @api private @@ -86,7 +86,7 @@ module Utils contents.gsub!(old, new) end - raise Error, path => contents.errors unless contents.errors.empty? + raise Utils::Inreplace::Error, [path => contents.errors] unless contents.errors.empty? Pathname(path).atomic_write(contents.inreplace_string) unless read_only_run contents.inreplace_string From 040d93a0060d277c684470165ab170df64f2d1a8 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Sun, 7 Aug 2022 15:55:37 +0200 Subject: [PATCH 2/4] formula: add back docstring --- Library/Homebrew/formula.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index cd67b45278..c4ae6330a4 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -2166,6 +2166,24 @@ class Formula # end def install; end + # 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`, + # because in the patch you don't have access to any variables + # defined by the formula, as only `HOMEBREW_PREFIX` is available + # in the {DATAPatch embedded patch}. + # + # `inreplace` supports regular expressions: + #
inreplace "somefile.cfg", /look[for]what?/, "replace by #{bin}/tool"
+ # + # `inreplace` supports blocks: + #
inreplace "Makefile" do |s|
+  #   s.gsub! "/usr/local", HOMEBREW_PREFIX.to_s
+  # end
+  # 
+ # + # @see Utils::Inreplace.inreplace + # @api public def inreplace(paths, before = nil, after = nil, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter super(paths, before, after, audit_result) rescue Utils::Inreplace::Error From 12d31853abe33915c9334c80346a80f5e3cd1366 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Mon, 8 Aug 2022 14:01:44 +0200 Subject: [PATCH 3/4] inreplace: fix map --- Library/Homebrew/utils/inreplace.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/utils/inreplace.rb b/Library/Homebrew/utils/inreplace.rb index fc54572fce..39543d3020 100644 --- a/Library/Homebrew/utils/inreplace.rb +++ b/Library/Homebrew/utils/inreplace.rb @@ -86,7 +86,7 @@ module Utils contents.gsub!(old, new) end - raise Utils::Inreplace::Error, [path => contents.errors] unless contents.errors.empty? + raise Utils::Inreplace::Error, path => contents.errors unless contents.errors.empty? Pathname(path).atomic_write(contents.inreplace_string) unless read_only_run contents.inreplace_string From 95080ebbb7f8293741172dd655bac3d37d6a389b Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Mon, 8 Aug 2022 16:18:06 +0200 Subject: [PATCH 4/4] inreplace: style fixes Co-authored-by: Mike McQuaid --- Library/Homebrew/utils/inreplace.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/utils/inreplace.rb b/Library/Homebrew/utils/inreplace.rb index 39543d3020..9763760258 100644 --- a/Library/Homebrew/utils/inreplace.rb +++ b/Library/Homebrew/utils/inreplace.rb @@ -70,7 +70,7 @@ module Utils Pathname(path).atomic_write(s.inreplace_string) end - raise Utils::Inreplace::Error, errors unless errors.empty? + raise Utils::Inreplace::Error, errors if errors.present? end # @api private @@ -86,7 +86,7 @@ module Utils contents.gsub!(old, new) end - raise Utils::Inreplace::Error, path => contents.errors unless contents.errors.empty? + raise Utils::Inreplace::Error, path => contents.errors if contents.errors.present? Pathname(path).atomic_write(contents.inreplace_string) unless read_only_run contents.inreplace_string