Skip to content
Snippets Groups Projects
Commit 01dec9c6 authored by Seeker's avatar Seeker
Browse files

utils/inreplace: add `inreplace_pairs`

parent 19ef9328
No related branches found
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment