Plugin loading and tests

This commit is contained in:
Jiiks 2018-01-16 03:45:23 +02:00
parent bc05138037
commit 50f0d4f91f
3 changed files with 155 additions and 28 deletions

View File

@ -27146,6 +27146,7 @@ webpackContext.id = 129;
const { Module } = __webpack_require__(1);
const { BDIpc } = __webpack_require__(123);
const fs = window.require('fs');
const path = window.require('path');
//TODO add these to actual utils
class Utils {
@ -27241,9 +27242,24 @@ class FileUtils {
}
}
class Plugin {
constructor(args) {}
start() {
if (this.onStart) return this.onStart();
}
stop() {
if (this.onStop) return this.onStop();
}
}
class PluginManager extends Module {
setInitialState() {
window.pm = this;
this.setState({
plugins: []
});
@ -27254,12 +27270,33 @@ class PluginManager extends Module {
return this.state.plugins;
}
loadPlugin(plugin) {
async loadPlugin(pluginPath) {
const { plugins } = this.state;
plugins.push(plugin);
this.setState({
plugins
});
try {
const loaded = plugins.find(plugin => plugin.pluginPath === pluginPath);
if (loaded) {
throw { 'message': 'Attempted to load an already loaded plugin' };
}
const readConfig = await this.readConfig(pluginPath);
const mainPath = path.join(pluginPath, readConfig.main);
const plugin = window.require(mainPath)(Plugin, {}, {});
const instance = new plugin();
plugins.push(Object.assign({
pluginPath,
instance
}, readConfig));
this.setState(plugins);
return instance;
} catch (err) {
throw err;
}
}
getPluginByName(name) {
@ -27270,6 +27307,18 @@ class PluginManager extends Module {
return this.plugins.find(plugin => plugin.id === id);
}
stopPlugin(name) {
const plugin = this.getPluginByName(name);
if (plugin && plugin.instance) return plugin.instance.stop();
return null;
}
startPlugin(name) {
const plugin = this.getPluginByName(name);
if (plugin && plugin.instance) return plugin.instance.start();
return null;
}
async readConfig(path) {
path = `${path}/config.json`;
return FileUtils.readJsonFromFile(path);
@ -27281,19 +27330,30 @@ const _instance = new PluginManager();
async function tests() {
const pluginName = 'Example';
const config = await BDIpc.send('getConfig');
const pluginPath = config.paths.find(path => 'plugins' in path).plugins;
console.log(`Plugin Path: ${pluginPath}`);
try {
const plugin = await _instance.loadPlugin(path.join(pluginPath, pluginName));
const plugin2 = await _instance.loadPlugin(path.join(pluginPath, pluginName));
window.pl = plugin;
} catch (err) {
console.log(`Failed to load plugin! ${err.message}`);
}
const examplePluginPath = `${pluginPath}/Example`;
// const config = await BDIpc.send('getConfig');
// const pluginPath = config.paths.find(path => 'plugins' in path).plugins;
// const examplePluginPath = path.join(pluginPath, pluginName);
//Test read config
try {
/*try {
const readConfig = await _instance.readConfig(examplePluginPath);
console.log(readConfig);
} catch (err) {
window.testPlugin = window.require(path.join(examplePluginPath, readConfig.main));
} catch (err) {
console.log(err);
}
}*/
}
module.exports = { PluginManager: _instance };

View File

@ -11,6 +11,7 @@
const { Module } = require('./modulebase');
const { BDIpc } = require('./bdipc');
const fs = window.require('fs');
const path = window.require('path');
//TODO add these to actual utils
class Utils {
@ -106,9 +107,28 @@ class FileUtils {
}
}
class Plugin {
constructor(args) {
}
start() {
if (this.onStart) return this.onStart();
return true; //Assume plugin started since it doesn't have onStart
}
stop() {
if (this.onStop) return this.onStop();
return true; //Assume plugin stopped since it doesn't have onStop
}
}
class PluginManager extends Module {
setInitialState() {
window.pm = this;
this.setState({
plugins: []
});
@ -119,23 +139,54 @@ class PluginManager extends Module {
return this.state.plugins;
}
loadPlugin(plugin) {
async loadPlugin(pluginPath) {
const { plugins } = this.state;
plugins.push(plugin);
this.setState({
plugins
});
try {
const loaded = plugins.find(plugin => plugin.pluginPath === pluginPath);
if (loaded) {
throw { 'message': 'Attempted to load an already loaded plugin' };
}
const readConfig = await this.readConfig(pluginPath);
const mainPath = path.join(pluginPath, readConfig.main);
const plugin = window.require(mainPath)(Plugin, {}, {});
const instance = new plugin();
plugins.push(Object.assign({
pluginPath,
instance
},readConfig));
this.setState(plugins);
return instance;
} catch (err) {
throw err;
}
}
getPluginByName(name) {
return this.plugins.find(plugin => plugin.name === name);
async reloadPlugin(pluginPath) {
//TODO Cleanup loaded plugin
return await this.loadPlugin(pluginPath);
}
getPluginById(id) {
return this.plugins.find(plugin => plugin.id === id);
getPluginByName(name) { return this.plugins.find(plugin => plugin.name === name); }
getPluginById(id) { return this.plugins.find(plugin => plugin.id === id); }
stopPlugin(name) {
const plugin = this.getPluginByName(name);
if (plugin && plugin.instance) return plugin.instance.stop();
return true; //Return true anyways since plugin doesn't exist
}
startPlugin(name) {
const plugin = this.getPluginByName(name);
if (plugin && plugin.instance) return plugin.instance.start();
return true; //Return true anyways since plugin doesn't exist
}
async readConfig(path) {
path = `${path}/config.json`;
@ -148,18 +199,16 @@ const _instance = new PluginManager();
async function tests() {
const pluginName = 'Example';
const config = await BDIpc.send('getConfig');
const pluginPath = config.paths.find(path => 'plugins' in path).plugins;
console.log(`Plugin Path: ${pluginPath}`);
const examplePluginPath = `${pluginPath}/Example`;
//Test read config
try {
const readConfig = await _instance.readConfig(examplePluginPath);
console.log(readConfig);
//Load test plugin
const plugin = await _instance.loadPlugin(path.join(pluginPath, pluginName));
//Attempt to load the same plugin again
const plugin2 = await _instance.loadPlugin(path.join(pluginPath, pluginName));
} catch (err) {
console.log(err);
console.log(`Failed to load plugin! ${err.message}`);
}
}

View File

@ -0,0 +1,18 @@
module.exports = (Plugin, Api, Vendor) => {
const { $, moment } = Vendor;
const { Events } = Api;
const test = 'Testing';
return class extends Plugin {
test() {
return test;
}
onStart() {
console.log('On Start!');
}
}
}