Skip to content

Commit

Permalink
use separate prefix for gems and additional plugin path
Browse files Browse the repository at this point in the history
  • Loading branch information
HatsuneMiku3939 committed Aug 1, 2016
1 parent 26eebdb commit d57576a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
14 changes: 7 additions & 7 deletions lib/fluent/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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_', dir_search_prefix: 'in_')
OUTPUT_REGISTRY = Registry.new(:output, 'fluent/plugin/out_', dir_search_prefix: 'out_')
FILTER_REGISTRY = Registry.new(:filter, 'fluent/plugin/filter_', dir_search_prefix: '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_', dir_search_prefix: 'buf_')
PARSER_REGISTRY = Registry.new(:parser, 'fluent/plugin/parser_', dir_search_prefix: 'parser_')
FORMATTER_REGISTRY = Registry.new(:formatter, 'fluent/plugin/formatter_', dir_search_prefix: 'formatter_')
STORAGE_REGISTRY = Registry.new(:storage, 'fluent/plugin/storage_', dir_search_prefix: 'storage_')

REGISTRIES = [INPUT_REGISTRY, OUTPUT_REGISTRY, FILTER_REGISTRY, BUFFER_REGISTRY, PARSER_REGISTRY, FORMATTER_REGISTRY, STORAGE_REGISTRY]

Expand Down
25 changes: 19 additions & 6 deletions lib/fluent/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ module Fluent
class Registry
DEFAULT_PLUGIN_PATH = File.expand_path('../plugin', __FILE__)

def initialize(kind, search_prefix)
def initialize(kind, search_prefix, dir_search_prefix: nil)
@kind = kind
@search_prefix = search_prefix
@dir_search_prefix = dir_search_prefix
@map = {}
@paths = [DEFAULT_PLUGIN_PATH]
end
Expand Down Expand Up @@ -54,11 +55,10 @@ def reverse_lookup(value)
end

def search(type)
path = "#{@search_prefix}#{type}"

# prefer LOAD_PATH than gems
[@paths, $LOAD_PATH].each do |paths|
files = paths.map { |lp|
# search from additional plugin directories
if @dir_search_prefix
path = "#{@dir_search_prefix}#{type}"
files = @paths.map { |lp|
lpath = File.expand_path(File.join(lp, "#{path}.rb"))
File.exist?(lpath) ? lpath : nil
}.compact
Expand All @@ -69,6 +69,19 @@ def search(type)
end
end

path = "#{@search_prefix}#{type}"

# prefer LOAD_PATH than gems
files = $LOAD_PATH.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

specs = Gem::Specification.find_all { |spec|
spec.contains_requirable_file? path
}
Expand Down

0 comments on commit d57576a

Please sign in to comment.