add more support for the console

This commit is contained in:
Iuri Matias 2016-09-23 12:31:09 +08:00
parent 70c9a1d9c5
commit 2f9c0a2996
4 changed files with 83 additions and 19 deletions

View File

@ -19,7 +19,7 @@ ABIGenerator.prototype.generateProvider = function() {
return result;
};
ABIGenerator.prototype.generateContracts = function() {
ABIGenerator.prototype.generateContracts = function(useEmbarkJS) {
var result = "\n";
for(var className in this.contractsManager.contracts) {
@ -27,23 +27,23 @@ ABIGenerator.prototype.generateContracts = function() {
var abi = JSON.stringify(contract.abiDefinition);
//console.log('address is ' + contract.deployedAddress);
//result += "\n" + className + "Abi = " + abi + ";";
//result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);";
//result += "\n" + className + " = " + className + "Contract.at('" + contract.deployedAddress + "');";
if (useEmbarkJS) {
result += "\n" + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: '" + contract.deployedAddress + "', code: '" + contract.code + "'});";
} else {
result += "\n" + className + "Abi = " + abi + ";";
result += "\n" + className + "Contract = web3.eth.contract(" + className + "Abi);";
result += "\n" + className + " = " + className + "Contract.at('" + contract.deployedAddress + "');";
}
}
return result;
};
ABIGenerator.prototype.generateABI = function() {
ABIGenerator.prototype.generateABI = function(options) {
var result = "";
result += this.generateProvider();
result += this.generateContracts();
result += this.generateContracts(options.useEmbarkJS);
return result;
};

35
lib/console.js Normal file
View File

@ -0,0 +1,35 @@
var Web3 = require('web3');
var Console = function(options) {
};
Console.prototype.runCode = function(code) {
eval(code);
};
Console.prototype.executeCmd = function(cmd, callback) {
if (cmd === 'help') {
var helpText = [
'Welcome to Embark 2',
'',
'possible commands are:',
'quit - to immediatly exit',
'',
'The web3 object and the interfaces for the deployed contrats and their methods are also available'
];
return callback(helpText.join('\n'));
} else if (cmd === 'quit') {
exit();
};
try {
var result = eval(cmd);
return callback(result);
}
catch(e) {
return callback(e.message.red);
}
};
module.exports = Console;

View File

@ -21,6 +21,7 @@ var Logger = require('./logger.js');
var Config = require('./config.js');
var Monitor = require('./monitor.js');
var ServicesMonitor = require('./services.js');
var Console = require('./console.js');
var Embark = {
@ -47,8 +48,12 @@ var Embark = {
run: function(env) {
var self = this;
async.waterfall([
function startConsole(callback) {
Embark.console = new Console();
callback();
},
function startMonitor(callback) {
Embark.monitor = new Monitor({env: env});
Embark.monitor = new Monitor({env: env, console: Embark.console});
Embark.monitor.setStatus("Starting");
self.logger.logFunction = Embark.monitor.logEntry;
self.logger.contractsState = Embark.monitor.setContracts;
@ -63,12 +68,22 @@ var Embark = {
Embark.servicesMonitor.startMonitor();
callback();
},
function deployAndGenerateABI(callback) {
function deployAndBuildContractsManager(callback) {
Embark.monitor.setStatus("Deploying Contracts");
Embark.deploy(function(abi) {
callback(null, abi);
Embark.buildAndDeploy(function(contractsManager) {
callback(null, contractsManager);
});
},
function generateConsoleABI(contractsManager, callback) {
var abiGenerator = new ABIGenerator(self.config.blockchainConfig, contractsManager);
var consoleABI = abiGenerator.generateABI({useEmbarkJS: false});
Embark.console.runCode(consoleABI);
callback(null, contractsManager);
},
function generateABI(contractsManager, callback) {
var abiGenerator = new ABIGenerator(self.config.blockchainConfig, contractsManager);
callback(null, abiGenerator.generateABI({useEmbarkJS: true}));
},
function buildPipeline(abi, callback) {
Embark.monitor.setStatus("Building Assets");
var pipeline = new Pipeline({
@ -119,7 +134,7 @@ var Embark = {
blockchain.run({env: env});
},
deploy: function(done) {
buildAndDeploy: function(done) {
var self = this;
async.waterfall([
function buildContracts(callback) {
@ -144,10 +159,23 @@ var Embark = {
deploy.deployAll(function() {
callback(null, contractsManager);
});
}
], function(err, result) {
done(result);
});
},
deploy: function(done) {
var self = this;
async.waterfall([
function buildAndDeploy(callback) {
Embark.buildAndDeploy(function(contractsManager) {
callback(null, contractsManager);
});
},
function generateABI(contractsManager, callback) {
var abiGenerator = new ABIGenerator(self.config.blockchainConfig, contractsManager);
callback(null, abiGenerator.generateABI());
callback(null, abiGenerator.generateABI({useEmbarkJS: true}));
},
], function(err, result) {
done(result);

View File

@ -10,6 +10,7 @@ var blessed = require("blessed");
function Dashboard(options) {
var title = options && options.title || "Embark 2.0";
this.env = options.env;
this.console = options.console;
this.color = options && options.color || "green";
this.minimal = options && options.minimal || false;
@ -318,10 +319,10 @@ Dashboard.prototype.layoutCmd = function() {
this.input.on('submit', function(data) {
if (data !== '') {
self.logText.log('console> ' + data);
self.console.executeCmd(data, function(result) {
self.logText.log(result);
});
}
if (data === 'quit') {
exit();
};
self.input.clearValue();
self.input.focus();
});