Module: Kscript::PluginLoader

Defined in:
lib/kscript/plugins.rb

Constant Summary collapse

PLUGIN_DIR =
File.expand_path('plugins', __dir__)

Class Method Summary collapse

Class Method Details

.load_allObject



7
8
9
10
11
12
13
14
15
# File 'lib/kscript/plugins.rb', line 7

def self.load_all
  Dir.glob(File.join(PLUGIN_DIR, 'kk_*.rb')).each do |file|
    # 解析出类名
    basename = File.basename(file, '.rb')
    class_name = basename.split('_').map(&:capitalize).join
    # 只在未定义时 require
    require file unless Kscript.const_defined?(class_name, false)
  end
end

.plugin_infosObject

返回所有已注册插件的元信息



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kscript/plugins.rb', line 18

def self.plugin_infos
  Kscript::Plugin.all.map do |name, klass|
    {
      name: name,
      class: klass,
      description: klass.respond_to?(:description) ? klass.description : nil,
      arguments: klass.respond_to?(:arguments) ? klass.arguments : nil,
      usage: klass.respond_to?(:usage) ? klass.usage : nil,
      group: klass.respond_to?(:group) ? klass.group : nil,
      author: klass.respond_to?(:author) ? klass.author : nil
    }
  end
end