feat: 缓存编译结果 使用load载入脚本

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-09-23 18:30:14 +08:00
parent 7c5ad40497
commit 9556008bee
7 changed files with 32 additions and 23 deletions

2
.gitignore vendored
View File

@ -36,3 +36,5 @@
# Atlassian Stuff # Atlassian Stuff
/atlassian-ide-plugin.xml /atlassian-ide-plugin.xml
/src/main/resources/cache/

View File

@ -57,7 +57,6 @@ public class MiaoScript extends JavaPlugin {
this.engine.eval(new InputStreamReader(this.getResource("bios.js"))); this.engine.eval(new InputStreamReader(this.getResource("bios.js")));
engine.invokeFunction("boot", this); engine.invokeFunction("boot", this);
} catch (Exception e) { } catch (Exception e) {
Log.w("脚本引擎初始化失败! %s:%s", e.getClass().getName(), e.getMessage());
Log.d(e); Log.d(e);
} finally { } finally {
currentThread.setContextClassLoader(previousClassLoader); currentThread.setContextClassLoader(previousClassLoader);

View File

@ -13,7 +13,12 @@ var disable;
root = plugin.dataFolder.canonicalPath; root = plugin.dataFolder.canonicalPath;
} }
load(root + '/core/init.js'); load(root + '/core/init.js');
try {
init(root, plugin); init(root, plugin);
} catch (ex) {
log.w("MiaoScript 初始化失败! %s", ex);
throw ex;
}
disable = disablePlugins disable = disablePlugins
}; };
})(); })();

View File

@ -13,6 +13,9 @@ var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption");
* @returns {*} * @returns {*}
*/ */
exports.file = function () { exports.file = function () {
if (!arguments[0]) {
log.w("文件名称不得为 undefined 或者 null !");
}
if (exports.canonical(arguments[0])) { if (exports.canonical(arguments[0])) {
return arguments[0]; return arguments[0];
} }
@ -28,7 +31,8 @@ exports.file = function () {
* @param file * @param file
*/ */
exports.mkdirs = function (file) { exports.mkdirs = function (file) {
file.getParentFile().mkdirs(); // noinspection JSUnresolvedVariable
file.parentFile.mkdirs();
}; };
/** /**
* 创建文件 * 创建文件

View File

@ -47,9 +47,11 @@ function loadPlugins(plugin) {
// 初始化本体插件 // 初始化本体插件
var self = require('modules/plugin'); var self = require('modules/plugin');
self.init(plugin, plugins_dir); self.init(plugin, plugins_dir);
if (!self.$) {
self.load(); self.load();
self.enable(); self.enable();
} }
}
// noinspection JSUnusedLocalSymbols // noinspection JSUnusedLocalSymbols
/** /**

View File

@ -8,6 +8,7 @@
var File = Java.type("java.io.File"); var File = Java.type("java.io.File");
var Files = Java.type("java.nio.file.Files"); var Files = Java.type("java.nio.file.Files");
var String = Java.type("java.lang.String"); var String = Java.type("java.lang.String");
var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption");
/** /**
* 解析模块名称为文件 * 解析模块名称为文件
@ -48,26 +49,20 @@
log.w("模块目录: %s", _canonical(moduleFile)); log.w("模块目录: %s", _canonical(moduleFile));
} }
/**
* 使用NIO读取文件内容
* @param file 文件
* @private
*/
function _readFile(file) {
// noinspection JSPrimitiveTypeWrapperUsage
return new String(Files.readAllBytes(file.toPath()), "UTF-8");
}
/** /**
* 预编译模块 * 预编译模块
* @param name
* @param src * @param src
* @returns {Object} * @returns {Object}
*/ */
function compileJs(src) { function compileJs(name, src) {
var head = "(function (module, exports, require) {\n"; var head = "(function (module, exports, require) {\n";
var tail = "\n});"; var tail = "\n});";
var fulljs = head + src + tail; var fulljs = head + src + tail;
return eval(fulljs); var cacheFile = cacheDir + "/" + name + ".js";
log.d(cacheFile);
base.save(cacheFile, fulljs);
return load(cacheFile);
} }
/** /**
@ -104,16 +99,17 @@
exports: {}, exports: {},
require: exports(file.parentFile) require: exports(file.parentFile)
}; };
var src = _readFile(file); var src = base.read(file);
try { try {
// 预编译模块 // 预编译模块
var compiledWrapper = compileJs(src); var compiledWrapper = compileJs(name, src);
compiledWrapper.apply(module.exports, [ compiledWrapper.apply(module.exports, [
module, module.exports, module.require module, module.exports, module.require
]); ]);
} catch (ex) { } catch (ex) {
log.w("模块 %s 编译失败!", name); log.w("模块 %s 编译失败!", name);
log.w(ex); log.d(ex);
return;
} }
log.d('模块 %s 编译成功!', name); log.d('模块 %s 编译成功!', name);
module.loaded = true; module.loaded = true;
@ -132,6 +128,8 @@
}; };
} }
var cacheDir = parent + "/cache";
// 等于 undefined 说明 parent 是一个字符串 需要转成File // 等于 undefined 说明 parent 是一个字符串 需要转成File
// 可能更加准确的方案 // 可能更加准确的方案
if (_canonical(parent) === undefined) { if (_canonical(parent) === undefined) {

View File

@ -11,7 +11,7 @@ var fs = require('core/fs');
function loadPlugins(path) { function loadPlugins(path) {
path = fs.file(path); path = fs.file(path);
log.i("开始扫描 %s 下的插件...", path); log.i("开始扫描 %s 下的插件...", path);
updatePlugins(); updatePlugins(path);
var files = []; var files = [];
fs.list(path).forEach(function (file) { fs.list(path).forEach(function (file) {
files.push(file.toFile()); files.push(file.toFile());
@ -54,7 +54,6 @@ function loadJsPlugin(files) {
return file.name.endsWith(".js") return file.name.endsWith(".js")
}).forEach(function (file) { }).forEach(function (file) {
var p = require(file); var p = require(file);
log.d(JSON.stringify(p));
if (!p.description || !p.description.name) { if (!p.description || !p.description.name) {
log.w("文件 %s 不存在 description 描述信息 无法加载插件!"); log.w("文件 %s 不存在 description 描述信息 无法加载插件!");
} else { } else {