2018-05-18 17:15:49 -04:00
|
|
|
const async = require('async');
|
|
|
|
|
2018-05-23 09:33:32 -04:00
|
|
|
const utils = require('../utils/utils');
|
2018-06-04 15:36:43 -04:00
|
|
|
const IPC = require('./ipc');
|
2018-05-22 15:46:02 -04:00
|
|
|
|
2017-03-30 20:12:39 +09:00
|
|
|
class Engine {
|
|
|
|
constructor(options) {
|
|
|
|
this.env = options.env;
|
2018-05-18 15:48:28 -04:00
|
|
|
this.client = options.client;
|
|
|
|
this.locale = options.locale;
|
2017-03-30 20:12:39 +09:00
|
|
|
this.embarkConfig = options.embarkConfig;
|
|
|
|
this.interceptLogs = options.interceptLogs;
|
2017-03-31 07:34:43 -04:00
|
|
|
this.version = options.version;
|
2018-04-19 14:25:43 +10:00
|
|
|
this.logFile = options.logFile;
|
2018-04-17 16:17:59 +10:00
|
|
|
this.logLevel = options.logLevel;
|
2018-04-19 14:25:43 +10:00
|
|
|
this.events = options.events;
|
2018-04-25 10:34:17 -04:00
|
|
|
this.context = options.context;
|
2018-06-15 09:37:52 +10:00
|
|
|
this.useDashboard = options.useDashboard;
|
2018-07-15 23:49:24 -05:00
|
|
|
this.webServerConfig = options.webServerConfig;
|
2018-08-16 15:51:34 -05:00
|
|
|
this.webpackConfigName = options.webpackConfigName;
|
2018-09-12 10:16:27 +01:00
|
|
|
this.ipcRole = options.ipcRole || 'client';
|
2017-03-30 20:12:39 +09:00
|
|
|
}
|
2017-03-03 01:22:12 -05:00
|
|
|
|
2018-09-05 10:39:09 +01:00
|
|
|
init(_options, callback) {
|
|
|
|
callback = callback || function() {};
|
2018-06-02 09:54:32 -04:00
|
|
|
const Events = require('./events.js');
|
|
|
|
const Logger = require('./logger.js');
|
|
|
|
const Config = require('./config.js');
|
|
|
|
|
2017-03-30 20:38:14 +09:00
|
|
|
let options = _options || {};
|
2018-04-19 14:25:43 +10:00
|
|
|
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});
|
2018-07-15 23:49:24 -05:00
|
|
|
this.config = new Config({env: this.env, logger: this.logger, events: this.events, context: this.context, webServerConfig: this.webServerConfig});
|
2017-03-30 20:38:14 +09:00
|
|
|
this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs});
|
|
|
|
this.plugins = this.config.plugins;
|
2018-07-10 08:49:08 -04:00
|
|
|
this.isDev = this.config && this.config.blockchainConfig && (this.config.blockchainConfig.isDev || this.config.blockchainConfig.default);
|
2017-03-30 20:38:14 +09:00
|
|
|
|
2018-04-30 15:56:43 +10:00
|
|
|
if (this.interceptLogs || this.interceptLogs === undefined) {
|
2018-08-22 18:04:52 -04:00
|
|
|
utils.interceptLogs(console, this.logger);
|
2018-04-30 15:56:43 +10:00
|
|
|
}
|
2018-08-08 13:42:45 +01:00
|
|
|
|
2018-09-12 10:16:27 +01:00
|
|
|
this.ipc = new IPC({logger: this.logger, ipcRole: this.ipcRole});
|
|
|
|
if (this.ipc.isClient()) {
|
|
|
|
return this.ipc.connect((_err) => {
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
} else if (this.ipc.isServer()) {
|
|
|
|
this.ipc.serve();
|
|
|
|
return callback();
|
|
|
|
}
|
2018-07-19 23:31:28 +03:00
|
|
|
callback();
|
2018-04-30 15:56:43 +10:00
|
|
|
}
|
|
|
|
|
2017-12-16 15:39:30 -05:00
|
|
|
registerModule(moduleName, options) {
|
2018-05-30 12:26:49 -04:00
|
|
|
this.plugins.loadInternalPlugin(moduleName, options || {});
|
2017-12-16 15:39:30 -05:00
|
|
|
}
|
|
|
|
|
2017-03-30 20:38:14 +09:00
|
|
|
startService(serviceName, _options) {
|
|
|
|
let options = _options || {};
|
|
|
|
|
|
|
|
let services = {
|
2018-06-01 12:27:12 -04:00
|
|
|
"serviceMonitor": this.serviceMonitor,
|
2017-03-30 20:38:14 +09:00
|
|
|
"pipeline": this.pipelineService,
|
2018-05-23 11:16:56 -04:00
|
|
|
"codeRunner": this.codeRunnerService,
|
2017-08-03 19:29:09 -04:00
|
|
|
"codeGenerator": this.codeGeneratorService,
|
2018-10-09 15:12:33 +02:00
|
|
|
"compiler": this.setupCompilerAndContractsManagerService,
|
2017-03-30 20:38:14 +09:00
|
|
|
"deployment": this.deploymentService,
|
|
|
|
"fileWatcher": this.fileWatchService,
|
|
|
|
"webServer": this.webServerService,
|
2018-05-25 12:25:02 -05:00
|
|
|
"namingSystem": this.namingSystem,
|
2018-08-31 09:24:45 +01:00
|
|
|
"console": this.console,
|
2017-12-30 15:52:51 -05:00
|
|
|
"web3": this.web3Service,
|
2018-04-24 10:27:11 +10:00
|
|
|
"libraryManager": this.libraryManagerService,
|
2018-07-19 23:31:28 +03:00
|
|
|
"processManager": this.processManagerService,
|
2018-07-20 20:03:55 +03:00
|
|
|
"storage": this.storageService,
|
2018-08-22 18:04:52 -04:00
|
|
|
"pluginCommand": this.pluginCommandService
|
2018-08-01 12:08:47 -04:00
|
|
|
"graph": this.graphService,
|
2018-09-09 23:07:55 +05:30
|
|
|
"codeCoverage": this.codeCoverageService,
|
2018-08-22 18:04:52 -04:00
|
|
|
"testRunner": this.testRunnerService
|
2017-03-30 20:38:14 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
let service = services[serviceName];
|
|
|
|
|
|
|
|
if (!service) {
|
|
|
|
throw new Error("unknown service: " + serviceName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// need to be careful with circular references due to passing the web3 object
|
|
|
|
//this.logger.trace("calling: " + serviceName + "(" + JSON.stringify(options) + ")");
|
|
|
|
return service.apply(this, [options]);
|
|
|
|
}
|
|
|
|
|
2018-07-19 23:31:28 +03:00
|
|
|
processManagerService(_options) {
|
2018-08-22 18:16:03 -04:00
|
|
|
const ProcessManager = require('./processes/processManager.js');
|
2018-08-22 18:04:52 -04:00
|
|
|
this.processManager = new ProcessManager({
|
2018-07-19 23:31:28 +03:00
|
|
|
events: this.events,
|
|
|
|
logger: this.logger,
|
|
|
|
plugins: this.plugins
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-22 18:16:03 -04:00
|
|
|
graphService(_options) {
|
|
|
|
this.registerModule('graph');
|
|
|
|
}
|
|
|
|
|
2017-10-14 10:13:30 -04:00
|
|
|
pipelineService(_options) {
|
2018-05-10 10:10:09 -04:00
|
|
|
const self = this;
|
2018-10-03 16:02:29 +02:00
|
|
|
this.registerModule('pipeline', {
|
2018-08-16 15:51:34 -05:00
|
|
|
webpackConfigName: this.webpackConfigName
|
2018-05-10 10:10:09 -04:00
|
|
|
});
|
2018-10-17 10:29:53 +11:00
|
|
|
this.events.on('code-generator-ready', function (modifiedAssets) {
|
2018-05-10 10:10:09 -04:00
|
|
|
self.events.request('code', function (abi, contractsJSON) {
|
2018-10-17 10:29:53 +11:00
|
|
|
self.events.request('pipeline:build', {abi, contractsJSON, modifiedAssets}, () => {
|
2018-05-10 10:10:09 -04:00
|
|
|
self.events.emit('outputDone');
|
2017-07-06 18:48:20 -04:00
|
|
|
});
|
2017-07-05 08:35:51 -04:00
|
|
|
});
|
2017-03-30 20:38:14 +09:00
|
|
|
});
|
2017-03-16 07:31:52 -04:00
|
|
|
}
|
2017-03-30 20:38:14 +09:00
|
|
|
|
2018-06-01 12:27:12 -04:00
|
|
|
serviceMonitor() {
|
|
|
|
const self = this;
|
2018-06-02 09:54:32 -04:00
|
|
|
const ServicesMonitor = require('./services_monitor.js');
|
2018-06-01 12:27:12 -04:00
|
|
|
this.servicesMonitor = new ServicesMonitor({events: this.events, logger: this.logger, plugins: this.plugins});
|
|
|
|
this.servicesMonitor.addCheck('embarkVersion', function (cb) {
|
|
|
|
return cb({name: 'Embark ' + self.version, status: 'on'});
|
|
|
|
}, 0);
|
|
|
|
this.servicesMonitor.startMonitor();
|
|
|
|
}
|
|
|
|
|
2018-09-09 23:07:55 +05:30
|
|
|
pluginCommandService() {
|
2018-09-10 22:38:17 +05:30
|
|
|
this.registerModule('plugin_cmd', {embarkConfigFile: this.embarkConfig, embarkConfig: this.config.embarkConfig, packageFile: 'package.json'});
|
2018-09-09 23:07:55 +05:30
|
|
|
}
|
|
|
|
|
2018-05-28 11:10:09 -04:00
|
|
|
namingSystem(_options) {
|
2018-05-30 12:26:49 -04:00
|
|
|
this.registerModule('ens');
|
2018-05-28 11:10:09 -04:00
|
|
|
}
|
|
|
|
|
2018-08-31 09:24:45 +01:00
|
|
|
console(_options) {
|
|
|
|
this.registerModule('console', {
|
|
|
|
events: this.events,
|
|
|
|
plugins: this.plugins,
|
|
|
|
version: this.version,
|
|
|
|
ipc: this.ipc,
|
|
|
|
logger: this.logger,
|
|
|
|
config: this.config
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-23 11:16:56 -04:00
|
|
|
codeRunnerService(_options) {
|
2018-08-22 18:04:52 -04:00
|
|
|
const CodeRunner = require('./modules/coderunner/codeRunner.js');
|
2018-05-21 17:22:19 -04:00
|
|
|
this.codeRunner = new CodeRunner({
|
2018-08-28 14:43:52 +01:00
|
|
|
config: this.config,
|
2018-05-23 11:16:56 -04:00
|
|
|
plugins: this.plugins,
|
|
|
|
events: this.events,
|
2018-08-22 18:04:52 -04:00
|
|
|
logger: this.logger,
|
|
|
|
ipc: this.ipc
|
2018-05-21 17:22:19 -04:00
|
|
|
});
|
2018-05-23 11:16:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
codeGeneratorService(_options) {
|
|
|
|
let self = this;
|
2018-05-21 17:22:19 -04:00
|
|
|
|
2018-08-22 18:04:52 -04:00
|
|
|
this.registerModule('code_generator', {plugins: self.plugins, env: self.env});
|
2018-05-23 11:16:13 -04:00
|
|
|
|
2018-10-17 10:29:53 +11:00
|
|
|
const generateCode = function (modifiedAssets) {
|
2018-07-27 14:54:25 -04:00
|
|
|
self.events.request("code-generator:embarkjs:build", () => {
|
2018-10-17 10:29:53 +11:00
|
|
|
self.events.emit('code-generator-ready', modifiedAssets);
|
2018-01-10 10:43:25 -05:00
|
|
|
});
|
2017-03-30 20:38:14 +09:00
|
|
|
};
|
2018-10-17 10:29:53 +11:00
|
|
|
const cargo = async.cargo((tasks, callback) => {
|
2018-10-22 14:25:18 -04:00
|
|
|
const modifiedAssets = tasks.map(task => task.modifiedAsset).filter(asset => asset); // filter null elements
|
2018-10-17 10:29:53 +11:00
|
|
|
generateCode(modifiedAssets);
|
2018-05-15 14:42:06 -04:00
|
|
|
self.events.once('outputDone', callback);
|
2018-10-17 10:29:53 +11:00
|
|
|
});
|
2018-10-11 22:19:27 +11:00
|
|
|
const addToCargo = function (modifiedAsset) {
|
|
|
|
cargo.push({modifiedAsset});
|
2018-05-15 14:42:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
this.events.on('contractsDeployed', addToCargo);
|
|
|
|
this.events.on('blockchainDisabled', addToCargo);
|
|
|
|
this.events.on('asset-changed', addToCargo);
|
2017-03-03 01:22:12 -05:00
|
|
|
}
|
2017-03-03 21:48:32 -05:00
|
|
|
|
2018-10-09 15:12:33 +02:00
|
|
|
setupCompilerAndContractsManagerService(options) {
|
|
|
|
this.registerModule('compiler', {plugins: this.plugins, disableOptimizations: options.disableOptimizations});
|
|
|
|
this.registerModule('solidity', {ipc: this.ipc, useDashboard: this.useDashboard});
|
|
|
|
this.registerModule('vyper');
|
|
|
|
this.registerModule('contracts_manager', {compileOnceOnly: options.compileOnceOnly});
|
|
|
|
}
|
|
|
|
|
2017-03-30 20:38:14 +09:00
|
|
|
deploymentService(options) {
|
|
|
|
let self = this;
|
2017-12-16 15:39:30 -05:00
|
|
|
|
2018-10-09 15:12:33 +02:00
|
|
|
this.setupCompilerAndContractsManagerService(options);
|
2018-05-30 12:26:49 -04:00
|
|
|
this.registerModule('profiler');
|
2018-08-24 09:25:47 -04:00
|
|
|
this.registerModule('deploytracker', {trackContracts: options.trackContracts});
|
2018-05-30 12:26:49 -04:00
|
|
|
this.registerModule('specialconfigs');
|
2018-10-05 14:49:10 +05:30
|
|
|
this.registerModule('ens');
|
2018-08-08 13:42:45 +01:00
|
|
|
this.registerModule('console_listener', {ipc: self.ipc});
|
2018-07-27 16:03:15 -04:00
|
|
|
this.registerModule('deployment', {plugins: this.plugins, onlyCompile: options.onlyCompile});
|
2017-03-30 20:38:14 +09:00
|
|
|
|
2018-10-11 22:19:27 +11:00
|
|
|
this.events.on('file-event', function ({fileType, path}) {
|
2018-05-15 15:41:24 -04:00
|
|
|
clearTimeout(self.fileTimeout);
|
|
|
|
self.fileTimeout = setTimeout(() => {
|
|
|
|
// TODO: still need to redeploy contracts because the original contracts
|
|
|
|
// config is being corrupted
|
2018-08-22 18:04:52 -04:00
|
|
|
self.config.reloadConfig();
|
|
|
|
|
2018-05-15 15:41:24 -04:00
|
|
|
if (fileType === 'asset') {
|
|
|
|
// Throttle file changes so we re-write only once for all files
|
2018-10-11 22:19:27 +11:00
|
|
|
self.events.emit('asset-changed', path);
|
2018-05-15 15:41:24 -04:00
|
|
|
}
|
|
|
|
// TODO: for now need to deploy on asset changes as well
|
|
|
|
// because the contractsManager config is corrupted after a deploy
|
|
|
|
if (fileType === 'contract' || fileType === 'config') {
|
2018-05-29 17:23:29 -04:00
|
|
|
self.events.request('deploy:contracts', () => {});
|
2018-05-15 15:41:24 -04:00
|
|
|
}
|
|
|
|
}, 50);
|
2017-03-30 20:12:39 +09:00
|
|
|
});
|
|
|
|
}
|
2017-03-10 22:00:30 -05:00
|
|
|
|
2018-10-03 17:15:16 +02:00
|
|
|
fileWatchService() {
|
|
|
|
this.registerModule('watcher');
|
|
|
|
this.events.request('watcher:start');
|
2017-03-30 20:38:14 +09:00
|
|
|
}
|
|
|
|
|
2018-09-17 17:51:46 -05:00
|
|
|
webServerService() {
|
2018-07-12 16:02:16 +03:00
|
|
|
this.registerModule('webserver', {plugins: this.plugins});
|
2017-03-30 20:38:14 +09:00
|
|
|
}
|
|
|
|
|
2018-05-25 17:13:57 +10:00
|
|
|
storageService(_options) {
|
2018-07-08 23:41:37 +03:00
|
|
|
this.registerModule('storage', {plugins: this.plugins});
|
2018-07-08 20:40:06 +03:00
|
|
|
this.registerModule('ipfs');
|
|
|
|
this.registerModule('swarm');
|
2018-04-24 10:27:11 +10:00
|
|
|
}
|
|
|
|
|
2017-03-30 20:38:14 +09:00
|
|
|
web3Service(options) {
|
2018-07-20 15:21:23 +03:00
|
|
|
this.registerModule('blockchain_process', {
|
2018-10-06 18:05:37 +02:00
|
|
|
client: this.client,
|
2018-07-20 15:21:23 +03:00
|
|
|
locale: this.locale,
|
2018-08-23 12:54:43 -04:00
|
|
|
isDev: this.isDev,
|
|
|
|
ipc: this.ipc
|
2018-07-20 15:21:23 +03:00
|
|
|
});
|
|
|
|
|
2018-07-27 16:54:33 -04:00
|
|
|
this.registerModule('blockchain_connector', {
|
2018-05-18 14:58:05 -04:00
|
|
|
isDev: this.isDev,
|
2018-07-19 23:31:28 +03:00
|
|
|
locale: this.locale,
|
2018-07-31 15:46:19 +01:00
|
|
|
plugins: this.plugins,
|
2018-10-09 16:43:38 -04:00
|
|
|
web3: options.web3,
|
|
|
|
wait: options.wait
|
2018-07-27 16:54:33 -04:00
|
|
|
});
|
2018-04-10 15:14:00 -04:00
|
|
|
|
2018-08-22 18:04:52 -04:00
|
|
|
this.registerModule('whisper');
|
2017-03-30 20:38:14 +09:00
|
|
|
}
|
2017-12-30 15:52:51 -05:00
|
|
|
|
2017-12-30 17:07:13 -05:00
|
|
|
libraryManagerService(_options) {
|
2018-08-22 18:04:52 -04:00
|
|
|
this.registerModule('library_manager');
|
|
|
|
}
|
|
|
|
|
|
|
|
codeCoverageService(_options) {
|
|
|
|
this.registerModule('coverage');
|
2018-08-22 18:04:52 -04:00
|
|
|
}
|
|
|
|
|
2018-10-18 08:36:34 -04:00
|
|
|
testRunnerService(options) {
|
2018-10-18 08:38:29 -04:00
|
|
|
this.registerModule('tests', Object.assign(options, {ipc: this.ipc}));
|
2018-10-18 08:36:34 -04:00
|
|
|
}
|
|
|
|
|
2018-08-01 12:08:47 -04:00
|
|
|
codeCoverageService(_options) {
|
|
|
|
this.registerModule('coverage');
|
|
|
|
}
|
2017-03-30 20:38:14 +09:00
|
|
|
}
|
2017-03-11 10:29:45 -05:00
|
|
|
|
2017-03-03 01:22:12 -05:00
|
|
|
module.exports = Engine;
|