From ee1d699ceaddc53f06420e6fe99eaa7bf63dd517 Mon Sep 17 00:00:00 2001 From: Ben Yohay Date: Sun, 24 Oct 2021 15:17:08 +0300 Subject: [PATCH] file ref: Remove build files that refer to it when removed. `PBXReferenceProxy` and `PBXGroup` might have build files that refer to them, and as mentioned in the previous commit, they don't have any meaning without it. Therefore everything that can be a `PBXBuildFile`'s `file_ref` removes the build files that refer to it when being removed. --- lib/xcodeproj/project/object/group.rb | 19 +++++++++++++++++++ .../project/object/reference_proxy.rb | 19 +++++++++++++++++++ spec/project/object/group_spec.rb | 9 +++++++++ spec/project/object/reference_proxy_spec.rb | 8 ++++++++ 4 files changed, 55 insertions(+) diff --git a/lib/xcodeproj/project/object/group.rb b/lib/xcodeproj/project/object/group.rb index 4888b7df8..7571eadd8 100644 --- a/lib/xcodeproj/project/object/group.rb +++ b/lib/xcodeproj/project/object/group.rb @@ -448,6 +448,25 @@ def sort(options = nil) result end end + + # @return [Array] the build files associated with the + # current reference proxy. + # + def build_files + referrers.grep(PBXBuildFile) + end + + # In addition to removing the reference proxy, this will also remove any + # items related to this reference. + # + # @see AbstractObject#remove_from_project + # + # @return [void] + # + def remove_from_project + build_files.each(&:remove_from_project) + super + end end #-----------------------------------------------------------------------# diff --git a/lib/xcodeproj/project/object/reference_proxy.rb b/lib/xcodeproj/project/object/reference_proxy.rb index 48aedbf52..08a369a7f 100644 --- a/lib/xcodeproj/project/object/reference_proxy.rb +++ b/lib/xcodeproj/project/object/reference_proxy.rb @@ -61,6 +61,25 @@ def display_name return path if path super end + + # @return [Array] the build files associated with the + # current reference proxy. + # + def build_files + referrers.grep(PBXBuildFile) + end + + # In addition to removing the reference proxy, this will also remove any + # items related to this reference. + # + # @see AbstractObject#remove_from_project + # + # @return [void] + # + def remove_from_project + build_files.each(&:remove_from_project) + super + end end end end diff --git a/spec/project/object/group_spec.rb b/spec/project/object/group_spec.rb index b3c2b6ca2..01f523944 100644 --- a/spec/project/object/group_spec.rb +++ b/spec/project/object/group_spec.rb @@ -352,6 +352,15 @@ module ProjectSpecs end end + it 'removes the build files when removing the group' do + @target = @project.new_target(:static_library, 'Pods', :ios) + build_file = @project.new(Xcodeproj::Project::PBXBuildFile) + build_file.file_ref = @group + @target.build_phases[0].files << build_file + + @group.remove_from_project + @target.build_phases[0].files.should.be.empty + end #-------------------------------------------------------------------------# end end diff --git a/spec/project/object/reference_proxy_spec.rb b/spec/project/object/reference_proxy_spec.rb index 139f1f99c..810a2a457 100644 --- a/spec/project/object/reference_proxy_spec.rb +++ b/spec/project/object/reference_proxy_spec.rb @@ -24,5 +24,13 @@ module ProjectSpecs @proxy.path = 'Path/To/Proxy' @proxy.display_name.should == 'Path/To/Proxy' end + + it 'removes the build files when removing the reference proxy' do + @target = @project.new_target(:static_library, 'Pods', :ios) + @target.build_phases[0].add_file_reference(@proxy) + + @proxy.remove_from_project + @target.build_phases[0].files.should.be.empty + end end end