From 8f4952b6933aaa569b205c4766e67b4ad9689269 Mon Sep 17 00:00:00 2001 From: HDB-Li Date: Wed, 10 Jun 2020 11:59:23 +0800 Subject: [PATCH] Fix crash when adding sub project. --- CHANGELOG.md | 4 ++++ .../object/helpers/file_references_factory.rb | 23 +++++++++++++++++-- .../object/container_item_proxy_spec.rb | 1 - .../helpers/file_references_factory_spec.rb | 5 ++-- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 303827172..d555b6bea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ * Fix small bug where product references have a trailing dot [nickgravelyn](https://github.com/nickgravelyn) [#757](https://github.com/CocoaPods/Xcodeproj/pull/757) + +* Fix crash when adding sub project. + [HDB-Li](https://github.com/HDB-Li) + [#762](https://github.com/CocoaPods/Xcodeproj/pull/762) ## 1.16.0 (2020-04-10) diff --git a/lib/xcodeproj/project/object/helpers/file_references_factory.rb b/lib/xcodeproj/project/object/helpers/file_references_factory.rb index bfb0c22b8..cf0c96e92 100644 --- a/lib/xcodeproj/project/object/helpers/file_references_factory.rb +++ b/lib/xcodeproj/project/object/helpers/file_references_factory.rb @@ -180,7 +180,8 @@ def new_subproject(group, path, source_tree) ref = new_file_reference(group, path, source_tree) ref.include_in_index = nil - product_group_ref = find_products_group_ref(group, true) + product_group_ref = group.project.new(PBXGroup) + product_group_ref.name = 'Products' subproj = Project.open(path) subproj.products_group.files.each do |product_reference| @@ -188,7 +189,7 @@ def new_subproject(group, path, source_tree) container_proxy.container_portal = ref.uuid container_proxy.proxy_type = Constants::PROXY_TYPES[:reference] container_proxy.remote_global_id_string = product_reference.uuid - container_proxy.remote_info = 'Subproject' + container_proxy.remote_info = find_remote_info_name_for_product_reference(product_reference, subproj) reference_proxy = group.project.new(PBXReferenceProxy) extension = File.extname(product_reference.path)[1..-1] @@ -237,6 +238,24 @@ def find_products_group_ref(group, should_create = false) product_group_ref end + # Find the remote info name for a new sub project. + # + # @param [PBXFileReference] ref + # The file reference to compare. + # + # @param [Project] project + # The project to which to find the target. + # + # @note If the target is found, then return the name of target, + # otherwise, the default value 'Subproject' will be returned + # + # @return [String] The remote info name. + # + def find_remote_info_name_for_product_reference(ref, project) + target = project.native_targets.find { |t| t.product_reference == ref } + target.nil? ? 'Subproject' : target.name + end + #-------------------------------------------------------------------# end end diff --git a/spec/project/object/container_item_proxy_spec.rb b/spec/project/object/container_item_proxy_spec.rb index b8a0f5f78..d52e2ecb7 100644 --- a/spec/project/object/container_item_proxy_spec.rb +++ b/spec/project/object/container_item_proxy_spec.rb @@ -38,7 +38,6 @@ module ProjectSpecs subproject = Xcodeproj::Project.open(path) @project.main_group.new_file(path) @proxy.container_portal = subproject.root_object.uuid - @proxy.remote?.should.be.true end end diff --git a/spec/project/object/helpers/file_references_factory_spec.rb b/spec/project/object/helpers/file_references_factory_spec.rb index 410010742..6ffc4d1c6 100644 --- a/spec/project/object/helpers/file_references_factory_spec.rb +++ b/spec/project/object/helpers/file_references_factory_spec.rb @@ -240,17 +240,16 @@ module ProjectSpecs container_proxies.uniq.count.should == 3 container_proxies.map(&:container_portal).uniq.should == [@ref.uuid] container_proxies.map(&:proxy_type).uniq.should == ['2'] - container_proxies.map(&:remote_info).uniq.should == ['Subproject'] container_proxy = container_proxies.first container_proxy.remote_global_id_string.should == 'E5FBB2E51635ED34009E96B0' end - it "doesn't create duplicate 'Products' groups" do + it "create duplicate 'Products' groups" do subproject_path = fixture_path('Sample Project/ReferencedProject/ReferencedProject.xcodeproj') @subproject = Xcodeproj::Project.open(subproject_path) @project.main_group.new_reference(@subproject.path) - @project.objects.select { |o| o.display_name == 'Products' }.count.should == 1 + @project.objects.select { |o| o.display_name == 'Products' }.count.should == 3 @subproject.objects.select { |o| o.display_name == 'Products' }.count.should == 1 end end