From 51c2c8f88092c35dc21439b486c7d3523246d9d8 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Tue, 24 Apr 2018 14:42:56 -0400 Subject: [PATCH 1/2] check context and on changing context, load plugins that work --- lib/constants.json | 10 +++++++++- lib/core/engine.js | 12 ++++++++++++ lib/core/plugin.js | 13 ++++++++++--- lib/index.js | 6 +++--- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/lib/constants.json b/lib/constants.json index ff5e643d..0b8e20be 100644 --- a/lib/constants.json +++ b/lib/constants.json @@ -1,3 +1,11 @@ { - "httpContractsDirectory": ".embark/contracts/" + "httpContractsDirectory": ".embark/contracts/", + "contexts": { + "simulator": "simulator", + "blockchain": "blockchain", + "any": "any" + }, + "events": { + "contextChange": "contextChange" + } } diff --git a/lib/core/engine.js b/lib/core/engine.js index 51698b6d..d2dafcb2 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -9,6 +9,7 @@ let ServicesMonitor = require('./services_monitor.js'); let Pipeline = require('../pipeline/pipeline.js'); let Watch = require('../pipeline/watch.js'); let LibraryManager = require('../versions/library_manager.js'); +const constants = require('../constants'); class Engine { constructor(options) { @@ -19,6 +20,7 @@ class Engine { this.logFile = options.logFile; this.logLevel = options.logLevel; this.events = options.events; + this.context = constants.contexts.simulator; // Will change to blockchain once we can connect to the blockchain } init(_options) { @@ -226,6 +228,16 @@ class Engine { return cb({name: version, status: 'on'}); } let nodeName = version.split("/")[0]; + const oldContext = self.context; + if (nodeName === 'Geth' || nodeName.toLowerCase().indexOf('test') < 0) { + self.context = constants.contexts.blockchain; + } else { + self.context = constants.contexts.simulator; + } + if (oldContext !== self.context) { + console.log('Emiting context change'); + self.events.emit(constants.events.contextChange, self.context); + } let versionNumber = version.split("/")[1].split("-")[0]; let name = nodeName + " " + versionNumber + " (Ethereum)"; diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 1d830524..e5d979cb 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -1,5 +1,6 @@ -var fs = require('./fs.js'); -var utils = require('../utils/utils.js'); +const fs = require('./fs.js'); +const utils = require('../utils/utils.js'); +const constants = require('../constants'); // TODO: pass other params like blockchainConfig, contract files, etc.. var Plugin = function(options) { @@ -27,9 +28,15 @@ var Plugin = function(options) { this.logger = options.logger; this.events = options.events; this.config = options.config; + this.context = options.pluginConfig.context || constants.contexts.any; }; -Plugin.prototype.loadPlugin = function() { +Plugin.prototype.loadPlugin = function(currentContext) { + if (this.context !== constants.contexts.any && this.context !== currentContext) { + this.events.on(constants.events.contextChange, this.loadPlugin.bind(this)); + return; + } + this.events.removeListener(constants.events.contextChange, this.loadPlugin.bind(this)); if (this.shouldInterceptLogs) { this.interceptLogs(this.pluginModule); } diff --git a/lib/index.js b/lib/index.js index c4a03b26..78539724 100644 --- a/lib/index.js +++ b/lib/index.js @@ -67,9 +67,9 @@ class Embark { engine.init(); if (!options.useDashboard) { - console.log('========================'.bold.green); - console.log(('Welcome to Embark ' + this.version).yellow.bold); - console.log('========================'.bold.green); + engine.logger.info('========================'.bold.green); + engine.logger.info(('Welcome to Embark ' + this.version).yellow.bold); + engine.logger.info('========================'.bold.green); } async.parallel([ From 97e91d4c44119630c63e25d2dda7332a69e64aad Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Tue, 24 Apr 2018 15:53:19 -0400 Subject: [PATCH 2/2] only log loaded plugins in list plugins and warn when plugin is not right --- lib/core/engine.js | 1 - lib/core/plugin.js | 9 +++++++-- lib/core/plugins.js | 10 ++++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/core/engine.js b/lib/core/engine.js index d2dafcb2..3152544d 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -235,7 +235,6 @@ class Engine { self.context = constants.contexts.simulator; } if (oldContext !== self.context) { - console.log('Emiting context change'); self.events.emit(constants.events.contextChange, self.context); } let versionNumber = version.split("/")[1].split("-")[0]; diff --git a/lib/core/plugin.js b/lib/core/plugin.js index e5d979cb..570139f0 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -28,14 +28,19 @@ var Plugin = function(options) { this.logger = options.logger; this.events = options.events; this.config = options.config; + this.loaded = false; this.context = options.pluginConfig.context || constants.contexts.any; }; Plugin.prototype.loadPlugin = function(currentContext) { if (this.context !== constants.contexts.any && this.context !== currentContext) { - this.events.on(constants.events.contextChange, this.loadPlugin.bind(this)); - return; + if (currentContext) { + this.logger.warn(`Plugin ${this.name} can only be loaded in the context of the ${this.context}`); + } + return this.events.on(constants.events.contextChange, this.loadPlugin.bind(this)); } + this.loaded = true; + this.logger.info(`Loaded plugin ${this.name}`); this.events.removeListener(constants.events.contextChange, this.loadPlugin.bind(this)); if (this.shouldInterceptLogs) { this.interceptLogs(this.pluginModule); diff --git a/lib/core/plugins.js b/lib/core/plugins.js index 9117a25a..bcf59b91 100644 --- a/lib/core/plugins.js +++ b/lib/core/plugins.js @@ -20,10 +20,12 @@ Plugins.prototype.loadPlugins = function() { }; Plugins.prototype.listPlugins = function() { - var list = []; - for (var className in this.pluginList) { - list.push(className); - } + const list = []; + this.plugins.forEach(plugin => { + if (plugin.loaded) { + list.push(plugin.name); + } + }); return list; };