From 35ac099b9d9b64f26f69d909dcef78c664cb48a2 Mon Sep 17 00:00:00 2001 From: Marc Boquet Date: Wed, 21 May 2014 17:21:46 +0200 Subject: [PATCH 1/9] Return a list of project targets excluding aggregate targets. --- lib/xcodeproj/project.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/xcodeproj/project.rb b/lib/xcodeproj/project.rb index 00022d84d..1880ff80b 100644 --- a/lib/xcodeproj/project.rb +++ b/lib/xcodeproj/project.rb @@ -469,13 +469,22 @@ def reference_for_path(absolute_path) end end - # @return [ObjectList] A list of all the targets in the + # @return [ObjectList] A list of all the targets in the # project. # def targets root_object.targets end + # @return [ObjectList] A list of all the targets in the + # project excluding aggregate targets. + # + def native_targets + root_object.targets.reject do |target| + target.is_a? PBXAggregateTarget + end + end + # @return [PBXGroup] The group which holds the product file references. # def products_group From b1d28ccd4cc5ce745349663384b4e601ce0c7106 Mon Sep 17 00:00:00 2001 From: Marius Rackwitz Date: Mon, 6 Apr 2015 14:31:31 +0200 Subject: [PATCH 2/9] [Spec] Test Project#targets That was actually supposed to be tested. --- spec/project_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/project_spec.rb b/spec/project_spec.rb index b789cd882..9c8456261 100644 --- a/spec/project_spec.rb +++ b/spec/project_spec.rb @@ -311,8 +311,8 @@ 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 products group' do From 7e2e7021d2ea0e343bfeb097de1400ad1b459498 Mon Sep 17 00:00:00 2001 From: Marius Rackwitz Date: Mon, 6 Apr 2015 15:09:40 +0200 Subject: [PATCH 3/9] [ProjectHelper] Make argument platform for optional --- lib/xcodeproj/project/project_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/xcodeproj/project/project_helper.rb b/lib/xcodeproj/project/project_helper.rb index c6c07b1be..de352789a 100644 --- a/lib/xcodeproj/project/project_helper.rb +++ b/lib/xcodeproj/project/project_helper.rb @@ -148,7 +148,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, language) cl = project.new(XCConfigurationList) cl.default_configuration_is_visible = '0' cl.default_configuration_name = 'Release' @@ -199,7 +199,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 From 60c65751ff90e164009903779accc54c984f3f9c Mon Sep 17 00:00:00 2001 From: Marius Rackwitz Date: Mon, 6 Apr 2015 15:13:08 +0200 Subject: [PATCH 4/9] [ProjectHelper] Make last arguments of #configuration_list optional --- lib/xcodeproj/project/project_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/xcodeproj/project/project_helper.rb b/lib/xcodeproj/project/project_helper.rb index de352789a..7ca5d930d 100644 --- a/lib/xcodeproj/project/project_helper.rb +++ b/lib/xcodeproj/project/project_helper.rb @@ -148,7 +148,7 @@ def self.new_resources_bundle(project, name, platform, product_group) # # @return [XCConfigurationList] the generated configuration list. # - def self.configuration_list(project, platform = nil, 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' From 2a1c60646f267fd81c5dbd020ea2247189c1f589 Mon Sep 17 00:00:00 2001 From: Marius Rackwitz Date: Mon, 6 Apr 2015 15:30:55 +0200 Subject: [PATCH 5/9] Add helper to create aggregate targets --- lib/xcodeproj/project.rb | 25 +++++++++++++++++++++++++ lib/xcodeproj/project/project_helper.rb | 20 ++++++++++++++++++++ spec/project/project_helper_spec.rb | 14 ++++++++++++++ spec/project_spec.rb | 7 +++++++ 4 files changed, 66 insertions(+) diff --git a/lib/xcodeproj/project.rb b/lib/xcodeproj/project.rb index 1880ff80b..b5c520117 100644 --- a/lib/xcodeproj/project.rb +++ b/lib/xcodeproj/project.rb @@ -607,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] 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. # diff --git a/lib/xcodeproj/project/project_helper.rb b/lib/xcodeproj/project/project_helper.rb index 7ca5d930d..0a39a7eb2 100644 --- a/lib/xcodeproj/project/project_helper.rb +++ b/lib/xcodeproj/project/project_helper.rb @@ -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 #-----------------------------------------------------------------------# diff --git a/spec/project/project_helper_spec.rb b/spec/project/project_helper_spec.rb index b682cf8da..ed43fc923 100644 --- a/spec/project/project_helper_spec.rb +++ b/spec/project/project_helper_spec.rb @@ -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 #-------------------------------------------------------------------------# diff --git a/spec/project_spec.rb b/spec/project_spec.rb index 9c8456261..7c00680ed 100644 --- a/spec/project_spec.rb +++ b/spec/project_spec.rb @@ -412,6 +412,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 From 56815655d1d77380676de0c7bc2dabbbcea06880 Mon Sep 17 00:00:00 2001 From: Marius Rackwitz Date: Mon, 6 Apr 2015 15:31:33 +0200 Subject: [PATCH 6/9] [Spec] Add spec for Project#native_targets --- spec/project_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/project_spec.rb b/spec/project_spec.rb index 7c00680ed..ed08db47c 100644 --- a/spec/project_spec.rb +++ b/spec/project_spec.rb @@ -315,6 +315,14 @@ module ProjectSpecs @project.targets.should.include?(target) end + it 'returns the native targets' do + native_target = @project.new_target(:static_library, 'Pods', :ios) + aggregate_target = @project.new_aggregate_target('Trees') + native_targets = @project.native_targets + native_targets.should.include?(native_target) + native_targets.should.not.include?(aggregate_target) + end + it 'returns the products group' do g = @project.products_group g.class.should == PBXGroup From 4f769cc55d3f503dee187b04698f463aa7b0d3c1 Mon Sep 17 00:00:00 2001 From: Marius Rackwitz Date: Mon, 6 Apr 2015 15:40:01 +0200 Subject: [PATCH 7/9] Select Project#native_targets more fail-safe --- lib/xcodeproj/project.rb | 4 ++-- spec/project_spec.rb | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/xcodeproj/project.rb b/lib/xcodeproj/project.rb index b5c520117..f2d99f230 100644 --- a/lib/xcodeproj/project.rb +++ b/lib/xcodeproj/project.rb @@ -480,8 +480,8 @@ def targets # project excluding aggregate targets. # def native_targets - root_object.targets.reject do |target| - target.is_a? PBXAggregateTarget + root_object.targets.select do |target| + target.is_a? PBXNativeTarget end end diff --git a/spec/project_spec.rb b/spec/project_spec.rb index ed08db47c..c450e7872 100644 --- a/spec/project_spec.rb +++ b/spec/project_spec.rb @@ -317,10 +317,11 @@ module ProjectSpecs it 'returns the native targets' do native_target = @project.new_target(:static_library, 'Pods', :ios) - aggregate_target = @project.new_aggregate_target('Trees') + @project.new_aggregate_target('Trees') + @project.targets << @project.new(PBXLegacyTarget) native_targets = @project.native_targets native_targets.should.include?(native_target) - native_targets.should.not.include?(aggregate_target) + native_targets.count.should == 1 end it 'returns the products group' do From c4d6c97ff6ba669cfab49e0282b2f0fdb69bd5b4 Mon Sep 17 00:00:00 2001 From: Marius Rackwitz Date: Mon, 6 Apr 2015 15:52:18 +0200 Subject: [PATCH 8/9] [CHANGELOG] Add entry for #256 --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f71c942b..a7fecfaf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## Master +##### Enhancements + +* 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) + #### Bug Fixes * Save xcconfig files also if only the includes where modified by fixing the From 4c2f4a2e8841e435a2af109832a7c7d8a18e36bd Mon Sep 17 00:00:00 2001 From: Marius Rackwitz Date: Mon, 6 Apr 2015 15:52:29 +0200 Subject: [PATCH 9/9] [CHANGELOG] Add entry for #260 --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a7fecfaf2..3d32298fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,15 @@ [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