Class: Fusuma::Plugin::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/fusuma/plugin/manager.rb

Overview

Manage Fusuma plugins

Class Method Summary collapse

Instance Method Summary collapse

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]

Parameters:

  • plugin_class (Class)


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fusuma/plugin/manager.rb', line 83

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

Parameters:

  • plugin_class (Class)

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
# File 'lib/fusuma/plugin/manager.rb', line 131

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_pathsArray<String>

Examples:

Manager.load_paths
=> ["/path/to/fusuma/lib/fusuma/plugin/inputs/input.rb",
    "/path/to/fusuma/lib/fusuma/plugin/inputs/libinput_command_input.rb",
    "/path/to/fusuma/lib/fusuma/plugin/inputs/timer_input.rb"]

Returns:



125
126
127
# File 'lib/fusuma/plugin/manager.rb', line 125

def load_paths
  @load_paths ||= []
end

.pluginsObject



115
116
117
# File 'lib/fusuma/plugin/manager.rb', line 115

def plugins
  @plugins ||= {}
end

.require_base_pluginsObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/fusuma/plugin/manager.rb', line 104

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_patternObject



22
23
24
# File 'lib/fusuma/plugin/manager.rb', line 22

def exclude_path_pattern
  %r{fusuma/plugin/[^/]*.rb}
end

#fusuma_default_plugin_pathsObject



26
27
28
# File 'lib/fusuma/plugin/manager.rb', line 26

def fusuma_default_plugin_paths
  @_fusuma_default_plugin_paths ||= Dir.glob(File.expand_path("#{__dir__}/../../#{search_key}")).grep_v(exclude_path_pattern).sort
end

#fusuma_external_plugin_pathsArray<String>

Returns paths of external plugins (installed by gem).

Returns:

  • (Array<String>)

    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
53
# 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.expand_path("../../../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_gemsObject



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_dirObject



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_keyString

search_key

> “fusuma/plugin/detectors/*rb”

Returns:

  • (String)

    search key for plugin



59
60
61
# File 'lib/fusuma/plugin/manager.rb', line 59

def search_key
  File.join(plugin_dir_name, "*rb")
end