Skip to content

Commit

Permalink
Move and use BuildType class
Browse files Browse the repository at this point in the history
  • Loading branch information
dnkoutso committed Aug 21, 2019
1 parent 9bafc2f commit 8a7bbea
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/cocoapods-core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Informative < PlainInformative; end
autoload :Specification, 'cocoapods-core/specification'
autoload :StandardError, 'cocoapods-core/standard_error'
autoload :YAMLHelper, 'cocoapods-core/yaml_helper'
autoload :BuildType, 'cocoapods-core/build_type'

# TODO: Fix
#
Expand Down
143 changes: 143 additions & 0 deletions lib/cocoapods-core/build_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
module Pod
class BuildType
# @return [Array<Symbol>] known packaging options.
#
KNOWN_PACKAGING_OPTIONS = %i(library framework).freeze

# @return [Array<Symbol>] known linking options.
#
KNOWN_LINKAGE_OPTIONS = %i(static dynamic).freeze

# @return [Symbol] the packaging for this build type, one of #KNOWN_PACKAGING_OPTIONS
#
attr_reader :packaging

# @return [Symbol] the linkage for this build type, one of #KNOWN_LINKAGE_OPTIONS
#
attr_reader :linkage

attr_reader :hash

def initialize(linkage: :static, packaging: :library)
unless KNOWN_LINKAGE_OPTIONS.include?(linkage)
raise ArgumentError, "Invalid linkage option #{linkage.inspect}, valid options are #{KNOWN_LINKAGE_OPTIONS.inspect}"
end
unless KNOWN_PACKAGING_OPTIONS.include?(packaging)
raise ArgumentError, "Invalid packaging option #{packaging.inspect}, valid options are #{KNOWN_PACKAGING_OPTIONS.inspect}"
end
@packaging = packaging
@linkage = linkage
@hash = packaging.hash ^ linkage.hash
end

# @param [Specification] spec
# the specification to infer the build type from
#
# @param [Boolean] host_build_type
# the host target definition build type.
#
# @return [BuildType] the appropriate build type for the given spec,
# also based on host target definition build type.
#
def self.infer_from_spec(spec, host_build_type: BuildType.static_library)
if host_build_type.framework?
root_spec = spec && spec.root
if root_spec && root_spec.static_framework
static_framework
else
host_build_type
end
else
static_library
end
end

# @return [BuildType] the build type for a dynamic library
def self.dynamic_library
new(:linkage => :dynamic, :packaging => :library)
end

# @return [BuildType] the build type for a static library
#
def self.static_library
new(:linkage => :static, :packaging => :library)
end

# @return [BuildType] the build type for a dynamic framework
#
def self.dynamic_framework
new(:linkage => :dynamic, :packaging => :framework)
end

# @return [BuildType] the build type for a static framework
#
def self.static_framework
new(:linkage => :static, :packaging => :framework)
end

# @return [Boolean] whether the target is built dynamically
#
def dynamic?
linkage == :dynamic
end

# @return [Boolean] whether the target is built statically
#
def static?
linkage == :static
end

# @return [Boolean] whether the target is built as a framework
#
def framework?
packaging == :framework
end

# @return [Boolean] whether the target is built as a library
#
def library?
packaging == :library
end

# @return [Boolean] whether the target is built as a dynamic framework
#
def dynamic_framework?
dynamic? && framework?
end

# @return [Boolean] whether the target is built as a dynamic library
#
def dynamic_library?
dynamic? && library?
end

# @return [Boolean] whether the target is built as a static framework
#
def static_framework?
static? && framework?
end

# @return [Boolean] whether the target is built as a static library
#
def static_library?
static? && library?
end

def to_s
"#{linkage} #{packaging}"
end

def to_hash
{ :linkage => linkage, :packaging => packaging }
end

def inspect
"#<#{self.class} linkage=#{linkage} packaging=#{packaging}>"
end

def ==(other)
linkage == other.linkage &&
packaging == other.packaging
end
end
end
22 changes: 14 additions & 8 deletions lib/cocoapods-core/podfile/target_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,19 @@ def set_inhibit_warnings_for_pod(pod_name, should_inhibit)
# 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!.
#
# @return [Hash]
# @return [BuildType]
#
def build_type
if root?
get_hash_value('uses_frameworks', :linkage => :static, :packaging => :library)
value = get_hash_value('uses_frameworks', root? ? BuildType.static_library : parent.build_type)
case value
when true, false
value ? BuildType.dynamic_framework : BuildType.static_library
when Hash
BuildType.new(:linkage => value.fetch(:linkage), :packaging => value.fetch(:packaging))
when BuildType
value
else
get_hash_value('uses_frameworks', parent.build_type)
raise ArgumentError, "Got `#{value.inspect}`, should be a boolean, hash or BuildType."
end
end

Expand All @@ -391,13 +397,13 @@ def build_type
def use_frameworks!(option = true)
value = case option
when true, false
{ :linkage => option ? :dynamic : :static, :packaging => option ? :framework : :library }
option ? BuildType.dynamic_framework : BuildType.static_library
when Hash
option.merge(:packaging => :framework)
BuildType.new(:linkage => option.fetch(:linkage), :packaging => :framework)
else
raise ArgumentError, "Got `#{option.inspect}`, should be a boolean or hash."
end
set_hash_value('uses_frameworks', value)
set_hash_value('uses_frameworks', value.to_hash)
end

