From 80ce8a0634b36b42f2119a4f5b5c488c6ef85457 Mon Sep 17 00:00:00 2001 From: Serhii Sychevyi Date: Tue, 9 Aug 2016 20:40:33 +0300 Subject: [PATCH 1/3] Add :directory option to curl source handler to allow extracting archives into other source directories --- lib/fpm/cookery/source_handler/curl.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fpm/cookery/source_handler/curl.rb b/lib/fpm/cookery/source_handler/curl.rb index 7334c19b..6eaae218 100644 --- a/lib/fpm/cookery/source_handler/curl.rb +++ b/lib/fpm/cookery/source_handler/curl.rb @@ -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) From c8a04a1004a9e9f671970feeea3d8ce3f31c412b Mon Sep 17 00:00:00 2001 From: Serhii Sychevyi Date: Tue, 9 Aug 2016 20:42:45 +0300 Subject: [PATCH 2/3] Move SourceHandler#handler_to_class method to SourceHandler class --- lib/fpm/cookery/source_handler.rb | 46 ++++++++++++++++--------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/fpm/cookery/source_handler.rb b/lib/fpm/cookery/source_handler.rb index c6d00c65..5b708543 100644 --- a/lib/fpm/cookery/source_handler.rb +++ b/lib/fpm/cookery/source_handler.rb @@ -12,6 +12,29 @@ 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] @@ -40,31 +63,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 From 70fdc17746838d022b380edfeaeca522f1d3be0a Mon Sep 17 00:00:00 2001 From: Serhii Sychevyi Date: Tue, 9 Aug 2016 20:44:11 +0300 Subject: [PATCH 3/3] Add multi_source source handler and allow source to receive array of sources instead of url string --- lib/fpm/cookery/source.rb | 13 +++-- lib/fpm/cookery/source_handler.rb | 1 + .../cookery/source_handler/multi_source.rb | 51 +++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 lib/fpm/cookery/source_handler/multi_source.rb diff --git a/lib/fpm/cookery/source.rb b/lib/fpm/cookery/source.rb index ff494282..f31ae0bb 100644 --- a/lib/fpm/cookery/source.rb +++ b/lib/fpm/cookery/source.rb @@ -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 @@ -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 } diff --git a/lib/fpm/cookery/source_handler.rb b/lib/fpm/cookery/source_handler.rb index 5b708543..fa0b7272 100644 --- a/lib/fpm/cookery/source_handler.rb +++ b/lib/fpm/cookery/source_handler.rb @@ -6,6 +6,7 @@ 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' diff --git a/lib/fpm/cookery/source_handler/multi_source.rb b/lib/fpm/cookery/source_handler/multi_source.rb new file mode 100644 index 00000000..c61639b1 --- /dev/null +++ b/lib/fpm/cookery/source_handler/multi_source.rb @@ -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