From affbeae85bd179c5e9a9a927a80f2aacc4d7cf7c Mon Sep 17 00:00:00 2001 From: Joachim Bengtsson Date: Thu, 12 Dec 2013 14:28:23 +0100 Subject: [PATCH 1/2] [pod] Whitelist pods for configurations An option to only link a specific pod to the target when building in a specific configuration. The exact syntax is still TBD, pending discussion in https://github.com/CocoaPods/CocoaPods/pull/1668 which implements the corresponding functionality. --- .../podfile/target_definition.rb | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/lib/cocoapods-core/podfile/target_definition.rb b/lib/cocoapods-core/podfile/target_definition.rb index 938470dc1..101aa5ab0 100644 --- a/lib/cocoapods-core/podfile/target_definition.rb +++ b/lib/cocoapods-core/podfile/target_definition.rb @@ -318,6 +318,48 @@ def inhibit_warnings_for_pod(pod_name) #--------------------------------------# + # Whether a specific pod should be linked to the target when building for + # a specific configuration. If a pod has not been explicitly whitelisted + # for any configuration, it is implicitly whitelisted. + # + # @param [String] pod_name + # The pod that we're querying about inclusion for in the given configuration. + # @param [String] configuration_name + # The configuration that we're querying about inclusion of the pod in + # + # @return [Bool] flag + # Whether the pod should be linked with the target + # + def is_pod_whitelisted_for_configuration?(pod_name, configuration_name) + found = false + configuration_pod_whitelist.each { |configuration, pods| + if pods.include?(pod_name) + found = true + return true if configuration.to_s == configuration_name.to_s + end + } + return !found + end + + # Whitelists a pod for a specific configuration. If a pod is whitelisted + # for any configuration, it will only be linked with the target in the + # configuration(s) specified. If it is not whitelisted for any configuration, + # it is implicitly included in all configurations. + # + # @param [String] pod_name + # The pod that should be included in the given configuration. + # @param [String] configuration_name + # The configuration that the pod should be included in + # + # @return [void] + # + def whitelist_pod_for_configuration(pod_name, configuration_name) + configuration_pod_whitelist[configuration_name] ||= [] + configuration_pod_whitelist[configuration_name] << pod_name + end + + #--------------------------------------# + # @return [Platform] the platform of the target definition. # # @note If no deployment target has been specified a default value is @@ -386,6 +428,7 @@ def set_platform(name, target = nil) # def store_pod(name, *requirements) parse_inhibit_warnings(name, requirements) + parse_configuration_whitelist(name, requirements) if requirements && !requirements.empty? pod = { name => requirements } @@ -446,6 +489,7 @@ def store_podspec(options = nil) build_configurations dependencies children + configuration_pod_whitelist ].freeze # @return [Hash] The hash representation of the target definition. @@ -539,6 +583,16 @@ def inhibit_warnings_hash get_hash_value('inhibit_warnings', {}) end + # Returns the configuration_pod_whitelist hash + # + # @return [Hash] Hash with configuration name as key, + # array of pod names to be linked in builds with that configuration + # as value. + # + def configuration_pod_whitelist + get_hash_value('configuration_pod_whitelist', {}) + end + # @return [Array] The dependencies specified by the user for # this target definition. # @@ -621,6 +675,32 @@ def parse_inhibit_warnings(name, requirements) requirements.pop if options.empty? end + # Removes :configurations from the requirements list, + # and adds the pod's name into internal hash for which pods should be + # linked in which configuration only. + # + # @param [String] pod name + # + # @param [Array] requirements + # If :configurations is the only key in the hash, the hash + # should be destroyed because it confuses Gem::Dependency. + # + # @return [void] + # + def parse_configuration_whitelist(name, requirements) + options = requirements.last + return requirements unless options.is_a?(Hash) + + configurations_to_whitelist_in = options.delete(:configurations) + if configurations_to_whitelist_in + configurations_to_whitelist_in.each{ |configuration| + whitelist_pod_for_configuration(name, configuration) + } + end + + requirements.pop if options.empty? + end + #-----------------------------------------------------------------------# end From 296df85207e8591b498f8da08c1b2f2ec5a6bd70 Mon Sep 17 00:00:00 2001 From: Joachim Bengtsson Date: Fri, 13 Dec 2013 14:08:12 +0100 Subject: [PATCH 2/2] [pod] whitelisting: code style and specs --- .../podfile/target_definition.rb | 10 +++++----- spec/podfile/target_definition_spec.rb | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/cocoapods-core/podfile/target_definition.rb b/lib/cocoapods-core/podfile/target_definition.rb index 101aa5ab0..8287b2337 100644 --- a/lib/cocoapods-core/podfile/target_definition.rb +++ b/lib/cocoapods-core/podfile/target_definition.rb @@ -332,13 +332,13 @@ def inhibit_warnings_for_pod(pod_name) # def is_pod_whitelisted_for_configuration?(pod_name, configuration_name) found = false - configuration_pod_whitelist.each { |configuration, pods| + configuration_pod_whitelist.each do |configuration, pods| if pods.include?(pod_name) found = true return true if configuration.to_s == configuration_name.to_s end - } - return !found + end + !found end # Whitelists a pod for a specific configuration. If a pod is whitelisted @@ -693,9 +693,9 @@ def parse_configuration_whitelist(name, requirements) configurations_to_whitelist_in = options.delete(:configurations) if configurations_to_whitelist_in - configurations_to_whitelist_in.each{ |configuration| + configurations_to_whitelist_in.each do |configuration| whitelist_pod_for_configuration(name, configuration) - } + end end requirements.pop if options.empty? diff --git a/spec/podfile/target_definition_spec.rb b/spec/podfile/target_definition_spec.rb index e793b989b..a439bcab0 100644 --- a/spec/podfile/target_definition_spec.rb +++ b/spec/podfile/target_definition_spec.rb @@ -230,6 +230,25 @@ module Pod #--------------------------------------# + it "whitelists pods by default" do + @root.store_pod("ObjectiveSugar") + @root.should.is_pod_whitelisted_for_configuration?("ObjectiveSugar", "Release") + end + + it "does not enable pods for un-whitelisted configurations if it is whitelisted for another" do + @root.store_pod("ObjectiveSugar") + @root.whitelist_pod_for_configuration("ObjectiveSugar", "Release") + @root.should.not.is_pod_whitelisted_for_configuration?("ObjectiveSugar", "Debug") + end + + it "enables pods for configurations they are whitelisted for" do + @root.store_pod("ObjectiveSugar") + @root.whitelist_pod_for_configuration("ObjectiveSugar", "Release") + @root.should.is_pod_whitelisted_for_configuration?("ObjectiveSugar", "Release") + end + + #--------------------------------------# + it "returns its platform" do @root.platform.should == Pod::Platform.new(:ios, '6.0') end