Merge pull request #15824 from dduugg/inreplace-static
Make inreplace a purely static method v2
This commit is contained in:
commit
d313e8b027
@ -6,8 +6,6 @@ require "utils/inreplace"
|
||||
|
||||
module Cask
|
||||
class Migrator
|
||||
extend ::Utils::Inreplace
|
||||
|
||||
attr_reader :old_cask, :new_cask
|
||||
|
||||
sig { params(old_cask: Cask, new_cask: Cask).void }
|
||||
@ -74,7 +72,7 @@ module Cask
|
||||
def self.replace_caskfile_token(path, old_token, new_token)
|
||||
case path.extname
|
||||
when ".rb"
|
||||
inreplace path, /\A\s*cask\s+"#{Regexp.escape(old_token)}"/, "cask #{new_token.inspect}"
|
||||
::Utils::Inreplace.inreplace path, /\A\s*cask\s+"#{Regexp.escape(old_token)}"/, "cask #{new_token.inspect}"
|
||||
when ".json"
|
||||
json = JSON.parse(path.read)
|
||||
json["token"] = new_token
|
||||
|
@ -59,7 +59,6 @@ require "extend/api_hashable"
|
||||
# end</pre>
|
||||
class Formula
|
||||
include FileUtils
|
||||
include Utils::Inreplace
|
||||
include Utils::Shebang
|
||||
include Utils::Shell
|
||||
include Context
|
||||
@ -2560,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,
|
||||
block: T.nilable(T.proc.params(s: StringInreplaceExtension).void),
|
||||
).void
|
||||
}
|
||||
def inreplace(paths, before = nil, after = nil, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter
|
||||
super(paths, before, after, audit_result)
|
||||
def inreplace(paths, before = nil, after = nil, audit_result = true, &block) # rubocop:disable Style/OptionalBooleanParameter
|
||||
Utils::Inreplace.inreplace(paths, before, after, audit_result: audit_result, &block)
|
||||
rescue Utils::Inreplace::Error => e
|
||||
onoe e.to_s
|
||||
raise BuildError.new(self, "inreplace", Array(paths), {})
|
||||
|
@ -464,6 +464,26 @@ describe Formula do
|
||||
|
||||
expect { f.inreplace([]) }.to raise_error(BuildError)
|
||||
end
|
||||
|
||||
specify "replaces text in file" do
|
||||
file = Tempfile.new("test")
|
||||
File.binwrite(file, <<~EOS)
|
||||
ab
|
||||
bc
|
||||
cd
|
||||
EOS
|
||||
f = formula do
|
||||
url "https://brew.sh/test-1.0.tbz"
|
||||
end
|
||||
f.inreplace(file.path) do |s|
|
||||
s.gsub!("bc", "yz")
|
||||
end
|
||||
expect(File.binread(file)).to eq <<~EOS
|
||||
ab
|
||||
yz
|
||||
cd
|
||||
EOS
|
||||
end
|
||||
end
|
||||
|
||||
describe "::installed_with_alias_path" do
|
||||
|
@ -18,8 +18,6 @@ module Utils
|
||||
end
|
||||
end
|
||||
|
||||
module_function
|
||||
|
||||
# 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`,
|
||||
@ -43,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,
|
||||
block: T.nilable(T.proc.params(s: StringInreplaceExtension).void),
|
||||
).void
|
||||
}
|
||||
def inreplace(paths, before = nil, after = nil, audit_result = true) # rubocop:disable Style/OptionalBooleanParameter
|
||||
def self.inreplace(paths, before = nil, after = nil, audit_result: true, &block)
|
||||
paths = Array(paths)
|
||||
after &&= after.to_s
|
||||
before = before.to_s if before.is_a?(Pathname)
|
||||
@ -59,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 block
|
||||
|
||||
yield s
|
||||
else
|
||||
s.gsub!(T.must(before), T.must(after), audit_result)
|
||||
@ -73,7 +74,7 @@ module Utils
|
||||
end
|
||||
|
||||
# @api private
|
||||
def inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false)
|
||||
def self.inreplace_pairs(path, replacement_pairs, read_only_run: false, silent: false)
|
||||
str = File.binread(path)
|
||||
contents = StringInreplaceExtension.new(str)
|
||||
replacement_pairs.each do |old, new|
|
||||
|
@ -1,7 +0,0 @@
|
||||
# typed: strict
|
||||
|
||||
module Utils
|
||||
module Inreplace
|
||||
include Kernel
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user