Skip to content

Commit

Permalink
Expand use_frameworks! DSL to accept linkage style.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnkoutso committed Aug 11, 2019
1 parent 0a6dea9 commit e07f8f7
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

##### Enhancements

* Expand `use_frameworks!` DSL to accept linkage style.
[Dimitris Koutsogiorgas](https://github.com/dnkoutso)
[#581](https://github.com/CocoaPods/Core/pull/581)

* Extend `script_phase` DSL to support dependency file.
[Dimitris Koutsogiorgas](https://github.com/dnkoutso)
[#579](https://github.com/CocoaPods/Core/pull/579)
Expand Down
31 changes: 28 additions & 3 deletions lib/cocoapods-core/podfile/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -680,14 +680,39 @@ def use_modular_headers!
current_target_definition.use_modular_headers_for_all_pods = true
end

# Use frameworks instead of static libraries for Pods.
# Use frameworks instead of static libraries for Pods. When using frameworks, you may also specify the `:linkage`
# style to use, either `:static` or `:dynamic`.
#
# ------
#
# This attribute is inherited by child target definitions.
#
def use_frameworks!(flag = true)
current_target_definition.use_frameworks!(flag)
# @param [Boolean, Hash] option
# The option to use for configuring packaging and linkage style.
#
# @example
#
# target 'MyApp' do
# use_frameworks!
# pod 'AFNetworking', '~> 1.0'
# end
#
# @example
#
# target 'MyApp' do
# use_frameworks!(:linkage => :dynamic)
# pod 'AFNetworking', '~> 1.0'
# end
#
# target 'ZipApp' do
# use_frameworks!(:linkage => :static)
# pod 'SSZipArchive'
# end
#
# @return [void]
#
def use_frameworks!(option = true)
current_target_definition.use_frameworks!(option)
end

# Specifies the Swift version requirements this target definition supports.
Expand Down
43 changes: 33 additions & 10 deletions lib/cocoapods-core/podfile/target_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,25 +365,48 @@ def set_inhibit_warnings_for_pod(pod_name, should_inhibit)

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

# Sets whether the target definition should build a framework.
# The (desired) build type for the pods integrated in this target definition. Defaults to static libraries and can
# only be overridden through Pod::Podfile::DSL#use_frameworks!.
#
# @param [Bool] flag
# Whether a framework should be built.
# @return [Hash]
#
def build_type
if root?
get_hash_value('build_type', :linkage => :static, :packaging => :library)
else
get_hash_value('build_type', parent.build_type)
end
end

# Sets whether the target definition's pods should be built as frameworks.
#
# @param [Boolean, Hash] option
# Whether pods that are integrated in this target should be built as frameworks. If the option is a
# boolean then the value affects both packaging and linkage styles. If set to true, then dynamic frameworks
# are used and if it's set to false, then static libraries are used. If the option is a hash then
# `:framework` packaging is implied and the user configures the `:linkage` style to use.
#
# @return [void]
#
def use_frameworks!(flag = true)
set_hash_value('uses_frameworks', flag)
def use_frameworks!(option = true)
value = case option
when true, false
{ :linkage => option ? :dynamic : :static, :packaging => option ? :framework : :library }
when Hash
option.merge(:packaging => :framework)
else
raise ArgumentError, "Got `#{option.inspect}`, should be a boolean or hash."
end
set_hash_value('build_type', value)
end

# @return [Bool] whether the target definition should build
# a framework.
# @return [Bool] whether the target definition pods should be built as frameworks.
#
def uses_frameworks?
if internal_hash['uses_frameworks'].nil?
if internal_hash['build_type'].nil?
root? ? false : parent.uses_frameworks?
else
get_hash_value('uses_frameworks')
build_type[:packaging] == :framework
end
end

Expand Down Expand Up @@ -790,7 +813,7 @@ def store_script_phase(options)
script_phases
children
configuration_pod_whitelist
uses_frameworks
build_type
swift_version_requirements
inheritance
abstract
Expand Down
39 changes: 39 additions & 0 deletions spec/podfile/target_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,26 @@ module Pod
@parent.should.uses_frameworks?
end

it 'returns it does not use frameworks if its explicitly specified' do
@parent.use_frameworks!(false)
@parent.should.not.uses_frameworks?
end

it 'returns if it should use frameworks by specifying static linkage' do
@parent.use_frameworks!(:linkage => :static)
@parent.should.uses_frameworks?
end

it 'returns if it should use frameworks by specifying dynamic linkage' do
@parent.use_frameworks!(:linkage => :dynamic)
@parent.should.uses_frameworks?
end

it 'raises when an invalid type is used' do
e = lambda { @parent.use_frameworks!(1) }.should.raise ArgumentError
e.message.should == 'Got `1`, should be a boolean or hash.'
end

it 'inherits the option to use frameworks' do
@parent.use_frameworks!
@child.should.uses_frameworks?
Expand All @@ -427,6 +447,25 @@ module Pod
@child.should.not.uses_frameworks?
end

it 'returns the default build type' do
@parent.build_type.should == { :linkage => :static, :packaging => :library }
end

it 'returns the default frameworks build type' do
@parent.use_frameworks!
@parent.build_type.should == { :linkage => :dynamic, :packaging => :framework }
end

it 'returns the configured frameworks build type' do
@parent.use_frameworks!(:linkage => :static)
@parent.build_type.should == { :linkage => :static, :packaging => :framework }
end

it 'inherits the build type' do
@parent.use_frameworks!
@child.build_type.should == { :linkage => :dynamic, :packaging => :framework }
end

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

it 'raises if script phase is missing required key' do
Expand Down
4 changes: 2 additions & 2 deletions spec/podfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ module Pod
}
end

it 'includes uses frameworks' do
it 'includes build type' do
podfile = Podfile.new do
pod 'ObjectiveSugar'
use_frameworks!
Expand All @@ -344,7 +344,7 @@ module Pod
'name' => 'Pods',
'abstract' => true,
'dependencies' => ['ObjectiveSugar'],
'uses_frameworks' => true,
'build_type' => { :linkage => :dynamic, :packaging => :framework },
],
}
end
Expand Down

0 comments on commit e07f8f7

Please sign in to comment.