utils/inreplace: add inreplace_pairs
This commit is contained in:
parent
19ef932885
commit
01dec9c6a8
@ -294,7 +294,10 @@ module Homebrew
|
|||||||
"",
|
"",
|
||||||
]
|
]
|
||||||
end
|
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)
|
new_formula_version = formula_version(formula, requested_spec, new_contents)
|
||||||
|
|
||||||
@ -462,34 +465,6 @@ module Homebrew
|
|||||||
[remote_url, username]
|
[remote_url, username]
|
||||||
end
|
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)
|
def formula_version(formula, spec, contents = nil)
|
||||||
name = formula.name
|
name = formula.name
|
||||||
path = formula.path
|
path = formula.path
|
||||||
|
@ -305,4 +305,12 @@ describe Utils::Inreplace do
|
|||||||
end
|
end
|
||||||
}.to raise_error(Utils::InreplaceError)
|
}.to raise_error(Utils::InreplaceError)
|
||||||
end
|
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
|
end
|
||||||
|
@ -11,6 +11,8 @@ module Utils
|
|||||||
end
|
end
|
||||||
|
|
||||||
module Inreplace
|
module Inreplace
|
||||||
|
module_function
|
||||||
|
|
||||||
# Sometimes we have to change a bit before we install. Mostly we
|
# 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
|
# 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
|
# patch you have to resort to `inreplace`, because in the patch
|
||||||
@ -42,6 +44,23 @@ module Utils
|
|||||||
|
|
||||||
raise InreplaceError, errors unless errors.empty?
|
raise InreplaceError, errors unless errors.empty?
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user