Skip to content

Commit f39acbd

Browse files
committed
get context from cmd ran and use arrays of contexts
1 parent c77d52c commit f39acbd

File tree

6 files changed

+91
-29
lines changed

6 files changed

+91
-29
lines changed

lib/constants.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
"contexts": {
44
"simulator": "simulator",
55
"blockchain": "blockchain",
6+
"templateGeneration": "templateGeneration",
7+
"run": "run",
8+
"upload": "upload",
9+
"build": "build",
10+
"graph": "graph",
11+
"test": "test",
12+
"reset": "reset",
613
"any": "any"
714
},
815
"events": {

lib/core/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var Config = function(options) {
2020
this.logger = options.logger;
2121
this.events = options.events;
2222
this.embarkConfig = {};
23+
this.context = options.context || [constants.contexts.any];
2324
};
2425

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

39-
this.plugins = new Plugins({plugins: this.embarkConfig.plugins, logger: this.logger, interceptLogs: interceptLogs, events: this.events, config: this});
40+
this.plugins = new Plugins({plugins: this.embarkConfig.plugins, logger: this.logger, interceptLogs: interceptLogs, events: this.events, config: this, context: this.context});
4041
this.plugins.loadPlugins();
4142

4243
this.loadEmbarkConfigFile();

lib/core/engine.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ let ServicesMonitor = require('./services_monitor.js');
99
let Pipeline = require('../pipeline/pipeline.js');
1010
let Watch = require('../pipeline/watch.js');
1111
let LibraryManager = require('../versions/library_manager.js');
12-
const constants = require('../constants');
1312

1413
class Engine {
1514
constructor(options) {
@@ -20,15 +19,15 @@ class Engine {
2019
this.logFile = options.logFile;
2120
this.logLevel = options.logLevel;
2221
this.events = options.events;
23-
this.context = constants.contexts.simulator; // Will change to blockchain once we can connect to the blockchain
22+
this.context = options.context;
2423
}
2524

2625
init(_options) {
2726
let self = this;
2827
let options = _options || {};
2928
this.events = options.events || this.events || new Events();
3029
this.logger = options.logger || new Logger({logLevel: options.logLevel || this.logLevel || 'debug', events: this.events, logFile: this.logFile});
31-
this.config = new Config({env: this.env, logger: this.logger, events: this.events});
30+
this.config = new Config({env: this.env, logger: this.logger, events: this.events, context: this.context});
3231
this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs});
3332
this.plugins = this.config.plugins;
3433

@@ -228,15 +227,6 @@ class Engine {
228227
return cb({name: version, status: 'on'});
229228
}
230229
let nodeName = version.split("/")[0];
231-
const oldContext = self.context;
232-
if (nodeName === 'Geth' || nodeName.toLowerCase().indexOf('test') < 0) {
233-
self.context = constants.contexts.blockchain;
234-
} else {
235-
self.context = constants.contexts.simulator;
236-
}
237-
if (oldContext !== self.context) {
238-
self.events.emit(constants.events.contextChange, self.context);
239-
}
240230
let versionNumber = version.split("/")[1].split("-")[0];
241231
let name = nodeName + " " + versionNumber + " (Ethereum)";
242232

lib/core/plugin.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,33 @@ var Plugin = function(options) {
2929
this.events = options.events;
3030
this.config = options.config;
3131
this.loaded = false;
32-
this.context = options.pluginConfig.context || constants.contexts.any;
32+
this.currentContext = options.context;
33+
this.acceptedContext = options.pluginConfig.context || [constants.contexts.any];
34+
35+
if (!Array.isArray(this.currentContext)) {
36+
this.currentContext = [this.currentContext];
37+
}
38+
if (!Array.isArray(this.acceptedContext)) {
39+
this.acceptedContext = [this.acceptedContext];
40+
}
41+
};
42+
43+
Plugin.prototype.isContextValid = function() {
44+
if (this.currentContext.includes(constants.contexts.any) || this.acceptedContext.includes(constants.contexts.any)) {
45+
return true;
46+
}
47+
return this.acceptedContext.some(context => {
48+
return this.currentContext.includes(context);
49+
});
3350
};
3451

35-
Plugin.prototype.loadPlugin = function(currentContext) {
36-
if (this.context !== constants.contexts.any && this.context !== currentContext) {
37-
if (currentContext) {
38-
this.logger.warn(`Plugin ${this.name} can only be loaded in the context of the ${this.context}`);
39-
}
40-
return this.events.on(constants.events.contextChange, this.loadPlugin.bind(this));
52+
Plugin.prototype.loadPlugin = function() {
53+
if (!this.isContextValid()) {
54+
console.log(this.acceptedContext);
55+
this.logger.warn(`Plugin ${this.name} can only be loaded in the context of "${this.acceptedContext.join(', ')}"`);
56+
return false;
4157
}
4258
this.loaded = true;
43-
this.logger.info(`Loaded plugin ${this.name}`);
44-
this.events.removeListener(constants.events.contextChange, this.loadPlugin.bind(this));
4559
if (this.shouldInterceptLogs) {
4660
this.interceptLogs(this.pluginModule);
4761
}

lib/core/plugins.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var Plugins = function(options) {
99
this.logger = options.logger;
1010
this.events = options.events;
1111
this.config = options.config;
12+
this.context = options.context;
1213
};
1314

1415
Plugins.prototype.loadPlugins = function() {
@@ -33,7 +34,18 @@ Plugins.prototype.listPlugins = function() {
3334
Plugins.prototype.createPlugin = function(pluginName, pluginConfig) {
3435
let plugin = {};
3536
let pluginPath = false;
36-
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});
37+
var pluginWrapper = new Plugin({
38+
name: pluginName,
39+
pluginModule: plugin,
40+
pluginConfig: pluginConfig,
41+
logger: this.logger,
42+
pluginPath: pluginPath,
43+
interceptLogs: this.interceptLogs,
44+
events: this.events,
45+
config: this.config,
46+
isInternal: true,
47+
context: this.context
48+
});
3749
this.plugins.push(pluginWrapper);
3850
return pluginWrapper;
3951
};
@@ -42,7 +54,18 @@ Plugins.prototype.loadInternalPlugin = function(pluginName, pluginConfig) {
4254
var pluginPath = utils.joinPath('../modules/', pluginName, 'index.js');
4355
var plugin = require(pluginPath);
4456

45-
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});
57+
var pluginWrapper = new Plugin({
58+
name: pluginName,
59+
pluginModule: plugin,
60+
pluginConfig: pluginConfig,
61+
logger: this.logger,
62+
pluginPath: pluginPath,
63+
interceptLogs: this.interceptLogs,
64+
events: this.events,
65+
config: this.config,
66+
isInternal: true,
67+
context: this.context
68+
});
4669
pluginWrapper.loadInternalPlugin();
4770
this.plugins.push(pluginWrapper);
4871
};
@@ -51,7 +74,18 @@ Plugins.prototype.loadPlugin = function(pluginName, pluginConfig) {
5174
var pluginPath = utils.joinPath(utils.pwd(), 'node_modules', pluginName);
5275
var plugin = require(pluginPath);
5376

54-
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});
77+
var pluginWrapper = new Plugin({
78+
name: pluginName,
79+
pluginModule: plugin,
80+
pluginConfig: pluginConfig,
81+
logger: this.logger,
82+
pluginPath: pluginPath,
83+
interceptLogs: this.interceptLogs,
84+
events: this.events,
85+
config: this.config,
86+
isInternal: false,
87+
context: this.context
88+
});
5589
pluginWrapper.loadPlugin();
5690
this.plugins.push(pluginWrapper);
5791
};

lib/index.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
let async = require('async');
2+
const constants = require('./constants');
23
// require("./utils/debug_util.js")(__filename, async);
34

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

34-
this.config = new Config({env: env, logger: this.logger, events: this.events});
35+
this.config = new Config({env: env, logger: this.logger, events: this.events, context: this.context});
3536
this.config.loadConfigFiles(options);
3637
this.plugins = this.config.plugins;
3738
}
3839

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

