Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
"contexts": {
"simulator": "simulator",
"blockchain": "blockchain",
"templateGeneration": "templateGeneration",
"run": "run",
"upload": "upload",
"build": "build",
"graph": "graph",
"test": "test",
"reset": "reset",
"any": "any"
},
"events": {
Expand Down
3 changes: 2 additions & 1 deletion lib/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var Config = function(options) {
this.logger = options.logger;
this.events = options.events;
this.embarkConfig = {};
this.context = options.context || [constants.contexts.any];
};

Config.prototype.loadConfigFiles = function(options) {
Expand All @@ -36,7 +37,7 @@ Config.prototype.loadConfigFiles = function(options) {
this.embarkConfig = fs.readJSONSync(options.embarkConfig);
this.embarkConfig.plugins = this.embarkConfig.plugins || {};

this.plugins = new Plugins({plugins: this.embarkConfig.plugins, logger: this.logger, interceptLogs: interceptLogs, events: this.events, config: this});
this.plugins = new Plugins({plugins: this.embarkConfig.plugins, logger: this.logger, interceptLogs: interceptLogs, events: this.events, config: this, context: this.context});
this.plugins.loadPlugins();

this.loadEmbarkConfigFile();
Expand Down
14 changes: 2 additions & 12 deletions lib/core/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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) {
Expand All @@ -20,15 +19,15 @@ 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
this.context = options.context;
}

init(_options) {
let self = this;
let options = _options || {};
this.events = options.events || this.events || new Events();
this.logger = options.logger || new Logger({logLevel: options.logLevel || this.logLevel || 'debug', events: this.events, logFile: this.logFile});
this.config = new Config({env: this.env, logger: this.logger, events: this.events});
this.config = new Config({env: this.env, logger: this.logger, events: this.events, context: this.context});
this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs});
this.plugins = this.config.plugins;

Expand Down Expand Up @@ -228,15 +227,6 @@ 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) {
self.events.emit(constants.events.contextChange, self.context);
}
let versionNumber = version.split("/")[1].split("-")[0];
let name = nodeName + " " + versionNumber + " (Ethereum)";

Expand Down
36 changes: 27 additions & 9 deletions lib/core/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,37 @@ var Plugin = function(options) {
this.events = options.events;
this.config = options.config;
this.loaded = false;
this.context = options.pluginConfig.context || constants.contexts.any;
this.currentContext = options.context;
this.acceptedContext = options.pluginConfig.context || [constants.contexts.any];

if (!Array.isArray(this.currentContext)) {
this.currentContext = [this.currentContext];
}
if (!Array.isArray(this.acceptedContext)) {
this.acceptedContext = [this.acceptedContext];
}
};

Plugin.prototype.isContextValid = function() {
if (this.currentContext.includes(constants.contexts.any) || this.acceptedContext.includes(constants.contexts.any)) {
return true;
}
return this.acceptedContext.some(context => {
return this.currentContext.includes(context);
});
};

Plugin.prototype.hasContext = function(context) {
return this.currentContext.includes(context);
};

Plugin.prototype.loadPlugin = function(currentContext) {
if (this.context !== constants.contexts.any && this.context !== currentContext) {
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));
Plugin.prototype.loadPlugin = function() {
if (!this.isContextValid()) {
console.log(this.acceptedContext);
this.logger.warn(`Plugin ${this.name} can only be loaded in the context of "${this.acceptedContext.join(', ')}"`);
return false;
}
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);
}
Expand Down
40 changes: 37 additions & 3 deletions lib/core/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var Plugins = function(options) {
this.logger = options.logger;
this.events = options.events;
this.config = options.config;
this.context = options.context;
};

