From 2f9c0a29961862f898b6c1196efc006b33cde35d Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 23 Sep 2016 12:31:09 +0800 Subject: [PATCH] add more support for the console --- lib/abi.js | 20 ++++++++++---------- lib/console.js | 35 +++++++++++++++++++++++++++++++++++ lib/index.js | 40 ++++++++++++++++++++++++++++++++++------ lib/monitor.js | 7 ++++--- 4 files changed, 83 insertions(+), 19 deletions(-) create mode 100644 lib/console.js diff --git a/lib/abi.js b/lib/abi.js index 06783221..36539398 100644 --- a/lib/abi.js +++ b/lib/abi.js @@ -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 + "');"; - - result += "\n" + className + " = new EmbarkJS.Contract({abi: " + abi + ", address: '" + contract.deployedAddress + "', code: '" + contract.code + "'});"; + 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; }; diff --git a/lib/console.js b/lib/console.js new file mode 100644 index 00000000..5bb5bbe1 --- /dev/null +++ b/lib/console.js @@ -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; + diff --git a/lib/index.js b/lib/index.js index 48cb7da6..06b1d2f9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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); diff --git a/lib/monitor.js b/lib/monitor.js index 84473dab..67662e5b 100644 --- a/lib/monitor.js +++ b/lib/monitor.js @@ -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(); });