Class: Fusuma::Plugin::Manager
- Inherits:
-
Object
- Object
- Fusuma::Plugin::Manager
- Defined in:
- lib/fusuma/plugin/manager.rb
Overview
Manage Fusuma plugins
Class Method Summary collapse
-
.add(plugin_class:, plugin_path:) ⇒ Object
return [Hash, false].
- .exist?(plugin_class:, plugin_path:) ⇒ Boolean
-
.load_paths ⇒ Array<String>
: () -> Array.
-
.plugins ⇒ Object
: () -> Hash[untyped, untyped].
-
.require_base_plugins ⇒ Object
: () -> void.
Instance Method Summary collapse
-
#exclude_path_pattern ⇒ Object
: () -> Regexp.
-
#fusuma_default_plugin_paths ⇒ Object
: () -> Array.
-
#fusuma_external_plugin_paths ⇒ Array<String>
Paths of external plugins (installed by gem).
-
#initialize(plugin_class) ⇒ Manager
constructor
: (Class) -> void.
-
#require_siblings_from_gems ⇒ Object
: () -> Array.
-
#require_siblings_from_plugin_dir ⇒ Object
: () -> Array.
-
#search_key ⇒ String
search_key => “fusuma/plugin/detectors/*rb” : () -> String.
Constructor Details
#initialize(plugin_class) ⇒ Manager
: (Class) -> void
11 12 13 |
# File 'lib/fusuma/plugin/manager.rb', line 11 def initialize(plugin_class) @plugin_class = plugin_class end |
Class Method Details
.add(plugin_class:, plugin_path:) ⇒ Object
return [Hash, false]
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/fusuma/plugin/manager.rb', line 90 def add(plugin_class:, plugin_path:) return false if exist?(plugin_class: plugin_class, plugin_path: plugin_path) base = plugin_class.superclass.name plugins[base] ||= [] plugins[base] << plugin_class load_paths << plugin_path manager = Manager.new(plugin_class) @already_required ||= {} key = manager.search_key return if @already_required[key] @already_required[key] = true manager.require_siblings_from_plugin_dir manager.require_siblings_from_gems end |
.exist?(plugin_class:, plugin_path:) ⇒ Boolean
141 142 143 144 145 146 147 148 |
# File 'lib/fusuma/plugin/manager.rb', line 141 def exist?(plugin_class:, plugin_path:) return false if load_paths.include?(plugin_path) base = plugin_class.superclass.name return false unless plugins[base] plugins[base].include?(plugin_class) end |
.load_paths ⇒ Array<String>
: () -> Array
135 136 137 |
# File 'lib/fusuma/plugin/manager.rb', line 135 def load_paths @load_paths ||= [] end |
.plugins ⇒ Object
: () -> Hash[untyped, untyped]
124 125 126 |
# File 'lib/fusuma/plugin/manager.rb', line 124 def plugins @plugins ||= {} end |
.require_base_plugins ⇒ Object
: () -> void
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/fusuma/plugin/manager.rb', line 112 def require_base_plugins require_relative "base" require_relative "events/event" require_relative "inputs/input" require_relative "filters/filter" require_relative "parsers/parser" require_relative "buffers/buffer" require_relative "detectors/detector" require_relative "executors/executor" end |
Instance Method Details
#exclude_path_pattern ⇒ Object
: () -> Regexp
26 27 28 |
# File 'lib/fusuma/plugin/manager.rb', line 26 def exclude_path_pattern %r{fusuma/plugin/[^/]*.rb} end |
#fusuma_default_plugin_paths ⇒ Object
: () -> Array
31 32 33 |
# File 'lib/fusuma/plugin/manager.rb', line 31 def fusuma_default_plugin_paths @_fusuma_default_plugin_paths ||= Dir.glob(File.("#{__dir__}/../../#{search_key}")).grep_v(exclude_path_pattern).sort end |
#fusuma_external_plugin_paths ⇒ Array<String>
Returns paths of external plugins (installed by gem).
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/fusuma/plugin/manager.rb', line 36 def fusuma_external_plugin_paths @_fusuma_external_plugin_paths ||= Gem.find_latest_files(search_key).map do |siblings_plugin| next unless %r{fusuma-plugin-(.+).*/lib/#{plugin_dir_name}/.+\.rb}.match?(siblings_plugin) match_data = siblings_plugin.match(%r{(.*)/(.*)/lib/(.*)}) plugin_gemspec_path = Dir.glob("#{match_data[1]}/#{match_data[2]}/*.gemspec").first raise "Not Found: #{match_data[1]}/#{match_data[2]}/*.gemspec" unless plugin_gemspec_path plugin_gemspec = Gem::Specification.load(plugin_gemspec_path) fusuma_gemspec_path = File.("../../../fusuma.gemspec", __dir__ || ".") fusuma_gemspec = Gem::Specification.load(fusuma_gemspec_path) next if plugin_gemspec == fusuma_gemspec if plugin_gemspec.dependencies.find { |d| d.name == "fusuma" }&.match?(fusuma_gemspec) siblings_plugin else MultiLogger.warn "#{plugin_gemspec.name} #{plugin_gemspec.version} is incompatible with running #{fusuma_gemspec.name} #{fusuma_gemspec.version}" MultiLogger.warn "gemspec: #{plugin_gemspec_path}" next end end.compact.grep_v(exclude_path_pattern).sort end |
#require_siblings_from_gems ⇒ Object
: () -> Array
21 22 23 |
# File 'lib/fusuma/plugin/manager.rb', line 21 def require_siblings_from_gems fusuma_external_plugin_paths.each { |siblings_plugin| require(siblings_plugin) } end |
#require_siblings_from_plugin_dir ⇒ Object
: () -> Array
16 17 18 |
# File 'lib/fusuma/plugin/manager.rb', line 16 def require_siblings_from_plugin_dir fusuma_default_plugin_paths.each { |siblings_plugin| require(siblings_plugin) } end |
#search_key ⇒ String
search_key
> “fusuma/plugin/detectors/*rb”
: () -> String
65 66 67 |
# File 'lib/fusuma/plugin/manager.rb', line 65 def search_key File.join(plugin_dir_name, "*rb") end |