lib/fusuma/plugin/manager.rb in fusuma-2.5.0 vs lib/fusuma/plugin/manager.rb in fusuma-2.5.1

- old
+ new

@@ -13,41 +13,55 @@ def require_siblings_from_plugin_dir fusuma_default_plugin_paths.each { |siblings_plugin| require(siblings_plugin) } end - def fusuma_default_plugin_paths - search_key = File.join("../../", plugin_dir_name, "*.rb") - Dir.glob(File.expand_path("#{__dir__}/#{search_key}")).sort - end - def require_siblings_from_gems fusuma_external_plugin_paths.each { |siblings_plugin| require(siblings_plugin) } end + def exclude_path_pattern + %r{fusuma/plugin/[^/]*.rb} + end + + def fusuma_default_plugin_paths + @_fusuma_default_plugin_paths ||= Dir.glob(File.expand_path("#{__dir__}/../../#{search_key}")).grep_v(exclude_path_pattern).sort + end + def fusuma_external_plugin_paths - search_key = File.join(plugin_dir_name, "*.rb") - Gem.find_latest_files(search_key).map do |siblings_plugin| - next unless %r{fusuma-plugin-(.+).*/lib/#{plugin_dir_name}/.+\.rb}.match?(siblings_plugin) + @_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/(.*)}) - 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 + 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) - siblings_plugin - else - MultiLogger.warn "#{gemspec.name} #{gemspec.version} is incompatible with running #{fusuma_gemspec.name} #{fusuma_gemspec.version}" - end - end.compact + 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) + siblings_plugin + else + MultiLogger.warn "#{gemspec.name} #{gemspec.version} is incompatible with running #{fusuma_gemspec.name} #{fusuma_gemspec.version}" + MultiLogger.warn "gemspec: #{gemspec_path}" + next + end + end.compact.grep_v(exclude_path_pattern).sort end + def search_key + File.join(plugin_dir_name, "*rb") + end + private + # @example + # plugin_dir_name + # => "fusuma/plugin/detectors" + # @return [String] def plugin_dir_name @plugin_class.name.match(/(Fusuma::.*)::/)[1].to_s.underscore end class << self @@ -68,9 +82,16 @@ 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 def require_base_plugins