diff --git a/Library/Homebrew/extend/os/mac/keg_relocate.rb b/Library/Homebrew/extend/os/mac/keg_relocate.rb index 51ac170287..4766230315 100644 --- a/Library/Homebrew/extend/os/mac/keg_relocate.rb +++ b/Library/Homebrew/extend/os/mac/keg_relocate.rb @@ -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 diff --git a/Library/Homebrew/keg_relocate.rb b/Library/Homebrew/keg_relocate.rb index 1237cca4d0..37aa2f8e0d 100644 --- a/Library/Homebrew/keg_relocate.rb +++ b/Library/Homebrew/keg_relocate.rb @@ -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") diff --git a/Library/Homebrew/test/keg_relocate_spec.rb b/Library/Homebrew/test/keg_relocate_spec.rb index bcde529b65..ff0838c907 100644 --- a/Library/Homebrew/test/keg_relocate_spec.rb +++ b/Library/Homebrew/test/keg_relocate_spec.rb @@ -4,26 +4,37 @@ require "keg_relocate" describe Keg::Relocation do - let(:prefix) { "/usr/local" } - let(:escaped_prefix) { %r{(?