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

Allow download strategies to preprocess download options #57

Merged
merged 8 commits into from
Jun 10, 2016
Merged
24 changes: 24 additions & 0 deletions lib/cocoapods-downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,29 @@ def self.for_target(target_path, options)
klass = downloader_class_by_key[strategy]
klass.new(target_path, url, sub_options)
end

# Have the concrete strategy preprocess options
#
# @param [Hash<Symbol,String>] options
# The request options to preprocess
#
# @return [Hash<Symbol,String>] the new options
#
def self.preprocess_options(options)
options = Hash[options.map { |k, v| [k.to_sym, v] }]

if options.nil? || options.empty?
raise DownloaderError, 'No source URL provided.'
end

strategy = strategy_from_options(options)
unless strategy
raise DownloaderError, 'Unsupported download strategy ' \
"`#{options.inspect}`."
end

klass = downloader_class_by_key[strategy]
Copy link
Member

Choose a reason for hiding this comment

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

maybe extract this into downloader_class_from_options to avoid duplication?

klass.preprocess_options(options)
end
end
end
3 changes: 3 additions & 0 deletions lib/cocoapods-downloader/api_exposable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ def expose_api(mod = nil, &block)
raise 'Only a module *or* is required, not both.'
end
include mod
# TODO: Try to find a nicer way to do this
# See https://github.com/CocoaPods/cocoapods-downloader/pull/57
extend mod
end

alias override_api expose_api
Expand Down
14 changes: 14 additions & 0 deletions lib/cocoapods-downloader/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,20 @@ def self.executable(name)
execute_command(name.to_s, command.flatten, true)
end
end

# preprocess download options
#
# Usage of this method is optional. concrete strategies should not
# assume options are preprocessed for correct execution.
#
# @param [Hash<Symbol,String>] options
# The request options to preprocess
#
# @return [Hash<Symbol,String>] the new options
#
def self.preprocess_options(options)
options
end
end
end
end
17 changes: 17 additions & 0 deletions lib/cocoapods-downloader/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ def checkout_options
end
end

def self.preprocess_options(options)
return options unless options[:branch]

command = ['ls-remote',
options[:git],
options[:branch]]
output = Git.execute_command('git', command)
match = /^([a-z0-9]*)\t.*/.match(output)

return options if match.nil?

options[:commit] = match[1]
options.delete(:branch)

options
end

private

# @!group Base class hooks
Expand Down
6 changes: 6 additions & 0 deletions spec/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ module Downloader
e = lambda { Base.new('path', 'url', options) }.should.raise DownloaderError
e.message.should.match /Unrecognized options/
end

it 'has no preprocessing' do
options = { :symbol => 'aaaaaa' }
new_options = Base.preprocess_options(options)
new_options.should == options
end
end
end
end
20 changes: 20 additions & 0 deletions spec/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,26 @@ def ensure_only_one_ref(folder)
should.not.raise { downloader.download }
end
end

describe '::preprocess_options' do
it 'skips non-branch requests' do
options = { :git => fixture_url('git-repo'), :commit => 'aaaa' }
new_options = Downloader.preprocess_options(options)
options.should == new_options
end

it 'resolves master to a commit' do
options = { :git => fixture_url('git-repo'), :branch => 'master' }
new_options = Downloader.preprocess_options(options)
new_options[:commit].should == '98cbf14201a78b56c6b7290f6cac840a7597a1c2'
end

it 'ignores invalid branches' do
options = { :git => fixture_url('git-repo'), :branch => 'aaaa' }
new_options = Downloader.preprocess_options(options)
new_options[:branch].should == 'aaaa'
end
end
end
end
end