From f94fa71f36ba5c3e79b3fd481802bf0a6085775c Mon Sep 17 00:00:00 2001 From: coding Date: Sat, 19 May 2018 17:08:26 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E9=A2=84=E7=BC=96=E8=AF=91=E6=8F=92=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/modules/template.js | 29 ++++++++++++ .../plugins/MiaoScriptPackageManager.js | 44 +++++++++++++------ 2 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 src/main/resources/modules/template.js diff --git a/src/main/resources/modules/template.js b/src/main/resources/modules/template.js new file mode 100644 index 0000000..ad9048c --- /dev/null +++ b/src/main/resources/modules/template.js @@ -0,0 +1,29 @@ +function Template(tpl) { + var match; + var code = ['var r=[];']; + var re = /\{\{\s*([a-zA-Z\.\_0-9()]+)\s*\}\}/m; + function addLine(text) { + code.push('r.push(\'' + text.replace(/\'/g, '\\\'').replace(/\n/g, '\\n').replace(/\r/g, '\\r') + '\');'); + }; + while (match = re.exec(tpl)) { + if (match.index > 0) { + addLine(tpl.slice(0, match.index)); + } + code.push('r.push(this.' + match[1] + ');'); + tpl = tpl.substring(match.index + match[0].length); + } + addLine(tpl); + code.push('return r.join(\'\');'); + // 创建函数: + var fn = new Function(code.join('\n')); + // 用render()调用函数并绑定this参数: + this.render = function (model) { + return fn.apply(model); + }; +} + +exports = module.exports = { + create: function (tpl) { + return new Template(tpl); + } +} \ No newline at end of file diff --git a/src/main/resources/plugins/MiaoScriptPackageManager.js b/src/main/resources/plugins/MiaoScriptPackageManager.js index a042d75..ce948b4 100644 --- a/src/main/resources/plugins/MiaoScriptPackageManager.js +++ b/src/main/resources/plugins/MiaoScriptPackageManager.js @@ -1,11 +1,12 @@ 'use strict'; -/*global Java, base, module, exports, require, __dirname, __filename*/ -var command = require('api/command'); -var manager = require('api/plugin'); +/*global Java, base, module, exports, require, __dirname, __filename, ScriptEngineContextHolder*/ var task = require('api/task'); +var manager = require('api/plugin'); +var command = require('api/command'); -var http = require('http'); var fs = require('fs'); +var http = require('http'); +var template = require('template'); var pluginCache = []; var packageCache = []; @@ -22,19 +23,21 @@ var description = { } }, config: { - center: 'https://ms.yumc.pw/api/package/list' + center: 'https://ms.yumc.pw/api/package/list', + template: 'http://paste.yumc.pw/pxus6ap6l/g7di8z/raw' } }; var help = [ - '§6========= §a' + description.name + ' §6帮助 §aBy §b喵♂呜 §6=========', - '§6/mpm §ainstall <插件名称> §6- §3安装插件', + '§6========= §6[§a' + description.name + '§6] 帮助 §aBy §b喵♂呜 §6=========', + '§6/mpm §ainstall §e<插件名称> §6- §3安装插件', '§6/mpm §alist §6- §3列出仓库插件', - '§6/mpm §aupdate <插件名称> §6- §3更新插件(无插件名称则更新源)', - '§6/mpm §aupgrade <插件名称> §6- §3及时更新插件(update需要重启生效)', - '§6/mpm §areload <插件名称> §6- §3重载插件(无插件名称则重载自身)', - '§6/mpm §arun §6- §3运行JS代码', - '§6/mpm §4restart §6- §4重启MiaoScript脚本引擎' + '§6/mpm §aupdate §e<插件名称> §6- §3更新插件(无插件名称则更新源)', + '§6/mpm §aupgrade §e<插件名称> §6- §3及时更新插件(update需要重启生效)', + '§6/mpm §areload §e<插件名称> §6- §3重载插件(无插件名称则重载自身)', + '§6/mpm §arun §e §6- §3运行JS代码', + '§6/mpm §acreate §e<插件名称> [作者] [版本] [主命令] §6- §3通过模板创建名称', + '§6/mpm §crestart §6- §4重启MiaoScript脚本引擎' ]; function load() { @@ -42,8 +45,8 @@ function load() { pluginCache = Object.keys(manager.plugins); JSON.parse(http.get(self.config.center)).data.forEach(function cachePackageName(pkg) { packageCache[pkg.name] = pkg; - packageNameCache.push(pkg.name); }) + packageNameCache = Object.keys(packageCache); }) } @@ -110,6 +113,21 @@ function enable() { args.shift(1); console.sender(sender, eval(args.join(' '))); break; + case "create": + var name = args[1]; + if (!name) { + console.sender(sender, '§4参数错误 /mpm create <插件名称> [作者] [版本] [主命令]'); + return; + } + var result = template.create(http.get(self.config.template)).render({ + name: name, + author: args[2] || 'MiaoWoo', + version: args[3] || '1.0', + command: args[4] || name.toLowerCase(), + }); + fs.save(fs.file(__dirname, name + '.js'), result); + console.sender(sender, '§6插件 §a' + name + ' §6已生成到插件目录...'); + break; case "help": sendHelp(sender); break;