Skip to content

Commit 8f4952b

Browse files
committed
Fix crash when adding sub project.
1 parent d9a1ccb commit 8f4952b

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
* Fix small bug where product references have a trailing dot
1313
[nickgravelyn](https://github.com/nickgravelyn)
1414
[#757](https://github.com/CocoaPods/Xcodeproj/pull/757)
15+
16+
* Fix crash when adding sub project.
17+
[HDB-Li](https://github.com/HDB-Li)
18+
[#762](https://github.com/CocoaPods/Xcodeproj/pull/762)
1519

1620

1721
## 1.16.0 (2020-04-10)

lib/xcodeproj/project/object/helpers/file_references_factory.rb

+21-2
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,16 @@ def new_subproject(group, path, source_tree)
180180
ref = new_file_reference(group, path, source_tree)
181181
ref.include_in_index = nil
182182

183-
product_group_ref = find_products_group_ref(group, true)
183+
product_group_ref = group.project.new(PBXGroup)
184+
product_group_ref.name = 'Products'
184185

185186
subproj = Project.open(path)
186187
subproj.products_group.files.each do |product_reference|
187188
container_proxy = group.project.new(PBXContainerItemProxy)
188189
container_proxy.container_portal = ref.uuid
189190
container_proxy.proxy_type = Constants::PROXY_TYPES[:reference]
190191
container_proxy.remote_global_id_string = product_reference.uuid
191-
container_proxy.remote_info = 'Subproject'
192+
container_proxy.remote_info = find_remote_info_name_for_product_reference(product_reference, subproj)
192193

193194
reference_proxy = group.project.new(PBXReferenceProxy)
194195
extension = File.extname(product_reference.path)[1..-1]
@@ -237,6 +238,24 @@ def find_products_group_ref(group, should_create = false)
237238
product_group_ref
238239
end
239240

241+
# Find the remote info name for a new sub project.
242+
#
243+
# @param [PBXFileReference] ref
244+
# The file reference to compare.
245+
#
246+
# @param [Project] project
247+
# The project to which to find the target.
248+
#
249+
# @note If the target is found, then return the name of target,
250+
# otherwise, the default value 'Subproject' will be returned
251+
#
252+
# @return [String] The remote info name.
253+
#
254+
def find_remote_info_name_for_product_reference(ref, project)
255+
target = project.native_targets.find { |t| t.product_reference == ref }
256+
target.nil? ? 'Subproject' : target.name
257+
end
258+
240259
#-------------------------------------------------------------------#
241260
end
242261
end

spec/project/object/container_item_proxy_spec.rb

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ module ProjectSpecs
3838
subproject = Xcodeproj::Project.open(path)
3939
@project.main_group.new_file(path)
4040
@proxy.container_portal = subproject.root_object.uuid
41-
4241
@proxy.remote?.should.be.true
4342
end
4443
end

spec/project/object/helpers/file_references_factory_spec.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -240,17 +240,16 @@ module ProjectSpecs
240240
container_proxies.uniq.count.should == 3
241241
container_proxies.map(&:container_portal).uniq.should == [@ref.uuid]
242242
container_proxies.map(&:proxy_type).uniq.should == ['2']
243-
container_proxies.map(&:remote_info).uniq.should == ['Subproject']
244243

245244
container_proxy = container_proxies.first
246245
container_proxy.remote_global_id_string.should == 'E5FBB2E51635ED34009E96B0'
247246
end
248247

249-
it "doesn't create duplicate 'Products' groups" do
248+
it "create duplicate 'Products' groups" do
250249
subproject_path = fixture_path('Sample Project/ReferencedProject/ReferencedProject.xcodeproj')
251250
@subproject = Xcodeproj::Project.open(subproject_path)
252251
@project.main_group.new_reference(@subproject.path)
253-
@project.objects.select { |o| o.display_name == 'Products' }.count.should == 1
252+
@project.objects.select { |o| o.display_name == 'Products' }.count.should == 3
254253
@subproject.objects.select { |o| o.display_name == 'Products' }.count.should == 1
255254
end
256255
end

0 commit comments

Comments
 (0)