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

Project Helper for different Target Types #260

Merged
merged 9 commits into from
Apr 7, 2015
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

## Master

##### Enhancements
Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't go below 0.23.1, but instead master.


* Return a list of project targets including only native targets by
`native_targets`.
[Marc Boquet](https://github.com/apalancat)
[Marius Rackwitz](https://github.com/mrackwitz)
[Xcodeproj#256](https://github.com/CocoaPods/Xcodeproj/pull/256)

* `ProjectHelper`: Allow to create aggregate targets.
[Marius Rackwitz](https://github.com/mrackwitz)
[Xcodeproj#260](https://github.com/CocoaPods/Xcodeproj/pull/260)

* `ProjectHelper`: Give optional parameter of `configuration_list`
and `common_build_settings` the default value `nil`.
[Marius Rackwitz](https://github.com/mrackwitz)
[Xcodeproj#260](https://github.com/CocoaPods/Xcodeproj/pull/260)

#### Bug Fixes

* Save xcconfig files also if only the includes where modified by fixing the
Expand Down
36 changes: 35 additions & 1 deletion lib/xcodeproj/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,22 @@ def reference_for_path(absolute_path)
end
end

# @return [ObjectList<PBXNativeTarget>] A list of all the targets in the
# @return [ObjectList<AbstractTarget>] A list of all the targets in the
# project.
#
def targets
root_object.targets
end

# @return [ObjectList<PBXNativeTarget>] A list of all the targets in the
# project excluding aggregate targets.
#
def native_targets
root_object.targets.select do |target|
Copy link
Member

Choose a reason for hiding this comment

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

root_object.targets.grep(PBXNativeTarget)

target.is_a? PBXNativeTarget
end
end

# @return [PBXGroup] The group which holds the product file references.
#
def products_group
Expand Down Expand Up @@ -598,6 +607,31 @@ def new_resources_bundle(name, platform, product_group = nil)
ProjectHelper.new_resources_bundle(self, name, platform, product_group)
end

# Creates a new target and adds it to the project.
#
# The target is configured for the given platform and its file reference it
# is added to the {products_group}.
#
# The target is pre-populated with common build settings, and the
# appropriate Framework according to the platform is added to to its
# Frameworks phase.
#
# @param [String] name
# the name of the target.
#
# @param [Array<AbstractTarget>] target_dependencies
# targets, which should be added as dependencies.
#
# @return [PBXNativeTarget] the target.
#
def new_aggregate_target(name, target_dependencies = [])
ProjectHelper.new_aggregate_target(self, name).tap do |aggregate_target|
target_dependencies.each do |dep|
aggregate_target.add_dependency(dep)
end
end
end

# Adds a new build configuration to the project and populates its with
# default settings according to the provided type.
#
Expand Down
24 changes: 22 additions & 2 deletions lib/xcodeproj/project/project_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ def self.new_resources_bundle(project, name, platform, product_group)
target
end

# Creates a new aggregate target and adds it to the project.
#
# The target is configured for the given platform.
#
# @param [Project] project
# the project to which the target should be added.
#
# @param [String] name
# the name of the aggregate target.
#
# @return [PBXAggregateTarget] the target.
#
def self.new_aggregate_target(project, name)
target = project.new(PBXAggregateTarget)
project.targets << target
target.name = name
target.build_configuration_list = configuration_list(project)
target
end

# @!group Private Helpers

#-----------------------------------------------------------------------#
Expand All @@ -148,7 +168,7 @@ def self.new_resources_bundle(project, name, platform, product_group)
#
# @return [XCConfigurationList] the generated configuration list.
#
def self.configuration_list(project, platform, deployment_target = nil, target_product_type, language)
def self.configuration_list(project, platform = nil, deployment_target = nil, target_product_type = nil, language = nil)
cl = project.new(XCConfigurationList)
cl.default_configuration_is_visible = '0'
cl.default_configuration_name = 'Release'
Expand Down Expand Up @@ -199,7 +219,7 @@ def self.configuration_list(project, platform, deployment_target = nil, target_p
#
# @return [Hash] The common build settings
#
def self.common_build_settings(type, platform, deployment_target = nil, target_product_type = nil, language = :objc)
def self.common_build_settings(type, platform = nil, deployment_target = nil, target_product_type = nil, language = :objc)
target_product_type = (Constants::PRODUCT_TYPE_UTI.find { |_, v| v == target_product_type } || [target_product_type || :application])[0]
common_settings = Constants::COMMON_BUILD_SETTINGS

Expand Down
14 changes: 14 additions & 0 deletions spec/project/project_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ module ProjectSpecs

target.build_phases.map(&:isa).sort.should == %w(PBXFrameworksBuildPhase PBXResourcesBuildPhase PBXSourcesBuildPhase)
end

it 'creates a new aggregate target' do
target = @helper.new_aggregate_target(@project, 'Pods')
target.name.should == 'Pods'
target.product_name.should.be.nil

target.build_configuration_list.should.not.be.nil
configurations = target.build_configuration_list.build_configurations
configurations.map(&:name).sort.should == %w(Debug Release)

@project.targets.should.include target

target.build_phases.count.should == 0
end
end

#-------------------------------------------------------------------------#
Expand Down
20 changes: 18 additions & 2 deletions spec/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,17 @@ module ProjectSpecs
end

it 'returns the targets' do
target = @project.new_target(:static_library, 'Pods', :ios).product_reference
@project.products.should.include?(target)
target = @project.new_target(:static_library, 'Pods', :ios)
@project.targets.should.include?(target)
end

it 'returns the native targets' do
native_target = @project.new_target(:static_library, 'Pods', :ios)
@project.new_aggregate_target('Trees')
@project.targets << @project.new(PBXLegacyTarget)
native_targets = @project.native_targets
native_targets.should.include?(native_target)
native_targets.count.should == 1
end

it 'returns the products group' do
Expand Down Expand Up @@ -412,6 +421,13 @@ module ProjectSpecs
target.product_type.should == 'com.apple.product-type.bundle'
end

it 'creates a new aggregate target' do
native_target = @project.new_target(:static_library, 'BananaLib', :ios, '6.0')
aggregate_target = @project.new_aggregate_target('Pods', [native_target])
aggregate_target.name.should == 'Pods'
aggregate_target.dependencies.first.target.should == native_target
end

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

describe '#add_build_configuration' do
Expand Down