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 multiple source support #168

Open
wants to merge 3 commits into
base: main
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
13 changes: 10 additions & 3 deletions lib/fpm/cookery/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ class Source

def initialize(url, options = nil)
options ||= {}
@url = Addressable::URI.parse(url.to_s)
@provider = options[:with]

if url.is_a? Array
@url = url
@provider = :multi_source
else
@url = Addressable::URI.parse(url.to_s)
@provider = options[:with]
end

@options = options
end

Expand All @@ -26,7 +33,7 @@ def fetchable?
end

def url
@url.to_s
@provider == :multi_source ? @url : @url.to_s
end
[:to_s, :to_str].each { |m| alias_method m, :url }

Expand Down
47 changes: 25 additions & 22 deletions lib/fpm/cookery/source_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,36 @@
require 'fpm/cookery/source_handler/local_path'
require 'fpm/cookery/source_handler/noop'
require 'fpm/cookery/source_handler/directory'
require 'fpm/cookery/source_handler/multi_source'
require 'fpm/cookery/log'
require 'fpm/cookery/exceptions'

module FPM
module Cookery
class SourceHandler
class << self
def handler_to_class(provider)
begin
maybe_klass = self.const_get(provider.to_s.split('_').map(&:capitalize).join)

instance_method_map = Hash[maybe_klass.instance_methods.map { |m| [m, true] }]
missing_methods = REQUIRED_METHODS.find_all { |m| !instance_method_map.key?(m) }

unless missing_methods.empty?
formatted_missing = missing_methods.map { |m| "`#{m}'" }.join(', ')
message = %{#{maybe_klass} does not implement required method(s): #{formatted_missing}}
Log.error message
raise Error::Misconfiguration, message
end

maybe_klass
rescue NameError => e
Log.error "Specified provider #{provider} does not exist."
raise Error::Misconfiguration, e.message
end
end
end

DEFAULT_HANDLER = :curl
LOCAL_HANDLER = :local_path
REQUIRED_METHODS = [:fetch, :extract]
Expand Down Expand Up @@ -40,31 +64,10 @@ def initialize(source, cachedir, builddir)

private
def get_source_handler(provider)
klass = handler_to_class(provider)
klass = SourceHandler.handler_to_class(provider)
# XXX Refactor handler to avoid passing the options.
klass.new(@source, @source.options, @cachedir, @builddir)
end

def handler_to_class(provider)
begin
maybe_klass = self.class.const_get(provider.to_s.split('_').map(&:capitalize).join)

instance_method_map = Hash[maybe_klass.instance_methods.map { |m| [m, true] }]
missing_methods = REQUIRED_METHODS.find_all { |m| !instance_method_map.key?(m) }

unless missing_methods.empty?
formatted_missing = missing_methods.map { |m| "`#{m}'" }.join(', ')
message = %{#{maybe_klass} does not implement required method(s): #{formatted_missing}}
Log.error message
raise Error::Misconfiguration, message
end

maybe_klass
rescue NameError => e
Log.error "Specified provider #{provider} does not exist."
raise Error::Misconfiguration, e.message
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/fpm/cookery/source_handler/curl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def fetch(config = {})
end

def extract(config = {})
Dir.chdir(builddir) do
Dir.chdir((builddir/options[:directory])) do
case local_path.extname
when '.bz2', '.gz', '.tgz', '.xz', '.tar'
safesystem('tar', 'xf', local_path)
Expand Down
51 changes: 51 additions & 0 deletions lib/fpm/cookery/source_handler/multi_source.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'fpm/cookery/source_handler/template'

module FPM
module Cookery
class SourceHandler
class MultiSource < FPM::Cookery::SourceHandler::Template
NAME = :multi_source
CHECKSUM = false

def initialize(source_url, options, cachedir, builddir)
super

@source_handlers = []

source.url.each do |url|
klass = FPM::Cookery::SourceHandler.handler_to_class(url[:with])

@source_handlers << klass.new(url[:url], url, cachedir, builddir)
end
end

def fetch(config = {})
Log.info "multi_source handler; fetching..."

@source_handlers.each do |source_handler|
source_handler.fetch(config)
end

Log.info "multi_source handler; fetch complete"
end

def extract(config = {})
Log.info "multi_source handler; extracting..."

extracted = builddir

@source_handlers.each do |source_handler|
if source_handler.options[:main]
extracted = source_handler.extract(config)
else
source_handler.extract(config)
end
end

extracted
end

end
end
end
end