Skip to content

Commit

Permalink
Fix crash when adding sub project.
Browse files Browse the repository at this point in the history
  • Loading branch information
HDB-Li committed Jun 10, 2020
1 parent d9a1ccb commit 8f4952b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 21 additions & 2 deletions lib/xcodeproj/project/object/helpers/file_references_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,16 @@ 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|
container_proxy = group.project.new(PBXContainerItemProxy)
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]
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion spec/project/object/container_item_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions spec/project/object/helpers/file_references_factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8f4952b

Please sign in to comment.