Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when adding sub project. #762

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

@dnkoutso dnkoutso Jul 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does Xcode keep adding a new "Products" group for each subproject?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Xcode does add a "Products" group for every subproject.

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)
dnkoutso marked this conversation as resolved.
Show resolved Hide resolved
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