Skip to content

Commit

Permalink
Merge pull request #690 from dnkoutso/odr_dsl_support
Browse files Browse the repository at this point in the history
Add support for `on_demand_resources` DSL.
  • Loading branch information
dnkoutso authored Jul 24, 2021
2 parents 366b8fb + 4a0f5f8 commit 65301d4
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 3 deletions.
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 `on_demand_resources` DSL.
[JunyiXie](https://github.com/JunyiXie)
[Dimitris Koutsogiorgas](https://github.com/dnkoutso)
[#690](https://github.com/CocoaPods/Core/pull/690)

* Add `before_headers` and `after_headers` execution position DSL.
[Dimitris Koutsogiorgas](https://github.com/dnkoutso)
[#686](https://github.com/CocoaPods/Core/pull/686)
Expand Down
26 changes: 25 additions & 1 deletion lib/cocoapods-core/specification/consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,13 @@ def user_target_xcconfig
#
spec_attr_accessor :vendored_libraries

# @return [Hash{String=>String}]] hash where the keys are the names of
# @return [Hash{String => Array<String>}] hash where the keys are the tags of
# the on demand resources and the values are their relative file
# patterns.
#
spec_attr_accessor :on_demand_resources

# @return [Hash{String=>String}]] hash where the keys are the names of
# the resource bundles and the values are their relative file
# patterns.
#
Expand Down Expand Up @@ -455,6 +461,24 @@ def _prepare_scheme(value)
Specification.convert_keys_to_symbol(value, :recursive => false) if value && value.is_a?(Hash)
end

# Ensures that the file patterns of the on demand resources are contained in
# an array.
#
# @param [String, Array, Hash] value.
# The value of the attribute as specified by the user.
#
# @return [Hash] the on demand resources1.
#
def _prepare_on_demand_resources(value)
result = {}
if value
value.each do |key, patterns|
result[key] = [*patterns].compact
end
end
result
end

# Ensures that the file patterns of the resource bundles are contained in
# an array.
#
Expand Down
26 changes: 26 additions & 0 deletions lib/cocoapods-core/specification/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,32 @@ def dependency=(args)

#------------------#

# @!method on_demand_resources=(on_demand_resources)
#
# A hash of on_demand_resources that should be copied into the target bundle. Resources specified here
# will automatically become part of the resources build phase of the target.
#
# @example
#
# s.on_demand_resources = {
# 'Tag1' => 'file1.png'
# }
#
# s.on_demand_resources = {
# 'Tag1' => ['file1.png', 'file2.png']
# }
#
# @param [Hash{String=>String}, Hash{String=>Array<String>}] on_demand_resources
# The on_demand_resources shipped with the Pod.
#
attribute :on_demand_resources,
:types => [String, Array],
:container => Hash,
:file_patterns => true,
:singularize => true

#------------------#

# @!method resource_bundles=(*resource_bundles)
#
# This attribute allows to define the name and the file of the resource
Expand Down
2 changes: 1 addition & 1 deletion lib/cocoapods-core/specification/linter/analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def validate_file_patterns
# Check empty subspec attributes
#
def check_if_spec_is_empty
methods = %w( source_files resources resource_bundles preserve_paths
methods = %w( source_files on_demand_resources resources resource_bundles preserve_paths
dependencies vendored_libraries vendored_frameworks )
empty_patterns = methods.all? { |m| consumer.send(m).empty? }
empty = empty_patterns && consumer.spec.subspecs.empty?
Expand Down
24 changes: 24 additions & 0 deletions spec/specification/consumer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,30 @@ module Pod

#------------------#

it 'returns the on demand resources specified as a string wrapped in an array' do
@spec.on_demand_resources = { 'Maps' => 'MapView/Map/Resources/*.png' }
@consumer.on_demand_resources.should == { 'Maps' => ['MapView/Map/Resources/*.png'] }
end

it 'returns the on demand resources specified as an array wrapped in an array' do
@spec.on_demand_resources = { 'Levels' => %w[Levels/Level1/Resources/*.png Levels/Level2/Resources/*.png] }
@consumer.on_demand_resources.should == { 'Levels' => %w[Levels/Level1/Resources/*.png Levels/Level2/Resources/*.png] }
end

it 'handles multi-platform on demand resources' do
@spec.ios.on_demand_resources = { 'Maps' => 'MapView/Map/Resources/*.png' }
@consumer.on_demand_resources.should == { 'Maps' => ['MapView/Map/Resources/*.png'] }
end

it 'merges multi platform on demand resources if needed' do
@spec.on_demand_resources = { 'Maps' => 'MapView/Map/Resources/*.png' }
@spec.ios.on_demand_resources = { 'Maps-iOS' => 'MapView/Map/iOS/Resources/*.png' }
@consumer.on_demand_resources.should == {
'Maps' => ['MapView/Map/Resources/*.png'],
'Maps-iOS' => ['MapView/Map/iOS/Resources/*.png'],
}
end

it 'returns the resource bundles' do
@spec.resource_bundles = { 'MapBox' => 'MapView/Map/Resources/*.png' }
@consumer.resource_bundles.should == { 'MapBox' => ['MapView/Map/Resources/*.png'] }
Expand Down
12 changes: 11 additions & 1 deletion spec/specification/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,16 @@ module Pod
@spec.attributes_hash['vendored_libraries'].should == ['libProj4.a']
end

it 'allows to specify the on demand resources shipped with the Pod' do
@spec.on_demand_resources = { 'Maps' => 'MapView/Map/Resources/*.png' }
@spec.attributes_hash['on_demand_resources'].should == { 'Maps' => 'MapView/Map/Resources/*.png' }
end

it 'allows to specify the on demand resources shipped with the Pod as an array' do
@spec.on_demand_resources = { 'Levels' => %w[Levels/Level1/Resources/*.png Levels/Level2/Resources/*.png] }
@spec.attributes_hash['on_demand_resources'].should == { 'Levels' => %w[Levels/Level1/Resources/*.png Levels/Level2/Resources/*.png] }
end

it 'allows to specify the resources bundles shipped with the Pod' do
@spec.resource_bundles = { 'MapBox' => 'MapView/Map/Resources/*.png' }
@spec.attributes_hash['resource_bundles'].should == { 'MapBox' => 'MapView/Map/Resources/*.png' }
Expand Down Expand Up @@ -599,7 +609,7 @@ module Pod
spec.should.respond_to(attr.writer_name)
end
singularized.map { |attr| attr.name.to_s }.sort.should == %w(
authors compiler_flags default_subspecs frameworks libraries
authors compiler_flags default_subspecs frameworks libraries on_demand_resources
preserve_paths resource_bundles resources screenshots script_phases
swift_versions vendored_frameworks vendored_libraries weak_frameworks
)
Expand Down
1 change: 1 addition & 0 deletions spec/specification/linter/analyzer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ module Pod
it 'checks if a specification is empty' do
consumer = Specification::Consumer
consumer.any_instance.stubs(:source_files).returns([])
consumer.any_instance.stubs(:on_demand_resources).returns({})
consumer.any_instance.stubs(:resources).returns({})
consumer.any_instance.stubs(:resource_bundles).returns([])
consumer.any_instance.stubs(:preserve_paths).returns([])
Expand Down

0 comments on commit 65301d4

Please sign in to comment.