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

Add support for adding and removing on demand resources. #844

Merged
merged 1 commit into from
Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,11 @@

##### Enhancements

* Add support for adding and removing on demand resources.
[JunyiXie](https://github.com/JunyiXie)
[Dimitris Koutsogiorgas](https://github.com/dnkoutso)
[#844](https://github.com/CocoaPods/Xcodeproj/pull/844)

* Add `platform_filters` support for `PBXBuildPhase`.
[Dimitris Koutsogiorgas](https://github.com/dnkoutso)
[#838](https://github.com/CocoaPods/Xcodeproj/issues/838)
Expand Down
43 changes: 43 additions & 0 deletions lib/xcodeproj/project/object/native_target.rb
Expand Up @@ -552,6 +552,49 @@ def add_resources(resource_file_references)
end
end

# Adds on demand resources to the resources build phase of the target.
#
# @param {String => [Array<PBXFileReference>]} on_demand_resource_tag_files
# the files references of the on demand resources to add to the target keyed by the tag.
#
# @return [void]
#
def add_on_demand_resources(on_demand_resource_tag_files)
on_demand_resource_tag_files.each do |tag, file_refs|
file_refs.each do |file_ref|
if resources_build_phase.include?(file_ref)
existing_build_file = resources_build_phase.build_file(file_ref)
existing_build_file.settings ||= {}
existing_build_file.settings['ASSET_TAGS'] ||= []
existing_build_file.settings['ASSET_TAGS'] << tag
Copy link
Contributor Author

Choose a reason for hiding this comment

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

a single resource may be part of multiple tags. this is tested below.

existing_build_file.settings['ASSET_TAGS'].uniq!
next
end
build_file = resources_build_phase.add_file_reference(file_ref, true)
build_file.settings = (build_file.settings ||= {}).merge('ASSET_TAGS' => [tag])
end
end
end

# Remove on demand resources from the resources build phase of the target.
#
# @param {String => [Array<PBXFileReference>]} on_demand_resource_tag_files
# the files references of the on demand resources to add to the target keyed by the tag.
#
# @return [void]
#
def remove_on_demand_resources(on_demand_resource_tag_files)
on_demand_resource_tag_files.each do |tag, file_refs|
file_refs.each do |file_ref|
build_file = resources_build_phase.build_file(file_ref)
next if build_file.nil?
asset_tags = build_file.settings['ASSET_TAGS']
asset_tags.delete(tag)
resources_build_phase.remove_file_reference(file_ref) if asset_tags.empty?
end
end
end

# Finds or creates the headers build phase of the target.
#
# @note A target should have only one headers build phase.
Expand Down
59 changes: 58 additions & 1 deletion spec/project/object/native_target_spec.rb
Expand Up @@ -947,7 +947,64 @@ module ProjectSpecs
build_files.first.settings.should.be.nil
end

it 'de-duplicates added sources files' do
it 'adds on demand resources' do
apple_ref = @project.main_group.new_file('apple.png')
orange_ref = @project.main_group.new_file('orange.png')
bear_ref = @project.main_group.new_file('bear.png')
frog_ref = @project.main_group.new_file('frog.png')
on_demand_resources = { 'fruits' => [apple_ref, orange_ref], 'animals' => [bear_ref, frog_ref], 'mixed' => [apple_ref, bear_ref] }
@target.add_on_demand_resources(on_demand_resources)
build_files = @target.resources_build_phase.files
build_files.count.should == 4
build_files.map(&:display_name).should == %w(apple.png orange.png bear.png frog.png)
build_files[0].settings.should == { 'ASSET_TAGS' => %w(fruits mixed) }
build_files[1].settings.should == { 'ASSET_TAGS' => ['fruits'] }
build_files[2].settings.should == { 'ASSET_TAGS' => %w(animals mixed) }
build_files[3].settings.should == { 'ASSET_TAGS' => ['animals'] }
end

it 'does not add a duplicate file reference to the resources build phase for on demand resources' do
apple_ref = @project.main_group.new_file('apple.png')
@target.resources_build_phase.add_file_reference(apple_ref)
on_demand_resources = { 'fruits' => [apple_ref] }
@target.add_on_demand_resources(on_demand_resources)
build_files = @target.resources_build_phase.files
build_files.count.should == 1
build_files.map(&:display_name).should == %w(apple.png)
build_files[0].settings.should == { 'ASSET_TAGS' => %w(fruits) }
end

it 'removes on demand resources' do
apple_ref = @project.main_group.new_file('apple.png')
orange_ref = @project.main_group.new_file('orange.png')
bear_ref = @project.main_group.new_file('bear.png')
frog_ref = @project.main_group.new_file('frog.png')
on_demand_resources = { 'fruits' => [apple_ref, orange_ref], 'animals' => [bear_ref, frog_ref], 'mixed' => [apple_ref, bear_ref] }
@target.add_on_demand_resources(on_demand_resources)
@target.resources_build_phase.files.count.should == 4
@target.remove_on_demand_resources(on_demand_resources)
@target.resources_build_phase.files.count.should == 0
end

it 'removes specific on demand resource tag' do
apple_ref = @project.main_group.new_file('apple.png')
orange_ref = @project.main_group.new_file('orange.png')
bear_ref = @project.main_group.new_file('bear.png')
frog_ref = @project.main_group.new_file('frog.png')
on_demand_resources = { 'fruits' => [apple_ref, orange_ref], 'animals' => [bear_ref, frog_ref], 'mixed' => [apple_ref, bear_ref] }
@target.add_on_demand_resources(on_demand_resources)
@target.resources_build_phase.files.count.should == 4
@target.remove_on_demand_resources('fruits' => [apple_ref])
build_files = @target.resources_build_phase.files
build_files.count.should == 4
build_files.map(&:display_name).should == %w(apple.png orange.png bear.png frog.png)
build_files[0].settings.should == { 'ASSET_TAGS' => ['mixed'] }
build_files[1].settings.should == { 'ASSET_TAGS' => ['fruits'] }
build_files[2].settings.should == { 'ASSET_TAGS' => %w(animals mixed) }
build_files[3].settings.should == { 'ASSET_TAGS' => ['animals'] }
end

it 'de-duplicates added source files' do
ref = @project.main_group.new_file('Class.h')
new_build_files = @target.add_file_references([ref], '-fobjc-arc')
@target.add_file_references([ref], '-fobjc-arc')
Expand Down