keg_relocate: wrap relocation locations in struct

This commit is contained in:
Josh Hagins 2016-10-23 23:48:07 -04:00
parent adc4b1f0c7
commit fab2cffe5d
2 changed files with 43 additions and 24 deletions

View File

@ -17,19 +17,19 @@ class Keg
generic_fix_dynamic_linkage
end
def relocate_dynamic_linkage(old_prefix, new_prefix, old_cellar, new_cellar)
def relocate_dynamic_linkage(relocation)
mach_o_files.each do |file|
file.ensure_writable do
if file.dylib?
id = dylib_id_for(file).sub(old_prefix, new_prefix)
id = dylib_id_for(file).sub(relocation.old_prefix, relocation.new_prefix)
change_dylib_id(id, file)
end
each_install_name_for(file) do |old_name|
if old_name.start_with? old_cellar
new_name = old_name.sub(old_cellar, new_cellar)
elsif old_name.start_with? old_prefix
new_name = old_name.sub(old_prefix, new_prefix)
if old_name.start_with? relocation.old_cellar
new_name = old_name.sub(relocation.old_cellar, relocation.new_cellar)
elsif old_name.start_with? relocation.old_prefix
new_name = old_name.sub(relocation.old_prefix, relocation.new_prefix)
end
change_install_name(old_name, new_name, file) if new_name

View File

@ -3,6 +3,14 @@ class Keg
CELLAR_PLACEHOLDER = "@@HOMEBREW_CELLAR@@".freeze
REPOSITORY_PLACEHOLDER = "@@HOMEBREW_REPOSITORY@@".freeze
Relocation = Struct.new(:old_prefix, :old_cellar, :old_repository,
:new_prefix, :new_cellar, :new_repository) do
# Use keyword args instead of positional args for initialization
def initialize(**kwargs)
super(*members.map { |k| kwargs[k] })
end
end
def fix_dynamic_linkage
symlink_files.each do |file|
link = file.readlink
@ -15,38 +23,49 @@ class Keg
end
alias generic_fix_dynamic_linkage fix_dynamic_linkage
def relocate_dynamic_linkage(_old_prefix, _new_prefix, _old_cellar, _new_cellar)
def relocate_dynamic_linkage(_relocation)
[]
end
def replace_locations_with_placeholders
relocate_dynamic_linkage(HOMEBREW_PREFIX.to_s, PREFIX_PLACEHOLDER,
HOMEBREW_CELLAR.to_s, CELLAR_PLACEHOLDER)
replacements = {
HOMEBREW_PREFIX.to_s => PREFIX_PLACEHOLDER,
HOMEBREW_CELLAR.to_s => CELLAR_PLACEHOLDER,
HOMEBREW_REPOSITORY.to_s => REPOSITORY_PLACEHOLDER,
}
replace_text_in_files(replacements)
relocation = Relocation.new(
old_prefix: HOMEBREW_PREFIX.to_s,
old_cellar: HOMEBREW_CELLAR.to_s,
old_repository: HOMEBREW_REPOSITORY.to_s,
new_prefix: PREFIX_PLACEHOLDER,
new_cellar: CELLAR_PLACEHOLDER,
new_repository: REPOSITORY_PLACEHOLDER
)
relocate_dynamic_linkage(relocation)
replace_text_in_files(relocation)
end
def replace_placeholders_with_locations(files)
relocate_dynamic_linkage(PREFIX_PLACEHOLDER, HOMEBREW_PREFIX.to_s,
CELLAR_PLACEHOLDER, HOMEBREW_CELLAR.to_s)
replacements = {
PREFIX_PLACEHOLDER => HOMEBREW_PREFIX.to_s,
CELLAR_PLACEHOLDER => HOMEBREW_CELLAR.to_s,
REPOSITORY_PLACEHOLDER => HOMEBREW_REPOSITORY.to_s,
}
replace_text_in_files(replacements, files)
relocation = Relocation.new(
old_prefix: PREFIX_PLACEHOLDER,
old_cellar: CELLAR_PLACEHOLDER,
old_repository: REPOSITORY_PLACEHOLDER,
new_prefix: HOMEBREW_PREFIX.to_s,
new_cellar: HOMEBREW_CELLAR.to_s,
new_repository: HOMEBREW_REPOSITORY.to_s
)
relocate_dynamic_linkage(relocation)
replace_text_in_files(relocation, files: files)
end
def replace_text_in_files(replacements, files = nil)
def replace_text_in_files(relocation, files: nil)
files ||= text_files | libtool_files
changed_files = []
files.map(&path.method(:join)).group_by { |f| f.stat.ino }.each_value do |first, *rest|
s = first.open("rb", &:read)
replacements = {
relocation.old_prefix => relocation.new_prefix,
relocation.old_cellar => relocation.new_cellar,
relocation.old_repository => relocation.new_repository,
}
regexp = Regexp.union(replacements.keys)
changed = s.gsub!(regexp, replacements)