mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00
srb: set utils/inreplace.rb to true and refactor
- Sorbet gives preference to class methods over methods defined in included modules, hence Sorbet was unavailable to resolve the definition of the gsub! method. - The gsub! method in StringInreplaceExtension conflicts with the definition in String. - This PR refactors the call to the gsub! method so that a custom object is exposed instead of a string.
This commit is contained in:
parent
c9fac7c289
commit
fd382d2ecd
@ -480,7 +480,7 @@ module Homebrew
|
|||||||
update_or_add = nil
|
update_or_add = nil
|
||||||
|
|
||||||
Utils::Inreplace.inreplace(path) do |s|
|
Utils::Inreplace.inreplace(path) do |s|
|
||||||
if s.include? "bottle do"
|
if s.inreplace_string.include? "bottle do"
|
||||||
update_or_add = "update"
|
update_or_add = "update"
|
||||||
if args.keep_old?
|
if args.keep_old?
|
||||||
mismatches = []
|
mismatches = []
|
||||||
|
@ -3,22 +3,27 @@
|
|||||||
require "active_support/core_ext/object/blank"
|
require "active_support/core_ext/object/blank"
|
||||||
|
|
||||||
# Used by the inreplace function (in `utils.rb`).
|
# Used by the inreplace function (in `utils.rb`).
|
||||||
module StringInreplaceExtension
|
class StringInreplaceExtension
|
||||||
attr_accessor :errors
|
attr_accessor :errors, :inreplace_string
|
||||||
|
|
||||||
|
def initialize(str)
|
||||||
|
@inreplace_string = str
|
||||||
|
@errors = []
|
||||||
|
end
|
||||||
|
|
||||||
def self.extended(str)
|
def self.extended(str)
|
||||||
str.errors = []
|
str.errors = []
|
||||||
end
|
end
|
||||||
|
|
||||||
def sub!(before, after)
|
def sub!(before, after)
|
||||||
result = super
|
result = inreplace_string.sub!(before, after)
|
||||||
errors << "expected replacement of #{before.inspect} with #{after.inspect}" unless result
|
errors << "expected replacement of #{before.inspect} with #{after.inspect}" unless result
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
# Warn if nothing was replaced
|
# Warn if nothing was replaced
|
||||||
def gsub!(before, after, audit_result = true)
|
def gsub!(before, after, audit_result = true)
|
||||||
result = super(before, after)
|
result = inreplace_string.gsub!(before, after)
|
||||||
errors << "expected replacement of #{before.inspect} with #{after.inspect}" if audit_result && result.nil?
|
errors << "expected replacement of #{before.inspect} with #{after.inspect}" if audit_result && result.nil?
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
@ -43,6 +48,6 @@ module StringInreplaceExtension
|
|||||||
|
|
||||||
# Finds the specified variable
|
# Finds the specified variable
|
||||||
def get_make_var(flag)
|
def get_make_var(flag)
|
||||||
self[/^#{Regexp.escape(flag)}[ \t]*[\\?+:!]?=[ \t]*((?:.*\\\n)*.*)$/, 1]
|
inreplace_string[/^#{Regexp.escape(flag)}[ \t]*[\\?+:!]?=[ \t]*((?:.*\\\n)*.*)$/, 1]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -824,7 +824,6 @@ false:
|
|||||||
- ./test/version_spec.rb
|
- ./test/version_spec.rb
|
||||||
- ./unpack_strategy/uncompressed.rb
|
- ./unpack_strategy/uncompressed.rb
|
||||||
- ./utils/gems.rb
|
- ./utils/gems.rb
|
||||||
- ./utils/inreplace.rb
|
|
||||||
- ./version.rb
|
- ./version.rb
|
||||||
|
|
||||||
true:
|
true:
|
||||||
@ -888,6 +887,7 @@ true:
|
|||||||
- ./test/support/helper/fixtures.rb
|
- ./test/support/helper/fixtures.rb
|
||||||
- ./test/support/lib/config.rb
|
- ./test/support/lib/config.rb
|
||||||
- ./utils/bottles.rb
|
- ./utils/bottles.rb
|
||||||
|
- ./utils/inreplace.rb
|
||||||
- ./utils/link.rb
|
- ./utils/link.rb
|
||||||
- ./utils/notability.rb
|
- ./utils/notability.rb
|
||||||
- ./utils/shebang.rb
|
- ./utils/shebang.rb
|
||||||
|
27
Library/Homebrew/sorbet/rbi/utils/inreplace.rbi
Normal file
27
Library/Homebrew/sorbet/rbi/utils/inreplace.rbi
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
# typed: strict
|
||||||
|
|
||||||
|
module Utils::Inreplace
|
||||||
|
include Kernel
|
||||||
|
|
||||||
|
sig { params(paths: T::Array[T.untyped], before: T.nilable(String), after: T.nilable(String), audit_result: T::Boolean).void }
|
||||||
|
def inreplace(paths, before = nil, after = nil, audit_result = true); end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
class StringInreplaceExtension
|
||||||
|
sig { params(before: String, after: String).returns(T.nilable(String)) }
|
||||||
|
def sub!(before, after)
|
||||||
|
end
|
||||||
|
|
||||||
|
sig { params(before: T.nilable(String), after: T.nilable(String), audit_result: T::Boolean).returns(T.nilable(String)) }
|
||||||
|
def gsub!(before, after, audit_result = true); end
|
||||||
|
|
||||||
|
def change_make_var!(flag, new_value)
|
||||||
|
end
|
||||||
|
|
||||||
|
def remove_make_var!(flags)
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_make_var(flag)
|
||||||
|
end
|
||||||
|
end
|
@ -5,7 +5,7 @@ require "tempfile"
|
|||||||
require "utils/inreplace"
|
require "utils/inreplace"
|
||||||
|
|
||||||
describe StringInreplaceExtension do
|
describe StringInreplaceExtension do
|
||||||
subject { string.dup.extend(described_class) }
|
subject { described_class.new(string.dup) }
|
||||||
|
|
||||||
describe "#change_make_var!" do
|
describe "#change_make_var!" do
|
||||||
context "flag" do
|
context "flag" do
|
||||||
@ -20,7 +20,7 @@ describe StringInreplaceExtension do
|
|||||||
|
|
||||||
it "is successfully replaced" do
|
it "is successfully replaced" do
|
||||||
subject.change_make_var! "FLAG", "def"
|
subject.change_make_var! "FLAG", "def"
|
||||||
expect(subject).to eq <<~EOS
|
expect(subject.inreplace_string).to eq <<~EOS
|
||||||
OTHER=def
|
OTHER=def
|
||||||
FLAG=def
|
FLAG=def
|
||||||
FLAG2=abc
|
FLAG2=abc
|
||||||
@ -29,7 +29,7 @@ describe StringInreplaceExtension do
|
|||||||
|
|
||||||
it "is successfully appended" do
|
it "is successfully appended" do
|
||||||
subject.change_make_var! "FLAG", "\\1 def"
|
subject.change_make_var! "FLAG", "\\1 def"
|
||||||
expect(subject).to eq <<~EOS
|
expect(subject.inreplace_string).to eq <<~EOS
|
||||||
OTHER=def
|
OTHER=def
|
||||||
FLAG=abc def
|
FLAG=abc def
|
||||||
FLAG2=abc
|
FLAG2=abc
|
||||||
@ -47,7 +47,7 @@ describe StringInreplaceExtension do
|
|||||||
|
|
||||||
it "is successfully replaced" do
|
it "is successfully replaced" do
|
||||||
subject.change_make_var! "CFLAGS", "-O3"
|
subject.change_make_var! "CFLAGS", "-O3"
|
||||||
expect(subject).to eq <<~EOS
|
expect(subject.inreplace_string).to eq <<~EOS
|
||||||
CFLAGS=-O3
|
CFLAGS=-O3
|
||||||
LDFLAGS\t=\t-lcrypto -lssl
|
LDFLAGS\t=\t-lcrypto -lssl
|
||||||
EOS
|
EOS
|
||||||
@ -65,7 +65,7 @@ describe StringInreplaceExtension do
|
|||||||
|
|
||||||
it "is successfully replaced" do
|
it "is successfully replaced" do
|
||||||
subject.change_make_var! "CFLAGS", "-O3"
|
subject.change_make_var! "CFLAGS", "-O3"
|
||||||
expect(subject).to eq <<~EOS
|
expect(subject.inreplace_string).to eq <<~EOS
|
||||||
CFLAGS=-O3
|
CFLAGS=-O3
|
||||||
LDFLAGS = -lcrypto -lssl
|
LDFLAGS = -lcrypto -lssl
|
||||||
EOS
|
EOS
|
||||||
@ -84,7 +84,7 @@ describe StringInreplaceExtension do
|
|||||||
|
|
||||||
it "is successfully replaced" do
|
it "is successfully replaced" do
|
||||||
subject.change_make_var! "FLAG", "def"
|
subject.change_make_var! "FLAG", "def"
|
||||||
expect(subject).to eq <<~EOS
|
expect(subject.inreplace_string).to eq <<~EOS
|
||||||
OTHER=def
|
OTHER=def
|
||||||
FLAG=def
|
FLAG=def
|
||||||
FLAG2=abc
|
FLAG2=abc
|
||||||
@ -102,7 +102,7 @@ describe StringInreplaceExtension do
|
|||||||
|
|
||||||
it "is successfully replaced" do
|
it "is successfully replaced" do
|
||||||
subject.change_make_var! "FLAG", "def"
|
subject.change_make_var! "FLAG", "def"
|
||||||
expect(subject).to eq <<~EOS
|
expect(subject.inreplace_string).to eq <<~EOS
|
||||||
FLAG=def
|
FLAG=def
|
||||||
mv file_a file_b
|
mv file_a file_b
|
||||||
EOS
|
EOS
|
||||||
@ -120,7 +120,7 @@ describe StringInreplaceExtension do
|
|||||||
|
|
||||||
it "is successfully replaced" do
|
it "is successfully replaced" do
|
||||||
subject.change_make_var! "FLAG", "def"
|
subject.change_make_var! "FLAG", "def"
|
||||||
expect(subject).to eq <<~EOS
|
expect(subject.inreplace_string).to eq <<~EOS
|
||||||
OTHER=def
|
OTHER=def
|
||||||
FLAG=def
|
FLAG=def
|
||||||
FLAG2=abc
|
FLAG2=abc
|
||||||
@ -142,7 +142,7 @@ describe StringInreplaceExtension do
|
|||||||
|
|
||||||
it "is successfully removed" do
|
it "is successfully removed" do
|
||||||
subject.remove_make_var! "FLAG"
|
subject.remove_make_var! "FLAG"
|
||||||
expect(subject).to eq <<~EOS
|
expect(subject.inreplace_string).to eq <<~EOS
|
||||||
OTHER=def
|
OTHER=def
|
||||||
FLAG2 = def
|
FLAG2 = def
|
||||||
EOS
|
EOS
|
||||||
@ -159,7 +159,7 @@ describe StringInreplaceExtension do
|
|||||||
|
|
||||||
it "is successfully removed" do
|
it "is successfully removed" do
|
||||||
subject.remove_make_var! "LDFLAGS"
|
subject.remove_make_var! "LDFLAGS"
|
||||||
expect(subject).to eq <<~EOS
|
expect(subject.inreplace_string).to eq <<~EOS
|
||||||
CFLAGS\t=\t-Wall -O2
|
CFLAGS\t=\t-Wall -O2
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
@ -176,7 +176,7 @@ describe StringInreplaceExtension do
|
|||||||
|
|
||||||
it "is successfully removed" do
|
it "is successfully removed" do
|
||||||
subject.remove_make_var! "CFLAGS"
|
subject.remove_make_var! "CFLAGS"
|
||||||
expect(subject).to eq <<~EOS
|
expect(subject.inreplace_string).to eq <<~EOS
|
||||||
LDFLAGS = -lcrypto -lssl
|
LDFLAGS = -lcrypto -lssl
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
@ -195,7 +195,7 @@ describe StringInreplaceExtension do
|
|||||||
|
|
||||||
specify "are be successfully removed" do
|
specify "are be successfully removed" do
|
||||||
subject.remove_make_var! ["FLAG", "FLAG2"]
|
subject.remove_make_var! ["FLAG", "FLAG2"]
|
||||||
expect(subject).to eq <<~EOS
|
expect(subject.inreplace_string).to eq <<~EOS
|
||||||
OTHER=def
|
OTHER=def
|
||||||
OTHER2=def
|
OTHER2=def
|
||||||
EOS
|
EOS
|
||||||
@ -250,7 +250,7 @@ describe StringInreplaceExtension do
|
|||||||
|
|
||||||
it "replaces the first occurrence" do
|
it "replaces the first occurrence" do
|
||||||
subject.sub!("o", "e")
|
subject.sub!("o", "e")
|
||||||
expect(subject).to eq("feo")
|
expect(subject.inreplace_string).to eq("feo")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -259,7 +259,7 @@ describe StringInreplaceExtension do
|
|||||||
|
|
||||||
it "replaces all occurrences" do
|
it "replaces all occurrences" do
|
||||||
subject.gsub!("o", "e") # rubocop:disable Performance/StringReplacement
|
subject.gsub!("o", "e") # rubocop:disable Performance/StringReplacement
|
||||||
expect(subject).to eq("fee")
|
expect(subject.inreplace_string).to eq("fee")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
require "extend/string"
|
require "extend/string"
|
||||||
|
|
||||||
describe StringInreplaceExtension do
|
describe StringInreplaceExtension do
|
||||||
subject { string.extend(described_class) }
|
subject { described_class.new(string) }
|
||||||
|
|
||||||
let(:string) { +"foobar" }
|
let(:string) { +"foobar" }
|
||||||
|
|
||||||
|
@ -25,7 +25,8 @@ module Utils
|
|||||||
errors["`paths` (first) parameter"] = ["`paths` was empty"] if paths.blank?
|
errors["`paths` (first) parameter"] = ["`paths` was empty"] if paths.blank?
|
||||||
|
|
||||||
Array(paths).each do |path|
|
Array(paths).each do |path|
|
||||||
s = File.open(path, "rb", &:read).extend(StringInreplaceExtension)
|
str = File.open(path, "rb", &:read) || ""
|
||||||
|
s = StringInreplaceExtension.new(str)
|
||||||
|
|
||||||
if before.nil? && after.nil?
|
if before.nil? && after.nil?
|
||||||
yield s
|
yield s
|
||||||
@ -36,7 +37,7 @@ module Utils
|
|||||||
|
|
||||||
errors[path] = s.errors unless s.errors.empty?
|
errors[path] = s.errors unless s.errors.empty?
|
||||||
|
|
||||||
Pathname(path).atomic_write(s)
|
Pathname(path).atomic_write(s.inreplace_string)
|
||||||
end
|
end
|
||||||
|
|
||||||
raise InreplaceError, errors unless errors.empty?
|
raise InreplaceError, errors unless errors.empty?
|
||||||
|
Loading…
x
Reference in New Issue
Block a user