From e93a438cc282ea2df41b50eb39c014daa968b7a2 Mon Sep 17 00:00:00 2001 From: coding Date: Thu, 12 Oct 2017 14:06:10 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9Ereload=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=20=E6=96=B0=E5=A2=9E=E9=85=8D=E7=BD=AE=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/modules/plugin.js | 67 +++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/src/main/resources/modules/plugin.js b/src/main/resources/modules/plugin.js index 47f4a74..cfa7bab 100644 --- a/src/main/resources/modules/plugin.js +++ b/src/main/resources/modules/plugin.js @@ -5,6 +5,7 @@ /*global Java, base, module, exports, require, __FILE__*/ // var zip = require("core/zip"); var fs = require('core/fs'); +var yaml = require('modules/yaml'); var event = require('modules/event'); /** @@ -70,12 +71,65 @@ function loadJsPlugin(files) { if (!p.description || !p.description.name) { log.w("文件 %s 不存在 description 描述信息 无法加载插件!", file); } else { + initPlugin(file, p); plugins.push(p); plugins[p.description.name] = p; - log.i('插件 %s 版本 %s 加载成功!', p.description.name, p.description.version); + log.i('载入插件 %s 版本 %s By %s', p.description.name, p.description.version || '未知', p.description.author || '未知'); } }) } +/** + * 初始化插件内容(提供config,__DATA__等参数) + */ +function initPlugin(file, plugin){ + // 初始化 __DATA__ + plugin.__DATA__ = fs.file(file.parentFile, plugin.description.name); + // 初始化 getFile() + plugin.getFile = function(name) { + return fs.file(plugin.__DATA__, name); + } + // 初始化 getConfig() + /** + * 获取配置文件 + * @constructor + * @constructor (file|string) + */ + plugin.getConfig = function() { + switch (arguments.length) { + case 0: + return plugin.config; + case 1: + var file = arguments[0]; + if (!file.isFile) { + file = plugin.getFile(file); + } + return yaml.safeLoad(base.read(file)); + } + } + // 初始化 saveConfig() + /** + * 保存配置文件 + * @constructor + * @constructor (file, content) + */ + plugin.saveConfig = function() { + switch (arguments.length) { + case 0: + base.save(plugin.configFile, yaml.safeDump(plugin.config)); + case 2: + base.save(arguments[0], yaml.safeDump(arguments[1])); + } + } + // 初始化 getDataFolder() + plugin.getDataFolder = function() { + return plugin.__DATA__; + } + // 初始化 config + plugin.configFile = plugin.getFile('config.yml'); + if (plugin.configFile.isFile()) { + plugin.config = plugin.getConfig('config.yml'); + } +} function runAndCatch(jsp, exec, ext) { if (exec) { @@ -97,6 +151,10 @@ function checkAndGet(args) { return plugins; } var name = args[0]; + // 如果是插件 则直接返回 + if (name.description) { + return [name]; + } if (!exports.plugins[name]) { throw new Error("插件 " + name + "不存在!"); } @@ -123,4 +181,11 @@ exports.enable = function () { }; exports.disable = function () { checkAndGet(arguments).forEach(function (p) runAndCatch(p, p.disable, function () event.disable(p))); +}; +exports.reload = function () { + checkAndGet(arguments).forEach(function (p) { + exports.disable(p); + exports.load(p); + exports.enable(p); + }); }; \ No newline at end of file