# @return [Bool] whether the target definition pods should be built as frameworks.
Expand All @@ -406,7 +412,7 @@ def uses_frameworks?
if internal_hash['uses_frameworks'].nil?
root? ? false : parent.uses_frameworks?
else
build_type[:packaging] == :framework
build_type.framework?
end
end

Expand Down
75 changes: 75 additions & 0 deletions spec/build_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require File.expand_path('../spec_helper', __FILE__)

module Pod
describe BuildType do
describe '#initialize' do
it 'returns static library by default' do
BuildType.new.should == BuildType.static_library
end

it 'allows specifying linkage' do
BuildType.new(:linkage => :dynamic).should == BuildType.dynamic_library
end

it 'allows specifying packaging' do
BuildType.new(:packaging => :framework).should == BuildType.static_framework
end

it 'raises when given an unknown linkage' do
-> { BuildType.new(:linkage => :foo) }.should.raise(ArgumentError).
message.should.include? 'Invalid linkage option :foo, valid options are [:static, :dynamic]'
end
end

describe 'convenience factory methods' do
it '#dynamic_library' do
BuildType.dynamic_library.should == BuildType.new(:linkage => :dynamic, :packaging => :library)
end

it '#static_library' do
BuildType.static_library.should == BuildType.new(:linkage => :static, :packaging => :library)
end

it '#dynamic_framework' do
BuildType.dynamic_framework.should == BuildType.new(:linkage => :dynamic, :packaging => :framework)
end

it '#static_framework' do
BuildType.static_framework.should == BuildType.new(:linkage => :static, :packaging => :framework)
end
end

describe '.infer_from_spec' do
it 'infers the build type' do
BuildType.infer_from_spec(nil, :host_build_type => BuildType.static_library).should == BuildType.static_library
BuildType.infer_from_spec(nil, :host_build_type => BuildType.dynamic_framework).should == BuildType.dynamic_framework

BuildType.infer_from_spec(stub('spec', :root => stub('root_spec', :static_framework => true)), :host_build_type => BuildType.static_library).
should == BuildType.static_library
BuildType.infer_from_spec(stub('spec', :root => stub('root_spec', :static_framework => false)), :host_build_type => BuildType.static_library).
should == BuildType.static_library
BuildType.infer_from_spec(stub('spec', :root => stub('root_spec', :static_framework => true)), :host_build_type => BuildType.static_framework).
should == BuildType.static_framework
BuildType.infer_from_spec(stub('spec', :root => stub('root_spec', :static_framework => false)), :host_build_type => BuildType.dynamic_framework).
should == BuildType.dynamic_framework
end
end

describe '#==' do
it 'compares equal build types as equal' do
BuildType.new(:linkage => :dynamic, :packaging => :library).should == BuildType.new(:linkage => :dynamic, :packaging => :library)
end

it 'compares unequal build types as unequal' do
BuildType.new(:linkage => :dynamic, :packaging => :framework).should != BuildType.new(:linkage => :dynamic, :packaging => :library)
BuildType.new(:linkage => :static, :packaging => :library).should != BuildType.new(:linkage => :dynamic, :packaging => :library)
end
end

describe '#to_s' do
it 'returns a readable representation' do
BuildType.static_framework.to_s.should == 'static framework'
end
end
end
end
15 changes: 15 additions & 0 deletions spec/fixtures/Podfile_UsesFrameworksBoolean.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
installation_method:
name: cocoapods
sources:
- https://cdn.cocoapods.org/
target_definitions:
- abstract: true
children:
- children:
- abstract: false
inheritance: search_paths
name: SampleAppTests
name: SampleApp
uses_frameworks: true
name: Pods
17 changes: 17 additions & 0 deletions spec/fixtures/Podfile_UsesFrameworksHash.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
installation_method:
name: cocoapods
sources:
- https://cdn.cocoapods.org/
target_definitions:
- abstract: true
children:
- children:
- abstract: false
inheritance: search_paths
name: SampleAppTests
name: SampleApp
uses_frameworks:
:linkage: :dynamic
:packaging: :framework
name: Pods
15 changes: 10 additions & 5 deletions spec/podfile/target_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -448,22 +448,27 @@ module Pod
end

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

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

it 'returns the configured frameworks build type' do
it 'returns the configured dynamic framework build type' do
@parent.use_frameworks!(:linkage => :dynamic)
@parent.build_type.should == BuildType.dynamic_framework
end

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

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

#--------------------------------------#
Expand Down
14 changes: 14 additions & 0 deletions spec/podfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,20 @@ module Pod
ruby_podfile.to_hash.should == yaml_podfile.to_hash
end

it 'can properly parse a boolean uses_frameworks value from a YAML file' do
podfile = Podfile.from_file(fixture('Podfile_UsesFrameworksBoolean.yaml'))
target_definition = podfile.target_definitions['SampleApp']
target_definition.uses_frameworks?.should.be.true
target_definition.build_type.should == BuildType.dynamic_framework
end

it 'can properly parse a hash uses_frameworks value from a YAML file' do
podfile = Podfile.from_file(fixture('Podfile_UsesFrameworksHash.yaml'))
target_definition = podfile.target_definitions['SampleApp']
target_definition.uses_frameworks?.should.be.true
target_definition.build_type.should == BuildType.dynamic_framework
end

it "raises if the given initialization file doesn't exists" do
should.raise Informative do
Podfile.from_file('Missing-file')
Expand Down

0 comments on commit 8a7bbea

Please sign in to comment.