Plugins.prototype.loadPlugins = function() {
Expand All @@ -33,7 +34,18 @@ Plugins.prototype.listPlugins = function() {
Plugins.prototype.createPlugin = function(pluginName, pluginConfig) {
let plugin = {};
let pluginPath = false;
var pluginWrapper = new Plugin({name: pluginName, pluginModule: plugin, pluginConfig: pluginConfig, logger: this.logger, pluginPath: pluginPath, interceptLogs: this.interceptLogs, events: this.events, config: this.config, isInternal: true});
var pluginWrapper = new Plugin({
name: pluginName,
pluginModule: plugin,
pluginConfig: pluginConfig,
logger: this.logger,
pluginPath: pluginPath,
interceptLogs: this.interceptLogs,
events: this.events,
config: this.config,
isInternal: true,
context: this.context
});
this.plugins.push(pluginWrapper);
return pluginWrapper;
};
Expand All @@ -42,7 +54,18 @@ Plugins.prototype.loadInternalPlugin = function(pluginName, pluginConfig) {
var pluginPath = utils.joinPath('../modules/', pluginName, 'index.js');
var plugin = require(pluginPath);

var pluginWrapper = new Plugin({name: pluginName, pluginModule: plugin, pluginConfig: pluginConfig, logger: this.logger, pluginPath: pluginPath, interceptLogs: this.interceptLogs, events: this.events, config: this.config, isInternal: true});
var pluginWrapper = new Plugin({
name: pluginName,
pluginModule: plugin,
pluginConfig: pluginConfig,
logger: this.logger,
pluginPath: pluginPath,
interceptLogs: this.interceptLogs,
events: this.events,
config: this.config,
isInternal: true,
context: this.context
});
pluginWrapper.loadInternalPlugin();
this.plugins.push(pluginWrapper);
};
Expand All @@ -51,7 +74,18 @@ Plugins.prototype.loadPlugin = function(pluginName, pluginConfig) {
var pluginPath = utils.joinPath(utils.pwd(), 'node_modules', pluginName);
var plugin = require(pluginPath);

var pluginWrapper = new Plugin({name: pluginName, pluginModule: plugin, pluginConfig: pluginConfig, logger: this.logger, pluginPath: pluginPath, interceptLogs: this.interceptLogs, events: this.events, config: this.config, isInternal: false});
var pluginWrapper = new Plugin({
name: pluginName,
pluginModule: plugin,
pluginConfig: pluginConfig,
logger: this.logger,
pluginPath: pluginPath,
interceptLogs: this.interceptLogs,
events: this.events,
config: this.config,
isInternal: false,
context: this.context
});
pluginWrapper.loadPlugin();
this.plugins.push(pluginWrapper);
};
Expand Down
24 changes: 20 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let async = require('async');
const constants = require('./constants');
// require("./utils/debug_util.js")(__filename, async);

require('colors');
Expand Down Expand Up @@ -31,29 +32,33 @@ class Embark {
this.events = new Events();
this.logger = new Logger({logLevel: 'debug', events: this.events});

this.config = new Config({env: env, logger: this.logger, events: this.events});
this.config = new Config({env: env, logger: this.logger, events: this.events, context: this.context});
this.config.loadConfigFiles(options);
this.plugins = this.config.plugins;
}

blockchain(env, client) {
this.context = [constants.contexts.blockchain];
return require('./cmds/blockchain/blockchain.js')(this.config.blockchainConfig, client, env).run();
}

simulator(options) {
this.context = options.context || [constants.contexts.simulator, constants.contexts.blockchain];
let Simulator = require('./cmds/simulator.js');
let simulator = new Simulator({blockchainConfig: this.config.blockchainConfig});
simulator.run(options);
}

generateTemplate(templateName, destinationFolder, name) {
this.context = [constants.contexts.templateGeneration];
let TemplateGenerator = require('./cmds/template_generator.js');
let templateGenerator = new TemplateGenerator(templateName);
templateGenerator.generate(destinationFolder, name);
}

run(options) {
let self = this;
self.context = options.context || [constants.contexts.run, constants.contexts.build];
let Dashboard = require('./dashboard/dashboard.js');
let windowSize = require('window-size');

Expand All @@ -62,7 +67,8 @@ class Embark {
version: this.version,
embarkConfig: options.embarkConfig || 'embark.json',
logFile: options.logFile,
logLevel: options.logLevel
logLevel: options.logLevel,
context: self.context
});
engine.init();

Expand Down Expand Up @@ -152,6 +158,7 @@ class Embark {
}

build(options, continueProcessing) {
this.context = options.context || [constants.contexts.build];
let engine = new Engine({
env: options.env,
version: this.version,
Expand All @@ -162,7 +169,8 @@ class Embark {
events: options.events,
logger: options.logger,
config: options.config,
plugins: options.plugins
plugins: options.plugins,
context: this.context
});
engine.init();

Expand Down Expand Up @@ -201,18 +209,22 @@ class Embark {
}

initTests(options) {
this.context = options.context || [constants.contexts.test];
let Test = require('./tests/test.js');
options.context = this.context;
return new Test(options);
}

graph(options) {
this.context = options.context || [constants.contexts.graph];
options.onlyCompile = true;

let engine = new Engine({
env: options.env,
version: this.version,
embarkConfig: options.embarkConfig || 'embark.json',
logFile: options.logFile
logFile: options.logFile,
context: this.context
});
engine.init();

Expand Down Expand Up @@ -253,11 +265,13 @@ class Embark {
}

reset() {
this.context = [constants.contexts.reset];
let resetCmd = require('./cmds/reset.js');
resetCmd();
}

upload(platform, options) {
this.context = options.context || [constants.contexts.upload, constants.contexts.build];

// populate options that were instantiated with initConfig to pass around
options.buildDir = 'dist/';
Expand Down Expand Up @@ -318,6 +332,7 @@ class Embark {
}

runTests(file) {
this.context = [constants.contexts.test];
let RunTests = require('./tests/run_tests.js');
RunTests.run(file);
}
Expand All @@ -327,6 +342,7 @@ class Embark {
// temporary until next refactor
Embark.initTests = function(options) {
let Test = require('./tests/test.js');
options.context = [constants.contexts.test];
return new Test(options);
};

Expand Down