refactor, move calls to engine; fix tests

This commit is contained in:
Iuri Matias 2017-03-03 01:22:12 -05:00
parent 6784fa662c
commit cd45a2be77
7 changed files with 226 additions and 212 deletions

View File

@ -58,11 +58,7 @@ Cmd.prototype.build = function() {
.command('build [environment]')
.description('deploy and build dapp at dist/ (default: development)')
.action(function(env, options) {
self.Embark.initConfig(env || 'development', {
embarkConfig: 'embark.json',
interceptLogs: false
});
self.Embark.build(env || 'development');
self.Embark.build({env: env || 'development'});
});
};
@ -77,9 +73,6 @@ Cmd.prototype.run = function() {
.option('--no-color', 'no colors in case it\'s needed for compatbility purposes')
.description('run dapp (default: development)')
.action(function(env, options) {
self.Embark.initConfig(env || 'development', {
embarkConfig: 'embark.json'
});
self.Embark.run({
env: env || 'development',
serverPort: options.port,

View File

@ -10,6 +10,13 @@ var DeployManager = function(options) {
this.blockchainConfig = this.config.blockchainConfig;
this.plugins = options.plugins;
this.events = options.events;
this.web3 = options.web3;
// TODO: lol, redo this
if (options.trackContracts !== false) {
this.chainConfig = this.config.chainTracker;
} else {
this.chainConfig = false;
}
};
DeployManager.prototype.deployContracts = function(done) {
@ -31,19 +38,24 @@ DeployManager.prototype.deployContracts = function(done) {
});
contractsManager.build(callback);
},
function deployContracts(contractsManager, callback) {
function deployContracts2(contractsManager, callback) {
//TODO: figure out where to put this since the web3 can be passed along if needed
// perhaps it should go into the deploy object itself
// TODO: should come from the config object
var web3 = new Web3();
var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort;
web3.setProvider(new web3.providers.HttpProvider(web3Endpoint));
var web3;
if (self.web3) {
web3 = self.web3;
} else {
web3 = new Web3();
var web3Endpoint = 'http://' + self.config.blockchainConfig.rpcHost + ':' + self.config.blockchainConfig.rpcPort;
web3.setProvider(new web3.providers.HttpProvider(web3Endpoint));
if (!web3.isConnected()) {
self.logger.error(("Couldn't connect to " + web3Endpoint.underline + " are you sure it's on?").red);
self.logger.info("make sure you have an ethereum node or simulator running. e.g 'embark blockchain'".magenta);
return callback(Error("error connecting to blockchain node"));
if (!web3.isConnected()) {
self.logger.error(("Couldn't connect to " + web3Endpoint.underline + " are you sure it's on?").red);
self.logger.info("make sure you have an ethereum node or simulator running. e.g 'embark blockchain'".magenta);
return callback(Error("error connecting to blockchain node"));
}
}
web3.eth.getAccounts(function(err, accounts) {
@ -56,7 +68,7 @@ DeployManager.prototype.deployContracts = function(done) {
web3: web3,
contractsManager: contractsManager,
logger: self.logger,
chainConfig: self.config.chainTracker,
chainConfig: self.chainConfig,
env: self.config.env
});
deploy.deployAll(function() {

View File

@ -11,6 +11,7 @@ var DeployTracker = function(options) {
return;
}
// TODO: need to make this async
var block = this.web3.eth.getBlock(0);
var chainId = block.hash;

View File

@ -61,7 +61,7 @@ Config.prototype.reloadConfig = function() {
Config.prototype.loadBlockchainConfigFile = function() {
var defaultBlockchainConfig = fs.readJSONSync(this.configDir + "blockchain.json");
this.blockchainConfig = defaultBlockchainConfig[this.env];
this.blockchainConfig = defaultBlockchainConfig[this.env] || {};
if (this.blockchainConfig.enabled === undefined) {
this.blockchainConfig.enabled = true;
@ -109,7 +109,7 @@ Config.prototype.loadStorageConfigFile = function() {
var envStorageConfig = configObject[this.env];
var mergedConfig = utils.recursiveMerge(defaultStorageConfig, envStorageConfig);
this.storageConfig = mergedConfig;
this.storageConfig = mergedConfig || {};
if (this.storageConfig.enabled === undefined) {
this.storageConfig.enabled = true;
@ -137,7 +137,7 @@ Config.prototype.loadCommunicationConfigFile = function() {
var envCommunicationConfig = configObject[this.env];
var mergedConfig = utils.recursiveMerge(defaultCommunicationConfig, envCommunicationConfig);
this.communicationConfig = mergedConfig;
this.communicationConfig = mergedConfig || {};
if (this.communicationConfig.enabled === undefined) {
this.communicationConfig.enabled = true;

115
lib/core/engine.js Normal file
View File

@ -0,0 +1,115 @@
var Events = require('./events.js');
var Logger = require('./logger.js');
var Config = require('./config.js');
var DeployManager = require('../contracts/deploy_manager.js');
var ABIGenerator = require('../contracts/abi.js');
var Dashboard = require('../dashboard/dashboard.js');
var ServicesMonitor = require('./services.js');
var Pipeline = require('../pipeline/pipeline.js');
var Server = require('../pipeline/server.js');
var Watch = require('../pipeline/watch.js');
var Engine = function(options) {
this.env = options.env;
this.embarkConfig = options.embarkConfig;
this.interceptLogs = options.interceptLogs;
this.version = "2.3.0";
};
Engine.prototype.init = function(_options) {
var options = _options || {};
this.events = new Events();
this.logger = options.logger || new Logger({logLevel: 'debug'});
this.config = new Config({env: this.env, logger: this.logger});
this.config.loadConfigFiles({embarkConfig: this.embarkConfig, interceptLogs: this.interceptLogs});
this.plugins = this.config.plugins;
};
Engine.prototype.startService = function(serviceName, _options) {
var self = this;
var options = _options || {};
if (serviceName === "monitor") {
var servicesMonitor = new ServicesMonitor({
logger: this.logger,
config: this.config,
serverHost: options.serverHost,
serverPort: options.serverPort,
runWebserver: options.runWebserver,
version: this.version
});
servicesMonitor.startMonitor();
} else if (serviceName === "pipeline") {
this.logger.setStatus("Building Assets");
var pipeline = new Pipeline({
buildDir: this.config.buildDir,
contractsFiles: this.config.contractsFiles,
assetFiles: this.config.assetFiles,
logger: this.logger,
plugins: this.plugins
});
this.events.on('abi', function(abi) {
self.currentAbi = abi;
pipeline.build(abi);
});
this.events.on('file-event', function(fileType, path) {
if (fileType === 'asset') {
self.config.reloadConfig();
pipeline.build(self.abi, path);
}
});
} else if (serviceName === "abi") {
var generateABICode = function(contractsManager) {
var abiGenerator = new ABIGenerator({
blockchainConfig: self.config.blockchainConfig,
contractsManager: contractsManager,
plugins: self.plugins,
storageConfig: self.config.storageConfig,
communicationConfig: self.config.communicationConfig
});
var embarkJSABI = abiGenerator.generateABI({useEmbarkJS: true});
var vanillaABI = abiGenerator.generateABI({useEmbarkJS: false});
var vanillaContractsABI = abiGenerator.generateContracts(false);
self.events.emit('abi-contracts-vanila', vanillaContractsABI);
self.events.emit('abi-vanila', vanillaABI);
self.events.emit('abi', embarkJSABI);
};
this.events.on('contractsDeployed', generateABICode);
this.events.on('blockchainDisabled', generateABICode);
} else if (serviceName === "deployment") {
this.deployManager = new DeployManager({
web3: options.web3,
trackContracts: options.trackContracts,
config: this.config,
logger: this.logger,
plugins: this.plugins,
events: this.events
});
//deployManager.deployContracts(function() {
//});
this.events.on('file-event', function(fileType, path) {
if (fileType === 'contract' || fileType === 'config') {
self.config.reloadConfig();
deployManager.deployContracts(function() {});
}
});
} else if (serviceName === "fileWatcher") {
this.logger.setStatus("Watching for changes");
var watch = new Watch({logger: this.logger, events: this.events});
watch.start();
} else if (serviceName === "webServer") {
this.logger.setStatus("Starting Server");
var server = new Server({
logger: this.logger,
host: options.serverHost,
port: options.serverPort
});
server.start(function(){});
} else {
throw new Error("unknown service: " + serviceName);
}
};
module.exports = Engine;

View File

@ -3,6 +3,8 @@ var Web3 = require('web3');
var Embark = require('../index.js');
var Engine = require('./engine.js');
var ABIGenerator = require('../contracts/abi.js');
var ContractsManager = require('../contracts/contracts.js');
var Deploy = require('../contracts/deploy.js');
@ -13,7 +15,17 @@ var TestLogger = require('./test_logger.js');
var Test = function(options) {
this.options = options || {};
this.logger = new TestLogger({logLevel: this.options.logLevel || 'debug'});
this.engine = new Engine({
env: this.options.env || 'test',
// TODO: confi will need to detect if this is a obj
embarkConfig: this.options.embarkConfig || 'embark.json',
interceptLogs: false
});
this.engine.init({
logger: new TestLogger({logLevel: this.options.logLevel || 'debug'})
});
var simOptions = this.options.simulatorOptions || {};
try {
@ -43,41 +55,24 @@ Test.prototype.deployAll = function(contractsConfig, cb) {
async.waterfall([
function getConfig(callback) {
var config = new Config({env: 'test', logger: self.logger});
config.loadConfigFiles({
embarkConfig: self.options.embarkConfig || 'embark.json',
interceptLogs: false
});
config.contractsConfig = {contracts: contractsConfig};
callback(null, config);
self.engine.config.contractsConfig = {contracts: contractsConfig};
callback();
},
function buildContracts(config, callback) {
var contractsManager = new ContractsManager({
contractFiles: config.contractsFiles,
contractsConfig: config.contractsConfig,
logger: self.logger,
plugins: config.plugins
});
contractsManager.build(function() {
callback(null, contractsManager);
});
},
function deployContracts(contractsManager, callback) {
var deploy = new Deploy({
function startServices(callback) {
//{abiType: 'contracts', embarkJS: false}
self.engine.startService("abi");
self.engine.startService("deployment", {
web3: self.web3,
contractsManager: contractsManager,
logger: self.logger,
chainConfig: false,
env: 'test'
});
deploy.deployAll(function() {
callback(null, contractsManager);
trackContracts: false
});
callback();
},
function generateABI(contractsManager, callback) {
var abiGenerator = new ABIGenerator({contractsManager: contractsManager});
var ABI = abiGenerator.generateContracts(false);
callback(null, ABI);
function deploy(callback) {
self.engine.events.on('abi-contracts-vanila', function(vanillaABI) {
callback(null, vanillaABI);
});
self.engine.deployManager.deployContracts(function() {
});
}
], function(err, result) {
if (err) {

View File

@ -5,25 +5,19 @@ var async = require('async');
var Web3 = require('web3');
var colors = require('colors');
var Engine = require('./core/engine.js');
var Blockchain = require('./cmds/blockchain/blockchain.js');
var Simulator = require('./cmds/simulator.js');
var TemplateGenerator = require('./cmds/template_generator.js');
var DeployManager = require('./contracts/deploy_manager.js');
var ABIGenerator = require('./contracts/abi.js');
var Test = require('./core/test.js');
var Logger = require('./core/logger.js');
var Config = require('./core/config.js');
var ServicesMonitor = require('./core/services.js');
var Events = require('./core/events.js');
var Dashboard = require('./dashboard/dashboard.js');
var Pipeline = require('./pipeline/pipeline.js');
var Server = require('./pipeline/server.js');
var Watch = require('./pipeline/watch.js');
var IPFS = require('./upload/ipfs.js');
var Swarm = require('./upload/swarm.js');
@ -45,7 +39,6 @@ var Embark = {
this.config = new Config({env: env, logger: this.logger});
this.config.loadConfigFiles(options);
this.plugins = this.config.plugins;
},
blockchain: function(env, client) {
@ -67,6 +60,12 @@ var Embark = {
var self = this;
var env = options.env;
var engine = new Engine({
env: options.env,
embarkConfig: 'embark.json'
});
engine.init();
if (!options.useDashboard) {
console.log('========================'.bold.green);
console.log(('Welcome to Embark ' + Embark.version).yellow.bold);
@ -80,13 +79,13 @@ var Embark = {
}
var dashboard = new Dashboard({
logger: Embark.logger,
plugins: self.plugins,
version: self.version,
env: env
logger: engine.logger,
plugins: engine.plugins,
version: engine.version,
env: engine.env
});
dashboard.start(function() {
self.events.on('abi-vanila', function(abi) {
engine.events.on('abi-vanila', function(abi) {
dashboard.console.runCode(abi);
});
@ -94,177 +93,76 @@ var Embark = {
});
},
function (callback) {
Embark.startEmbark(options, callback);
}
], function(err, result) {
if (err) {
self.logger.error(err.message);
} else {
self.logger.setStatus("Ready".green);
self.logger.info("Looking for documentation? you can find it at ".cyan + "http://embark.readthedocs.io/".green.underline);
self.logger.info("Ready".underline);
}
});
},
startEmbark: function(options, done) {
var self = this;
var env = options.env;
async.waterfall([
function displayLoadedPlugins(callback) {
var pluginList = self.plugins.listPlugins();
var pluginList = engine.plugins.listPlugins();
if (pluginList.length > 0) {
self.logger.info("loaded plugins: " + pluginList.join(", "));
engine.logger.info("loaded plugins: " + pluginList.join(", "));
}
callback();
},
// can be done in paralell
function monitorServices(callback) {
if (!options.useDashboard) {
return callback();
}
var servicesMonitor = new ServicesMonitor({
logger: Embark.logger,
config: Embark.config,
serverHost: options.serverHost,
serverPort: options.serverPort,
runWebserver: options.runWebserver,
version: Embark.version
});
servicesMonitor.startMonitor();
callback();
},
function buildPipeline(callback) {
self.logger.setStatus("Building Assets");
var pipeline = new Pipeline({
buildDir: self.config.buildDir,
contractsFiles: self.config.contractsFiles,
assetFiles: self.config.assetFiles,
logger: self.logger,
plugins: self.plugins
});
self.events.on('abi', function(abi) {
Embark.currentAbi = abi;
pipeline.build(abi);
});
self.events.on('file-event', function(fileType, path) {
if (fileType === 'asset') {
self.config.reloadConfig();
pipeline.build(Embark.abi, path);
}
});
callback();
},
function generateABI(callback) {
var generateABICode = function(contractsManager) {
var abiGenerator = new ABIGenerator({
blockchainConfig: self.config.blockchainConfig,
contractsManager: contractsManager,
plugins: self.plugins,
storageConfig: self.config.storageConfig,
communicationConfig: self.config.communicationConfig
if (options.useDashboard) {
engine.startService("monitor", {
serverHost: options.serverHost,
serverPort: options.serverPort,
runWebserver: options.runWebserver
});
var embarkJSABI = abiGenerator.generateABI({useEmbarkJS: true});
var vanillaABI = abiGenerator.generateABI({useEmbarkJS: false});
}
engine.startService("pipeline");
engine.startService("abi");
engine.startService("deployment");
self.events.emit('abi-vanila', vanillaABI);
self.events.emit('abi', embarkJSABI);
};
self.events.on('contractsDeployed', generateABICode);
self.events.on('blockchainDisabled', generateABICode);
callback();
},
function deploy(callback) {
var deployManager = new DeployManager({
config: Embark.config,
logger: Embark.logger,
plugins: self.plugins,
events: self.events
});
deployManager.deployContracts(function() {
engine.deployManager.deployContracts(function() {
engine.startService("fileWatcher");
if (options.runWebserver) {
engine.startService("webServer", {
host: options.serverHost,
port: options.serverPort
});
}
callback();
});
self.events.on('file-event', function(fileType, path) {
if (fileType === 'contract' || fileType === 'config') {
self.config.reloadConfig();
deployManager.deployContracts(function() {});
}
});
},
function watchFilesForChanges(callback) {
self.logger.setStatus("Watching for changes");
var watch = new Watch({logger: self.logger, events: self.events});
watch.start();
callback();
},
function startAssetServer(callback) {
if (!options.runWebserver) {
return callback();
}
self.logger.setStatus("Starting Server");
var server = new Server({
logger: self.logger,
host: options.serverHost,
port: options.serverPort
});
server.start(callback);
}
], function(err, result) {
if (err) {
self.logger.error(err.message);
engine.logger.error(err.message);
} else {
engine.logger.setStatus("Ready".green);
engine.logger.info("Looking for documentation? you can find it at ".cyan + "http://embark.readthedocs.io/".green.underline);
engine.logger.info("Ready".underline);
}
done();
});
},
build: function(env) {
build: function(options) {
var self = this;
var engine = new Engine({
env: options.env,
embarkConfig: 'embark.json',
interceptLogs: false
});
engine.init();
async.waterfall([
function displayLoadedPlugins(callback) {
var pluginList = self.plugins.listPlugins();
function startServices(callback) {
var pluginList = engine.plugins.listPlugins();
if (pluginList.length > 0) {
self.logger.info("loaded plugins: " + pluginList.join(", "));
engine.logger.info("loaded plugins: " + pluginList.join(", "));
}
engine.startService("pipeline");
engine.startService("abi");
engine.startService("deployment");
callback();
},
function deployAndGenerateABI(callback) {
var deployManager = new DeployManager({
config: Embark.config,
logger: Embark.logger,
plugins: self.plugins,
events: self.events
function deploy(callback) {
engine.deployManager.deployContracts(function() {
callback();
});
deployManager.deployContracts(function(error, abi) {
callback(null, abi);
});
},
function buildPipeline(abi, callback) {
self.logger.trace("Building Assets");
var pipeline = new Pipeline({
buildDir: self.config.buildDir,
contractsFiles: self.config.contractsFiles,
assetFiles: self.config.assetFiles,
logger: self.logger,
plugins: self.plugins
});
pipeline.build(abi);
callback();
}
], function(err, result) {
if (err) {
self.logger.error(err.message);
engine.logger.error(err.message);
} else {
self.logger.info("finished building".underline);
engine.logger.info("finished building".underline);
}
// needed due to child processes
process.exit();