Skip to content

Commit

Permalink
Add support for adding and removing on demand resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnkoutso committed Jul 23, 2021
1 parent 67f5aa0 commit e0321e9
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
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
Original file line number Diff line number Diff line change
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

0 comments on commit e0321e9

Please sign in to comment.