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)


57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fusuma/plugin/manager.rb', line 57

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)
  manager.require_siblings_from_plugin_dir
  manager.require_siblings_from_gems
end

.exist?(plugin_class:, plugin_path:) ⇒ Boolean

Parameters:

  • plugin_class (Class)

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
# File 'lib/fusuma/plugin/manager.rb', line 92

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_pathsObject



86
87
88
# File 'lib/fusuma/plugin/manager.rb', line 86

def load_paths
  @load_paths ||= []
end

.pluginsObject



82
83
84
# File 'lib/fusuma/plugin/manager.rb', line 82

def plugins
  @plugins ||= {}
end

.require_base_pluginsObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/fusuma/plugin/manager.rb', line 71

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

#require_siblings_from_gemsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fusuma/plugin/manager.rb', line 21

def require_siblings_from_gems
  search_key = File.join(plugin_dir_name, '*.rb')
  Gem.find_latest_files(search_key).each do |siblings_plugin|
    next unless siblings_plugin =~ %r{fusuma-plugin-(.+).*/lib/#{plugin_dir_name}/\1_.+.rb}

    match_data = siblings_plugin.match(%r{(.*)/(.*)/lib/(.*)})
    gemspec_path = Dir.glob("#{match_data[1]}/#{match_data[2]}/*.gemspec").first
    raise "Not Found: #{match_data[1]}/#{match_data[2]}/*.gemspec" unless gemspec_path

    gemspec = Gem::Specification.load(gemspec_path)
    fusuma_gemspec_path = File.expand_path('../../../fusuma.gemspec', __dir__)
    fusuma_gemspec = Gem::Specification.load(fusuma_gemspec_path)
    if gemspec.dependencies.find { |d| d.name == 'fusuma' }&.match?(fusuma_gemspec)
      require siblings_plugin
    else
      MultiLogger.warn "#{gemspec.name} #{gemspec.version} is incompatible with running #{fusuma_gemspec.name} #{fusuma_gemspec.version}"
    end
  end
end

#require_siblings_from_plugin_dirObject



14
15
16
17
18
19
# File 'lib/fusuma/plugin/manager.rb', line 14

def require_siblings_from_plugin_dir
  search_key = File.join('../../', plugin_dir_name, '*.rb')
  Dir.glob(File.expand_path("#{__dir__}/#{search_key}")).sort.each do |siblings_plugin|
    require siblings_plugin
  end
end