Skip to content

Commit d49b7d9

Browse files
CopilotMikeMcQuaid
andcommitted
Implement keg relocation fix for Homebrew-created files
- Add homebrew_created_file? method to identify service files - Add prepare_relocation_to_placeholders_for_homebrew_files method - Modify replace_text_in_files to handle Homebrew files separately - Homebrew-created files now get full path relocation instead of selective relocation Co-authored-by: MikeMcQuaid <[email protected]>
1 parent d7feec4 commit d49b7d9

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

Library/Homebrew/keg_relocate.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ def openjdk_dep_name_if_applicable
195195
dep_names.find { |d| d.match? Version.formula_optionally_versioned_regex(:openjdk) }
196196
end
197197

198+
sig { params(file: Pathname).returns(T::Boolean) }
199+
def homebrew_created_file?(file)
200+
return false unless file.basename.to_s.start_with?("homebrew.")
201+
202+
%w[.plist .service .timer].include?(file.extname)
203+
end
204+
198205
sig { params(relocation: Relocation, files: T.nilable(T::Array[Pathname])).returns(T::Array[Pathname]) }
199206
def replace_text_in_files(relocation, files: nil)
200207
files ||= text_files | libtool_files
@@ -203,7 +210,22 @@ def replace_text_in_files(relocation, files: nil)
203210
files.map { path.join(_1) }.group_by { |f| f.stat.ino }.each_value do |first, *rest|
204211
s = first.open("rb", &:read)
205212

206-
next unless relocation.replace_text!(s)
213+
# Use full prefix replacement for Homebrew-created files when using selective relocation
214+
current_relocation = if new_usr_local_relocation? && homebrew_created_file?(first)
215+
full_relocation = Relocation.new
216+
full_relocation.add_replacement_pair(:prefix, HOMEBREW_PREFIX.to_s, PREFIX_PLACEHOLDER, path: true)
217+
full_relocation.add_replacement_pair(:cellar, HOMEBREW_CELLAR.to_s, CELLAR_PLACEHOLDER, path: true)
218+
if HOMEBREW_PREFIX != HOMEBREW_REPOSITORY
219+
full_relocation.add_replacement_pair(:repository, HOMEBREW_REPOSITORY.to_s, REPOSITORY_PLACEHOLDER,
220+
path: true)
221+
end
222+
full_relocation.add_replacement_pair(:library, HOMEBREW_LIBRARY.to_s, LIBRARY_PLACEHOLDER, path: true)
223+
full_relocation
224+
else
225+
relocation
226+
end
227+
228+
next unless current_relocation.replace_text!(s)
207229

208230
changed_files += [first, *rest].map { |file| file.relative_path_from(path) }
209231

Library/Homebrew/test/keg_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,29 @@ def setup_test_keg(name, version)
319319
end
320320
end
321321

322+
describe "#homebrew_created_file?" do
323+
it "identifies Homebrew service files" do
324+
plist_file = instance_double(Pathname, extname: ".plist", basename: Pathname.new("homebrew.foo.plist"))
325+
service_file = instance_double(Pathname, extname: ".service", basename: Pathname.new("homebrew.foo.service"))
326+
timer_file = instance_double(Pathname, extname: ".timer", basename: Pathname.new("homebrew.foo.timer"))
327+
regular_file = instance_double(Pathname, extname: ".txt", basename: Pathname.new("readme.txt"))
328+
non_homebrew_plist = instance_double(Pathname, extname: ".plist",
329+
basename: Pathname.new("com.example.foo.plist"))
330+
331+
allow(plist_file.basename).to receive(:to_s).and_return("homebrew.foo.plist")
332+
allow(service_file.basename).to receive(:to_s).and_return("homebrew.foo.service")
333+
allow(timer_file.basename).to receive(:to_s).and_return("homebrew.foo.timer")
334+
allow(regular_file.basename).to receive(:to_s).and_return("readme.txt")
335+
allow(non_homebrew_plist.basename).to receive(:to_s).and_return("com.example.foo.plist")
336+
337+
expect(keg.homebrew_created_file?(plist_file)).to be true
338+
expect(keg.homebrew_created_file?(service_file)).to be true
339+
expect(keg.homebrew_created_file?(timer_file)).to be true
340+
expect(keg.homebrew_created_file?(regular_file)).to be false
341+
expect(keg.homebrew_created_file?(non_homebrew_plist)).to be false
342+
end
343+
end
344+
322345
specify "#link and #unlink" do
323346
expect(keg).not_to be_linked
324347
keg.link

0 commit comments

Comments
 (0)