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:) ⇒ false, ...
Register a plugin class with the manager.
-
.exist?(plugin_class:, plugin_path:) ⇒ Boolean
Check if a plugin class is already registered.
-
.load_paths ⇒ Array<String>
: () -> Array.
-
.plugins ⇒ Object
: () -> Hash[String, Array].
-
.require_base_plugins ⇒ Object
: () -> bool.
Instance Method Summary collapse
-
#exclude_path_pattern ⇒ Object
: () -> Regexp.
-
#fusuma_default_plugin_paths ⇒ Object
: () -> Array.
-
#fusuma_external_plugin_paths ⇒ Array<String>
: () -> Array.
-
#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:) ⇒ false, ...
Register a plugin class with the manager. : (plugin_class: Class, plugin_path: String) -> (Array | false | nil)
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/fusuma/plugin/manager.rb', line 96 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
Check if a plugin class is already registered. Note: This intentionally returns false if only the path is registered, allowing multiple plugin classes from the same file (e.g., subclasses). : (plugin_class: Class, plugin_path: String) -> bool
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/fusuma/plugin/manager.rb', line 152 def exist?(plugin_class:, plugin_path:) # Skip existence check if path is already in load_paths # This allows multiple classes from the same file to be registered 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
141 142 143 |
# File 'lib/fusuma/plugin/manager.rb', line 141 def load_paths @load_paths ||= [] end |
.plugins ⇒ Object
: () -> Hash[String, Array]
130 131 132 |
# File 'lib/fusuma/plugin/manager.rb', line 130 def plugins @plugins ||= {} end |
.require_base_plugins ⇒ Object
: () -> bool
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/fusuma/plugin/manager.rb', line 118 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>
: () -> Array
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fusuma/plugin/manager.rb', line 37 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
66 67 68 |
# File 'lib/fusuma/plugin/manager.rb', line 66 def search_key File.join(plugin_dir_name, "*rb") end |