keg_relocate: refactor relocate_text_files

Replace relocate_text_files with three methods that clarify intent:
replace_locations_with_placeholders, replace_placeholders_with_locations
and replace_text_in_files, the first two calling the third.
This commit is contained in:
Josh Hagins 2016-10-10 12:02:12 -04:00 committed by Josh Hagins
parent 5b64fa6fb1
commit 9c519bbdbc
5 changed files with 34 additions and 31 deletions

View File

@ -191,11 +191,7 @@ module Homebrew
begin begin
unless ARGV.include? "--skip-relocation" unless ARGV.include? "--skip-relocation"
keg.relocate_dynamic_linkage prefix, Keg::PREFIX_PLACEHOLDER, changed_files = keg.replace_locations_with_placeholders
cellar, Keg::CELLAR_PLACEHOLDER
changed_files = keg.relocate_text_files prefix, Keg::PREFIX_PLACEHOLDER,
cellar, Keg::CELLAR_PLACEHOLDER,
repository, Keg::REPOSITORY_PLACEHOLDER
end end
keg.delete_pyc_files! keg.delete_pyc_files!
@ -266,11 +262,7 @@ module Homebrew
ignore_interrupts do ignore_interrupts do
original_tab.write if original_tab original_tab.write if original_tab
unless ARGV.include? "--skip-relocation" unless ARGV.include? "--skip-relocation"
keg.relocate_dynamic_linkage Keg::PREFIX_PLACEHOLDER, prefix, keg.replace_placeholders_with_locations changed_files
Keg::CELLAR_PLACEHOLDER, cellar
keg.relocate_text_files Keg::PREFIX_PLACEHOLDER, prefix,
Keg::CELLAR_PLACEHOLDER, cellar,
Keg::REPOSITORY_PLACEHOLDER, repository, changed_files
end end
end end
end end

View File

@ -762,18 +762,12 @@ class FormulaInstaller
end end
keg = Keg.new(formula.prefix) keg = Keg.new(formula.prefix)
tab_file = formula.prefix.join(Tab::FILENAME)
# Skip the cache since the receipt will be rewritten
orig_tab = Tab.from_file_content(tab_file.read, tab_file)
changed_files = orig_tab.changed_files.map { |f| formula.prefix.join(f) }
unless formula.bottle_specification.skip_relocation? unless formula.bottle_specification.skip_relocation?
keg.relocate_dynamic_linkage Keg::PREFIX_PLACEHOLDER, HOMEBREW_PREFIX.to_s, tab = Tab.for_keg(keg)
Keg::CELLAR_PLACEHOLDER, HOMEBREW_CELLAR.to_s Tab.clear_cache
keg.replace_placeholders_with_locations tab.changed_files
end end
keg.relocate_text_files Keg::PREFIX_PLACEHOLDER, HOMEBREW_PREFIX.to_s,
Keg::CELLAR_PLACEHOLDER, HOMEBREW_CELLAR.to_s,
Keg::REPOSITORY_PLACEHOLDER, HOMEBREW_REPOSITORY.to_s, changed_files
Pathname.glob("#{formula.bottle_prefix}/{etc,var}/**/*") do |path| Pathname.glob("#{formula.bottle_prefix}/{etc,var}/**/*") do |path|
path.extend(InstallRenamed) path.extend(InstallRenamed)
@ -781,7 +775,7 @@ class FormulaInstaller
end end
FileUtils.rm_rf formula.bottle_prefix FileUtils.rm_rf formula.bottle_prefix
tab = Tab.for_keg(formula.prefix) tab = Tab.for_keg(keg)
CxxStdlib.check_compatibility( CxxStdlib.check_compatibility(
formula, formula.recursive_dependencies, formula, formula.recursive_dependencies,

View File

@ -19,16 +19,36 @@ class Keg
[] []
end end
def relocate_text_files(old_prefix, new_prefix, old_cellar, new_cellar, # rubocop:disable Metrics/ParameterLists def replace_locations_with_placeholders
old_repository, new_repository, files = nil) 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)
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)
end
def replace_text_in_files(replacements, files = nil)
files ||= text_files | libtool_files files ||= text_files | libtool_files
changed_files = [] changed_files = []
files.group_by { |f| f.stat.ino }.each_value do |first, *rest| files.map(&path.method(:join)).group_by { |f| f.stat.ino }.each_value do |first, *rest|
s = first.open("rb", &:read) s = first.open("rb", &:read)
changed = s.gsub!(old_cellar, new_cellar) regexp = Regexp.union(replacements.keys)
changed = s.gsub!(old_prefix, new_prefix) || changed changed = s.gsub!(regexp, replacements)
changed = s.gsub!(old_repository, new_repository) || changed
next unless changed next unless changed
changed_files << first.relative_path_from(path) changed_files << first.relative_path_from(path)

View File

@ -25,7 +25,6 @@ class Tab < OpenStruct
"tabfile" => formula.prefix.join(FILENAME), "tabfile" => formula.prefix.join(FILENAME),
"built_as_bottle" => build.bottle?, "built_as_bottle" => build.bottle?,
"poured_from_bottle" => false, "poured_from_bottle" => false,
"changed_files" => [],
"time" => Time.now.to_i, "time" => Time.now.to_i,
"source_modified_time" => formula.source_modified_time.to_i, "source_modified_time" => formula.source_modified_time.to_i,
"HEAD" => HOMEBREW_REPOSITORY.git_head, "HEAD" => HOMEBREW_REPOSITORY.git_head,
@ -62,7 +61,6 @@ class Tab < OpenStruct
attributes = Utils::JSON.load(content) attributes = Utils::JSON.load(content)
attributes["tabfile"] = path attributes["tabfile"] = path
attributes["source_modified_time"] ||= 0 attributes["source_modified_time"] ||= 0
attributes["changed_files"] ||= []
attributes["source"] ||= {} attributes["source"] ||= {}
tapped_from = attributes["tapped_from"] tapped_from = attributes["tapped_from"]
@ -173,7 +171,6 @@ class Tab < OpenStruct
"unused_options" => [], "unused_options" => [],
"built_as_bottle" => false, "built_as_bottle" => false,
"poured_from_bottle" => false, "poured_from_bottle" => false,
"changed_files" => [],
"time" => nil, "time" => nil,
"source_modified_time" => 0, "source_modified_time" => 0,
"HEAD" => nil, "HEAD" => nil,
@ -306,7 +303,7 @@ class Tab < OpenStruct
"unused_options" => unused_options.as_flags, "unused_options" => unused_options.as_flags,
"built_as_bottle" => built_as_bottle, "built_as_bottle" => built_as_bottle,
"poured_from_bottle" => poured_from_bottle, "poured_from_bottle" => poured_from_bottle,
"changed_files" => changed_files.map(&:to_s), "changed_files" => changed_files && changed_files.map(&:to_s),
"time" => time, "time" => time,
"source_modified_time" => source_modified_time.to_i, "source_modified_time" => source_modified_time.to_i,
"HEAD" => self.HEAD, "HEAD" => self.HEAD,

View File

@ -34,7 +34,7 @@ class TabTests < Homebrew::TestCase
tab = Tab.empty tab = Tab.empty
assert_empty tab.unused_options assert_empty tab.unused_options
assert_empty tab.used_options assert_empty tab.used_options
assert_empty tab.changed_files assert_nil tab.changed_files
refute_predicate tab, :built_as_bottle refute_predicate tab, :built_as_bottle
refute_predicate tab, :poured_from_bottle refute_predicate tab, :poured_from_bottle
assert_predicate tab, :stable? assert_predicate tab, :stable?