diff --git a/lib/cocoapods-core/podfile/dsl.rb b/lib/cocoapods-core/podfile/dsl.rb index 5bdfe4ae4..df732ab0a 100644 --- a/lib/cocoapods-core/podfile/dsl.rb +++ b/lib/cocoapods-core/podfile/dsl.rb @@ -292,6 +292,19 @@ def ensure_bundler!(version = nil) # # pod 'JSONKit', :podspec => 'https://example.com/JSONKit.podspec' # + # ### Use a previously defined external source. + # + # If a dependency was already defined using an external source, reuse that definition: + # + # target 'TargetA' do + # # define an external source for `JSONKit` + # pod 'JSONKit', :podspec => 'https://example.com/JSONKit.podspec' + # end + # + # target 'TargetB' do + # # reuse the previous external source for `JSONKit` + # pod 'JSONKit' + # end # # @note This method allow a nil name and the raises to be more # informative. @@ -303,6 +316,18 @@ def pod(name = nil, *requirements) raise StandardError, 'A dependency requires a name.' end + if requirements.empty? || requirements.last.is_a?(Hash) + theoretical_dependency = Dependency.new(name, requirements.last) + if theoretical_dependency.external_source.nil? + previously_defined_dependency = dependencies.find do |dependency| + dependency.root_name == theoretical_dependency.root_name && dependency.external_source + end + if previously_defined_dependency + requirements = [previously_defined_dependency.external_source] + end + end + end + current_target_definition.store_pod(name, *requirements) end diff --git a/lib/cocoapods-core/podfile/target_definition.rb b/lib/cocoapods-core/podfile/target_definition.rb index 9d0307322..9a9e2fc8e 100644 --- a/lib/cocoapods-core/podfile/target_definition.rb +++ b/lib/cocoapods-core/podfile/target_definition.rb @@ -174,7 +174,7 @@ def abstract=(abstract) # @return [String] the inheritance mode for this target definition. # def inheritance - get_hash_value('inheritance', 'complete') + get_hash_value('inheritance') || 'complete' end # Sets the inheritance mode for this target definition. @@ -907,7 +907,7 @@ def get_hash_value(key, base_value = nil) unless HASH_KEYS.include?(key) raise StandardError, "Unsupported hash key `#{key}`" end - internal_hash[key] = base_value if internal_hash[key].nil? + internal_hash[key] ||= base_value unless base_value.nil? internal_hash[key] end diff --git a/spec/podfile/dsl_spec.rb b/spec/podfile/dsl_spec.rb index 806aba659..427ffec5e 100644 --- a/spec/podfile/dsl_spec.rb +++ b/spec/podfile/dsl_spec.rb @@ -411,6 +411,24 @@ module Pod target.dependencies.sort_by(&:name).should == expected_dependencies end + it 'implicitly reuses an external source' do + podfile = Podfile.new do + target 'A' do + pod 'RestKit', :git => 'https://github.com/RestKit/RestKit.git' + end + + target 'B' do + pod 'RestKit' + end + end + + expected_dependencies = [ + Dependency.new('RestKit', :git => 'https://github.com/RestKit/RestKit.git'), + ] + podfile.target_definitions['A'].dependencies.sort_by(&:name).should == expected_dependencies + podfile.target_definitions['B'].dependencies.sort_by(&:name).should == expected_dependencies + end + it 'raises if no name is specified for a Pod' do lambda do Podfile.new do