From 9c519bbdbc1723c9f35e4fbc8ba594ac8d81d258 Mon Sep 17 00:00:00 2001 From: Josh Hagins <jhagins@palantir.com> Date: Mon, 10 Oct 2016 12:02:12 -0400 Subject: [PATCH] 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. --- Library/Homebrew/dev-cmd/bottle.rb | 12 ++-------- Library/Homebrew/formula_installer.rb | 14 ++++-------- Library/Homebrew/keg_relocate.rb | 32 ++++++++++++++++++++++----- Library/Homebrew/tab.rb | 5 +---- Library/Homebrew/test/test_tab.rb | 2 +- 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/Library/Homebrew/dev-cmd/bottle.rb b/Library/Homebrew/dev-cmd/bottle.rb index 4af12a7a7d..dcc9e0e98c 100644 --- a/Library/Homebrew/dev-cmd/bottle.rb +++ b/Library/Homebrew/dev-cmd/bottle.rb @@ -191,11 +191,7 @@ module Homebrew begin unless ARGV.include? "--skip-relocation" - keg.relocate_dynamic_linkage prefix, Keg::PREFIX_PLACEHOLDER, - cellar, Keg::CELLAR_PLACEHOLDER - changed_files = keg.relocate_text_files prefix, Keg::PREFIX_PLACEHOLDER, - cellar, Keg::CELLAR_PLACEHOLDER, - repository, Keg::REPOSITORY_PLACEHOLDER + changed_files = keg.replace_locations_with_placeholders end keg.delete_pyc_files! @@ -266,11 +262,7 @@ module Homebrew ignore_interrupts do original_tab.write if original_tab unless ARGV.include? "--skip-relocation" - keg.relocate_dynamic_linkage Keg::PREFIX_PLACEHOLDER, prefix, - Keg::CELLAR_PLACEHOLDER, cellar - keg.relocate_text_files Keg::PREFIX_PLACEHOLDER, prefix, - Keg::CELLAR_PLACEHOLDER, cellar, - Keg::REPOSITORY_PLACEHOLDER, repository, changed_files + keg.replace_placeholders_with_locations changed_files end end end diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index 127561c912..006db8f8aa 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -762,18 +762,12 @@ class FormulaInstaller end 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? - keg.relocate_dynamic_linkage Keg::PREFIX_PLACEHOLDER, HOMEBREW_PREFIX.to_s, - Keg::CELLAR_PLACEHOLDER, HOMEBREW_CELLAR.to_s + tab = Tab.for_keg(keg) + Tab.clear_cache + keg.replace_placeholders_with_locations tab.changed_files 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| path.extend(InstallRenamed) @@ -781,7 +775,7 @@ class FormulaInstaller end FileUtils.rm_rf formula.bottle_prefix - tab = Tab.for_keg(formula.prefix) + tab = Tab.for_keg(keg) CxxStdlib.check_compatibility( formula, formula.recursive_dependencies, diff --git a/Library/Homebrew/keg_relocate.rb b/Library/Homebrew/keg_relocate.rb index be07475367..0e21bb7330 100644 --- a/Library/Homebrew/keg_relocate.rb +++ b/Library/Homebrew/keg_relocate.rb @@ -19,16 +19,36 @@ class Keg [] end - def relocate_text_files(old_prefix, new_prefix, old_cellar, new_cellar, # rubocop:disable Metrics/ParameterLists - old_repository, new_repository, files = nil) + 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) + 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 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) - changed = s.gsub!(old_cellar, new_cellar) - changed = s.gsub!(old_prefix, new_prefix) || changed - changed = s.gsub!(old_repository, new_repository) || changed + regexp = Regexp.union(replacements.keys) + changed = s.gsub!(regexp, replacements) next unless changed changed_files << first.relative_path_from(path) diff --git a/Library/Homebrew/tab.rb b/Library/Homebrew/tab.rb index 3ce6eadc5e..1cabacefdd 100644 --- a/Library/Homebrew/tab.rb +++ b/Library/Homebrew/tab.rb @@ -25,7 +25,6 @@ class Tab < OpenStruct "tabfile" => formula.prefix.join(FILENAME), "built_as_bottle" => build.bottle?, "poured_from_bottle" => false, - "changed_files" => [], "time" => Time.now.to_i, "source_modified_time" => formula.source_modified_time.to_i, "HEAD" => HOMEBREW_REPOSITORY.git_head, @@ -62,7 +61,6 @@ class Tab < OpenStruct attributes = Utils::JSON.load(content) attributes["tabfile"] = path attributes["source_modified_time"] ||= 0 - attributes["changed_files"] ||= [] attributes["source"] ||= {} tapped_from = attributes["tapped_from"] @@ -173,7 +171,6 @@ class Tab < OpenStruct "unused_options" => [], "built_as_bottle" => false, "poured_from_bottle" => false, - "changed_files" => [], "time" => nil, "source_modified_time" => 0, "HEAD" => nil, @@ -306,7 +303,7 @@ class Tab < OpenStruct "unused_options" => unused_options.as_flags, "built_as_bottle" => built_as_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, "source_modified_time" => source_modified_time.to_i, "HEAD" => self.HEAD, diff --git a/Library/Homebrew/test/test_tab.rb b/Library/Homebrew/test/test_tab.rb index 3db7bbea17..2c756cf685 100644 --- a/Library/Homebrew/test/test_tab.rb +++ b/Library/Homebrew/test/test_tab.rb @@ -34,7 +34,7 @@ class TabTests < Homebrew::TestCase tab = Tab.empty assert_empty tab.unused_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, :poured_from_bottle assert_predicate tab, :stable? -- GitLab