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>
- .plugins ⇒ Object
- .require_base_plugins ⇒ Object
Instance Method Summary collapse
- #exclude_path_pattern ⇒ Object
- #fusuma_default_plugin_paths ⇒ Object
-
#fusuma_external_plugin_paths ⇒ Array<String>
Paths of external plugins (installed by gem).
-
#initialize(plugin_class) ⇒ Manager
constructor
A new instance of Manager.
- #require_siblings_from_gems ⇒ Object
- #require_siblings_from_plugin_dir ⇒ Object
-
#search_key ⇒ String
search_key => “fusuma/plugin/detectors/*rb”.
Constructor Details
#initialize(plugin_class) ⇒ Manager
Returns a new instance of Manager.
10 11 12 |
# File 'lib/fusuma/plugin/manager.rb', line 10 def initialize(plugin_class) @plugin_class = plugin_class end |
Class Method Details
.add(plugin_class:, plugin_path:) ⇒ Object
return [Hash, false]
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/fusuma/plugin/manager.rb', line 82 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
130 131 132 133 134 135 136 137 |
# File 'lib/fusuma/plugin/manager.rb', line 130 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>
124 125 126 |
# File 'lib/fusuma/plugin/manager.rb', line 124 def load_paths @load_paths ||= [] end |
.plugins ⇒ Object
114 115 116 |
# File 'lib/fusuma/plugin/manager.rb', line 114 def plugins @plugins ||= {} end |
.require_base_plugins ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/fusuma/plugin/manager.rb', line 103 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
22 23 24 |
# File 'lib/fusuma/plugin/manager.rb', line 22 def exclude_path_pattern %r{fusuma/plugin/[^/]*.rb} end |
#fusuma_default_plugin_paths ⇒ Object
26 27 28 |
# File 'lib/fusuma/plugin/manager.rb', line 26 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).
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/fusuma/plugin/manager.rb', line 31 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) 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
18 19 20 |
# File 'lib/fusuma/plugin/manager.rb', line 18 def require_siblings_from_gems fusuma_external_plugin_paths.each { |siblings_plugin| require(siblings_plugin) } end |
#require_siblings_from_plugin_dir ⇒ Object
14 15 16 |
# File 'lib/fusuma/plugin/manager.rb', line 14 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”
58 59 60 |
# File 'lib/fusuma/plugin/manager.rb', line 58 def search_key File.join(plugin_dir_name, "*rb") end |