4345
simulator(options) {
46+
this.context = options.context || [constants.contexts.simulator, constants.contexts.blockchain];
4447
let Simulator = require('./cmds/simulator.js');
4548
let simulator = new Simulator({blockchainConfig: this.config.blockchainConfig});
4649
simulator.run(options);
4750
}
4851

4952
generateTemplate(templateName, destinationFolder, name) {
53+
this.context = [constants.contexts.templateGeneration];
5054
let TemplateGenerator = require('./cmds/template_generator.js');
5155
let templateGenerator = new TemplateGenerator(templateName);
5256
templateGenerator.generate(destinationFolder, name);
5357
}
5458

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

@@ -62,7 +67,8 @@ class Embark {
6267
version: this.version,
6368
embarkConfig: options.embarkConfig || 'embark.json',
6469
logFile: options.logFile,
65-
logLevel: options.logLevel
70+
logLevel: options.logLevel,
71+
context: self.context
6672
});
6773
engine.init();
6874

@@ -152,6 +158,7 @@ class Embark {
152158
}
153159

154160
build(options, continueProcessing) {
161+
this.context = options.context || [constants.contexts.build];
155162
let engine = new Engine({
156163
env: options.env,
157164
version: this.version,
@@ -162,7 +169,8 @@ class Embark {
162169
events: options.events,
163170
logger: options.logger,
164171
config: options.config,
165-
plugins: options.plugins
172+
plugins: options.plugins,
173+
context: this.context
166174
});
167175
engine.init();
168176

@@ -201,18 +209,22 @@ class Embark {
201209
}
202210

203211
initTests(options) {
212+
this.context = options.context || [constants.contexts.test];
204213
let Test = require('./tests/test.js');
214+
options.context = this.context;
205215
return new Test(options);
206216
}
207217

208218
graph(options) {
219+
this.context = options.context || [constants.contexts.graph];
209220
options.onlyCompile = true;
210221

211222
let engine = new Engine({
212223
env: options.env,
213224
version: this.version,
214225
embarkConfig: options.embarkConfig || 'embark.json',
215-
logFile: options.logFile
226+
logFile: options.logFile,
227+
context: this.context
216228
});
217229
engine.init();
218230

@@ -253,11 +265,13 @@ class Embark {
253265
}
254266

255267
reset() {
268+
this.context = [constants.contexts.reset];
256269
let resetCmd = require('./cmds/reset.js');
257270
resetCmd();
258271
}
259272

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

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

320334
runTests(file) {
335+
this.context = [constants.contexts.test];
321336
let RunTests = require('./tests/run_tests.js');
322337
RunTests.run(file);
323338
}
@@ -327,6 +342,7 @@ class Embark {
327342
// temporary until next refactor
328343
Embark.initTests = function(options) {
329344
let Test = require('./tests/test.js');
345+
options.context = [constants.contexts.test];
330346
return new Test(options);
331347
};
332348

0 commit comments

Comments
 (0)