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

Add a Podfile option to reuse an external source specification #585

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/cocoapods-core/podfile/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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|
Copy link
Member

Choose a reason for hiding this comment

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

how will this linear scan perform on very large podfiled?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's a bit of a Shlemiel effect here, but ultimately it's all done in-memory and my profiling for 70 total pods shows an increase from 3% samples to 7%.

What do you think? Should I optimize now or can this be done later? I mean, I've made several optimizations recently that outweigh this by a margin 😂

Copy link
Member

Choose a reason for hiding this comment

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

Probably fine for now, but we might want to eventually put it in a hash for constant-time lookup

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

Expand Down
4 changes: 2 additions & 2 deletions lib/cocoapods-core/podfile/target_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Member

Choose a reason for hiding this comment

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

why these changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed several getters to avoid mutating the hash because iterating over deps mutated them.

The code assumed, previously, (and implicitly) that deps only get evaluated after the Podfile is completely scanned.

However, going over previously added deps before the entire Podfile is scanned mutated things and now it doesn't.

Copy link
Member

Choose a reason for hiding this comment

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

👍. Is there a test that will fail if we accidentally go back to the old behavior later?

Copy link
Contributor

Choose a reason for hiding this comment

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

want to continue this @igor-makarov ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hadn't had the time to think about this yet.. Sorry 😭

end

# Sets the inheritance mode for this target definition.
Expand Down Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions spec/podfile/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down