Only use path regex when requested
This commit is contained in:
parent
5f781770b4
commit
f13f7b9326
@ -131,9 +131,9 @@ class Keg
|
||||
old_prefix, new_prefix = relocation.replacement_pair_for(:prefix)
|
||||
old_cellar, new_cellar = relocation.replacement_pair_for(:cellar)
|
||||
|
||||
if old_name.start_with? old_cellar
|
||||
if relocation.start_with_old_value? :cellar, old_name
|
||||
old_name.sub(old_cellar, new_cellar)
|
||||
elsif old_name.start_with? old_prefix
|
||||
elsif relocation.start_with_old_value? :prefix, old_name
|
||||
old_name.sub(old_prefix, new_prefix)
|
||||
end
|
||||
end
|
||||
|
||||
@ -22,9 +22,10 @@ class Keg
|
||||
super
|
||||
end
|
||||
|
||||
sig { params(key: Symbol, old_value: T.any(String, Regexp), new_value: String).void }
|
||||
def add_replacement_pair(key, old_value, new_value)
|
||||
@replacement_map[key] = [self.class.path_regex(old_value), new_value]
|
||||
sig { params(key: Symbol, old_value: T.any(String, Regexp), new_value: String, path: T::Boolean).void }
|
||||
def add_replacement_pair(key, old_value, new_value, path: false)
|
||||
old_value = self.class.path_regex(old_value) if path
|
||||
@replacement_map[key] = [old_value, new_value]
|
||||
end
|
||||
|
||||
sig { params(key: Symbol).returns(T::Array[T.any(String, Regexp)]) }
|
||||
@ -32,6 +33,10 @@ class Keg
|
||||
@replacement_map.fetch(key)
|
||||
end
|
||||
|
||||
def start_with_old_value?(key, text)
|
||||
text.match?(/^#{@replacement_map.fetch(key)}/)
|
||||
end
|
||||
|
||||
sig { params(text: String).void }
|
||||
def replace_text(text)
|
||||
replacements = @replacement_map.values.to_h
|
||||
@ -83,14 +88,14 @@ class Keg
|
||||
|
||||
def prepare_relocation_to_placeholders
|
||||
relocation = Relocation.new
|
||||
relocation.add_replacement_pair(:prefix, HOMEBREW_PREFIX.to_s, PREFIX_PLACEHOLDER)
|
||||
relocation.add_replacement_pair(:cellar, HOMEBREW_CELLAR.to_s, CELLAR_PLACEHOLDER)
|
||||
relocation.add_replacement_pair(:prefix, HOMEBREW_PREFIX.to_s, PREFIX_PLACEHOLDER, path: true)
|
||||
relocation.add_replacement_pair(:cellar, HOMEBREW_CELLAR.to_s, CELLAR_PLACEHOLDER, path: true)
|
||||
# when HOMEBREW_PREFIX == HOMEBREW_REPOSITORY we should use HOMEBREW_PREFIX for all relocations to avoid
|
||||
# being unable to differentiate between them.
|
||||
if HOMEBREW_PREFIX != HOMEBREW_REPOSITORY
|
||||
relocation.add_replacement_pair(:repository, HOMEBREW_REPOSITORY.to_s, REPOSITORY_PLACEHOLDER)
|
||||
relocation.add_replacement_pair(:repository, HOMEBREW_REPOSITORY.to_s, REPOSITORY_PLACEHOLDER, path: true)
|
||||
end
|
||||
relocation.add_replacement_pair(:library, HOMEBREW_LIBRARY.to_s, LIBRARY_PLACEHOLDER)
|
||||
relocation.add_replacement_pair(:library, HOMEBREW_LIBRARY.to_s, LIBRARY_PLACEHOLDER, path: true)
|
||||
relocation.add_replacement_pair(:perl,
|
||||
%r{\A#!(?:/usr/bin/perl\d\.\d+|#{HOMEBREW_PREFIX}/opt/perl/bin/perl)( |$)}o,
|
||||
"#!#{PERL_PLACEHOLDER}\\1")
|
||||
|
||||
@ -4,26 +4,37 @@
|
||||
require "keg_relocate"
|
||||
|
||||
describe Keg::Relocation do
|
||||
let(:prefix) { "/usr/local" }
|
||||
let(:escaped_prefix) { %r{(?<![a-zA-Z0-9])/usr/local} }
|
||||
let(:cellar) { "#{prefix}/Cellar" }
|
||||
let(:escaped_cellar) { %r{(?<![a-zA-Z0-9])/usr/local/Cellar} }
|
||||
let(:prefix) { HOMEBREW_PREFIX.to_s }
|
||||
let(:cellar) { HOMEBREW_CELLAR.to_s }
|
||||
let(:repository) { HOMEBREW_REPOSITORY.to_s }
|
||||
let(:library) { HOMEBREW_LIBRARY.to_s }
|
||||
let(:prefix_placeholder) { "@@HOMEBREW_PREFIX@@" }
|
||||
let(:cellar_placeholder) { "@@HOMEBREW_CELLAR@@" }
|
||||
let(:repository_placeholder) { "@@HOMEBREW_REPOSITORY@@" }
|
||||
let(:library_placeholder) { "@@HOMEBREW_LIBRARY@@" }
|
||||
let(:escaped_prefix) { /(?<![a-zA-Z0-9])#{Regexp.escape(HOMEBREW_PREFIX)}/o }
|
||||
let(:escaped_cellar) { /(?<![a-zA-Z0-9])#{HOMEBREW_CELLAR}/o }
|
||||
|
||||
def setup_relocation
|
||||
relocation = described_class.new
|
||||
relocation.add_replacement_pair :prefix, prefix, prefix_placeholder, path: true
|
||||
relocation.add_replacement_pair :cellar, /#{cellar}/o, cellar_placeholder, path: true
|
||||
relocation.add_replacement_pair :repository_placeholder, repository_placeholder, repository
|
||||
relocation.add_replacement_pair :library_placeholder, library_placeholder, library
|
||||
relocation
|
||||
end
|
||||
|
||||
specify "#add_replacement_pair" do
|
||||
relocation = described_class.new
|
||||
relocation.add_replacement_pair :prefix, prefix, prefix_placeholder
|
||||
relocation.add_replacement_pair :cellar, /#{cellar}/o, cellar_placeholder
|
||||
relocation = setup_relocation
|
||||
|
||||
expect(relocation.replacement_pair_for(:prefix)).to eq [escaped_prefix, prefix_placeholder]
|
||||
expect(relocation.replacement_pair_for(:cellar)).to eq [escaped_cellar, cellar_placeholder]
|
||||
expect(relocation.replacement_pair_for(:repository_placeholder)).to eq [repository_placeholder, repository]
|
||||
expect(relocation.replacement_pair_for(:library_placeholder)).to eq [library_placeholder, library]
|
||||
end
|
||||
|
||||
specify "#replace_text" do
|
||||
relocation = described_class.new
|
||||
relocation.add_replacement_pair :prefix, prefix, prefix_placeholder
|
||||
relocation.add_replacement_pair :cellar, /#{cellar}/o, cellar_placeholder
|
||||
relocation = setup_relocation
|
||||
|
||||
text = +"foo"
|
||||
relocation.replace_text(text)
|
||||
@ -34,6 +45,8 @@ describe Keg::Relocation do
|
||||
#{cellar}/foo
|
||||
foo#{prefix}/bar
|
||||
foo#{cellar}/bar
|
||||
#{repository_placeholder}/foo
|
||||
foo#{library_placeholder}/bar
|
||||
TEXT
|
||||
relocation.replace_text(text)
|
||||
expect(text).to eq <<~REPLACED
|
||||
@ -41,6 +54,8 @@ describe Keg::Relocation do
|
||||
#{cellar_placeholder}/foo
|
||||
foo#{prefix}/bar
|
||||
foo#{cellar}/bar
|
||||
#{repository}/foo
|
||||
foo#{library}/bar
|
||||
REPLACED
|
||||
end
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user