From 01dec9c6a8d7cde25f0a2351e206a8628b669eee Mon Sep 17 00:00:00 2001 From: Seeker Date: Sun, 16 Aug 2020 10:28:26 -0700 Subject: [PATCH] utils/inreplace: add `inreplace_pairs` --- Library/Homebrew/dev-cmd/bump-formula-pr.rb | 33 +++------------------ Library/Homebrew/test/inreplace_spec.rb | 8 +++++ Library/Homebrew/utils/inreplace.rb | 21 ++++++++++++- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/Library/Homebrew/dev-cmd/bump-formula-pr.rb b/Library/Homebrew/dev-cmd/bump-formula-pr.rb index 8ed387f7de..5cdd52714a 100644 --- a/Library/Homebrew/dev-cmd/bump-formula-pr.rb +++ b/Library/Homebrew/dev-cmd/bump-formula-pr.rb @@ -294,7 +294,10 @@ module Homebrew "", ] end - new_contents = inreplace_pairs(formula.path, replacement_pairs.uniq.compact, args: args) + new_contents = Utils::Inreplace.inreplace_pairs(formula.path, + replacement_pairs.uniq.compact, + read_only_run: read_only_run, + silent: args.quiet?) new_formula_version = formula_version(formula, requested_spec, new_contents) @@ -462,34 +465,6 @@ module Homebrew [remote_url, username] end - def inreplace_pairs(path, replacement_pairs, args:) - read_only_run = args.dry_run? && !args.write? - if read_only_run - str = path.open("r") { |f| Formulary.ensure_utf8_encoding(f).read } - contents = StringInreplaceExtension.new(str) - replacement_pairs.each do |old, new| - ohai "replace #{old.inspect} with #{new.inspect}" unless args.quiet? - raise "No old value for new value #{new}! Did you pass the wrong arguments?" unless old - - contents.gsub!(old, new) - end - raise Utils::InreplaceError, path => contents.errors unless contents.errors.empty? - - path.atomic_write(contents.inreplace_string) if args.write? - contents.inreplace_string - else - Utils::Inreplace.inreplace(path) do |s| - replacement_pairs.each do |old, new| - ohai "replace #{old.inspect} with #{new.inspect}" unless args.quiet? - raise "No old value for new value #{new}! Did you pass the wrong arguments?" unless old - - s.gsub!(old, new) - end - end - path.open("r") { |f| Formulary.ensure_utf8_encoding(f).read } - end - end - def formula_version(formula, spec, contents = nil) name = formula.name path = formula.path diff --git a/Library/Homebrew/test/inreplace_spec.rb b/Library/Homebrew/test/inreplace_spec.rb index e35e5bd3ae..8b2aa1be17 100644 --- a/Library/Homebrew/test/inreplace_spec.rb +++ b/Library/Homebrew/test/inreplace_spec.rb @@ -305,4 +305,12 @@ describe Utils::Inreplace do end }.to raise_error(Utils::InreplaceError) end + + describe "#inreplace_pairs" do + it "raises error if there is no old value" do + expect { + described_class.inreplace_pairs(file.path, [[nil, "f"]]) + }.to raise_error(Utils::InreplaceError) + end + end end diff --git a/Library/Homebrew/utils/inreplace.rb b/Library/Homebrew/utils/inreplace.rb index bf0aeb7add..fb3f355bf5 100644 --- a/Library/Homebrew/utils/inreplace.rb +++ b/Library/Homebrew/utils/inreplace.rb @@ -11,6 +11,8 @@ module Utils end module Inreplace + module_function + # Sometimes we have to change a bit before we install. Mostly we # prefer a patch but if you need the `prefix` of this formula in the # patch you have to resort to `inreplace`, because in the patch @@ -42,6 +44,23 @@ module Utils raise InreplaceError, errors unless errors.empty? end - module_function :inreplace + + def inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false) + str = File.open(path, "rb", &:read) + contents = StringInreplaceExtension.new(str) + replacement_pairs.each do |old, new| + ohai "replace #{old.inspect} with #{new.inspect}" unless silent + unless old + contents.errors << "No old value for new value #{new}! Did you pass the wrong arguments?" + next + end + + contents.gsub!(old, new) + end + raise InreplaceError, path => contents.errors unless contents.errors.empty? + + Pathname(path).atomic_write(contents.inreplace_string) unless read_only_run + contents.inreplace_string + end end end