From bc2d25737e8ef2b492a61687fb37f72324893fcd Mon Sep 17 00:00:00 2001 From: filepang Date: Mon, 1 Aug 2016 12:24:42 +0900 Subject: [PATCH] Allow to search plugin from multiple prefix directory --- lib/fluent/plugin.rb | 14 ++++++------ lib/fluent/registry.rb | 50 ++++++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/lib/fluent/plugin.rb b/lib/fluent/plugin.rb index e970580446..1daa538b63 100644 --- a/lib/fluent/plugin.rb +++ b/lib/fluent/plugin.rb @@ -26,15 +26,15 @@ module Plugin # ex: storage, buffer chunk, ... # first class plugins (instantiated by Engine) - INPUT_REGISTRY = Registry.new(:input, 'fluent/plugin/in_') - OUTPUT_REGISTRY = Registry.new(:output, 'fluent/plugin/out_') - FILTER_REGISTRY = Registry.new(:filter, 'fluent/plugin/filter_') + INPUT_REGISTRY = Registry.new(:input, 'fluent/plugin/in_', 'in_') + OUTPUT_REGISTRY = Registry.new(:output, 'fluent/plugin/out_', 'out_') + FILTER_REGISTRY = Registry.new(:filter, 'fluent/plugin/filter_', 'filter_') # feature plugin: second class plugins (instanciated by Plugins or Helpers) - BUFFER_REGISTRY = Registry.new(:buffer, 'fluent/plugin/buf_') - PARSER_REGISTRY = Registry.new(:parser, 'fluent/plugin/parser_') - FORMATTER_REGISTRY = Registry.new(:formatter, 'fluent/plugin/formatter_') - STORAGE_REGISTRY = Registry.new(:storage, 'fluent/plugin/storage_') + BUFFER_REGISTRY = Registry.new(:buffer, 'fluent/plugin/buf_', 'buf_') + PARSER_REGISTRY = Registry.new(:parser, 'fluent/plugin/parser_', 'parser_') + FORMATTER_REGISTRY = Registry.new(:formatter, 'fluent/plugin/formatter_', 'formatter_') + STORAGE_REGISTRY = Registry.new(:storage, 'fluent/plugin/storage_', 'storage_') REGISTRIES = [INPUT_REGISTRY, OUTPUT_REGISTRY, FILTER_REGISTRY, BUFFER_REGISTRY, PARSER_REGISTRY, FORMATTER_REGISTRY, STORAGE_REGISTRY] diff --git a/lib/fluent/registry.rb b/lib/fluent/registry.rb index afd66d2e3e..97be1e735f 100644 --- a/lib/fluent/registry.rb +++ b/lib/fluent/registry.rb @@ -20,9 +20,9 @@ module Fluent class Registry DEFAULT_PLUGIN_PATH = File.expand_path('../plugin', __FILE__) - def initialize(kind, search_prefix) + def initialize(kind, *search_prefixes) @kind = kind - @search_prefix = search_prefix + @search_prefixes = search_prefixes @map = {} @paths = [DEFAULT_PLUGIN_PATH] end @@ -54,32 +54,34 @@ def reverse_lookup(value) end def search(type) - path = "#{@search_prefix}#{type}" + @search_prefixes.each do |search_prefix| + path = "#{search_prefix}#{type}" - # prefer LOAD_PATH than gems - [@paths, $LOAD_PATH].each do |paths| - files = paths.map { |lp| - lpath = File.expand_path(File.join(lp, "#{path}.rb")) - File.exist?(lpath) ? lpath : nil - }.compact - unless files.empty? - # prefer newer version - require files.sort.last - return + # prefer LOAD_PATH than gems + [@paths, $LOAD_PATH].each do |paths| + files = paths.map { |lp| + lpath = File.expand_path(File.join(lp, "#{path}.rb")) + File.exist?(lpath) ? lpath : nil + }.compact + unless files.empty? + # prefer newer version + require files.sort.last + return + end end - end - - specs = Gem::Specification.find_all { |spec| - spec.contains_requirable_file? path - } - # prefer newer version - specs = specs.sort_by { |spec| spec.version } - if spec = specs.last - spec.require_paths.each { |lib| - file = "#{spec.full_gem_path}/#{lib}/#{path}" - require file + specs = Gem::Specification.find_all { |spec| + spec.contains_requirable_file? path } + + # prefer newer version + specs = specs.sort_by { |spec| spec.version } + if spec = specs.last + spec.require_paths.each { |lib| + file = "#{spec.full_gem_path}/#{lib}/#{path}" + require file + } + end end end end