From cf0ea4299ed51c3caf22c92b1af288671d5874ce Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 20 Feb 2017 17:12:13 -0500 Subject: [PATCH] support disabling blockchain stack; tolerate lack of web3 object --- js/embark.js | 7 +++++ lib/contracts/abi.js | 13 +++++++-- lib/index.js | 49 ++++++++++++++++++++++----------- test_app/app/test2.html | 12 ++++++++ test_app/config/blockchain.json | 1 + test_app/embark.json | 4 ++- 6 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 test_app/app/test2.html diff --git a/js/embark.js b/js/embark.js index 7e595a118..6ddfdcd11 100644 --- a/js/embark.js +++ b/js/embark.js @@ -204,6 +204,13 @@ EmbarkJS.Messages.setProvider = function(provider, options) { var ipfs; if (provider === 'whisper') { this.currentMessages = EmbarkJS.Messages.Whisper; + if (web3 === undefined) { + if (options === undefined) { + web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); + } else { + web3 = new Web3(new Web3.providers.HttpProvider("http://" + options.server + ':' + options.port)); + } + } this.currentMessages.identity = web3.shh.newIdentity(); } else if (provider === 'orbit') { this.currentMessages = EmbarkJS.Messages.Orbit; diff --git a/lib/contracts/abi.js b/lib/contracts/abi.js index f6f175d0b..4682e8634 100644 --- a/lib/contracts/abi.js +++ b/lib/contracts/abi.js @@ -2,6 +2,7 @@ var ABIGenerator = function(options) { this.blockchainConfig = options.blockchainConfig || {}; this.storageConfig = options.storageConfig || {}; + this.communicationConfig = options.communicationConfig || {}; this.contractsManager = options.contractsManager; this.rpcHost = options.blockchainConfig && options.blockchainConfig.rpcHost; this.rpcPort = options.blockchainConfig && options.blockchainConfig.rpcPort; @@ -13,6 +14,10 @@ ABIGenerator.prototype.generateProvider = function() { var result = ""; var providerPlugins; + if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) { + return ""; + } + if (this.plugins) { providerPlugins = this.plugins.getPluginsFor('clientWeb3Provider'); } @@ -38,6 +43,10 @@ ABIGenerator.prototype.generateContracts = function(useEmbarkJS) { var result = "\n"; var contractsPlugins; + if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) { + return ""; + } + if (this.plugins) { contractsPlugins = this.plugins.getPluginsFor('contractGeneration'); } @@ -70,7 +79,7 @@ ABIGenerator.prototype.generateStorageInitialization = function(useEmbarkJS) { var self = this; var result = "\n"; - if (!useEmbarkJS || self.storageConfig === {}) return; + if (!useEmbarkJS || self.storageConfig === {}) return ""; if (self.storageConfig.provider === 'ipfs' && self.storageConfig.enabled === true) { result += "\n" + "EmbarkJS.Storage.setProvider('" + self.storageConfig.provider + "', {server: '" + self.storageConfig.host + "', port: '" + self.storageConfig.port + "'});"; @@ -83,7 +92,7 @@ ABIGenerator.prototype.generateCommunicationInitialization = function(useEmbarkJ var self = this; var result = "\n"; - if (!useEmbarkJS || self.communicationConfig === {}) return; + if (!useEmbarkJS || self.communicationConfig === {}) return ""; if (self.communicationConfig.provider === 'ipfs' && self.communicationConfig.enabled === true) { result += "\n" + "EmbarkJS.Storage.setProvider('" + self.communicationConfig.provider + "');"; diff --git a/lib/index.js b/lib/index.js index 81c8bfea0..94450d344 100644 --- a/lib/index.js +++ b/lib/index.js @@ -68,7 +68,19 @@ var Embark = { self.config.reloadConfig(); callback(); }, - self.buildDeployGenerate.bind(self) + self.buildDeployGenerate.bind(self), + function buildPipeline(abi, 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 + }); + pipeline.build(abi); + callback(); + } ], function(err, result) { if (err) { self.logger.error(err.message); @@ -136,6 +148,18 @@ var Embark = { callback(); }, self.buildDeployGenerate.bind(self), + function buildPipeline(abi, 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 + }); + pipeline.build(abi); + callback(); + }, function watchFilesForChanges(callback) { self.logger.setStatus("Watching for changes"); var watch = new Watch({logger: self.logger}); @@ -285,6 +309,11 @@ var Embark = { buildDeployGenerate: function(done) { var self = this; + if (self.config.blockchainConfig.enabled === false) { + self.logger.info('== blockchain is disabled in this environment in config/blockchain.json'.underline); + return done(null, ""); + } + self.logger.setStatus("Deploying...".magenta.underline); async.waterfall([ function deployAndBuildContractsManager(callback) { @@ -296,26 +325,14 @@ var Embark = { }); }, function generateConsoleABI(contractsManager, callback) { - var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, storageConfig: self.config.storageConfig}); + var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, storageConfig: self.config.storageConfig, communicationConfig: self.config.communicationConfig}); var consoleABI = abiGenerator.generateABI({useEmbarkJS: false}); Embark.console.runCode(consoleABI); callback(null, contractsManager); }, function generateABI(contractsManager, callback) { - var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, plugins: self.plugins, storageConfig: self.config.storageConfig}); + var abiGenerator = new ABIGenerator({blockchainConfig: self.config.blockchainConfig, contractsManager: contractsManager, plugins: self.plugins, storageConfig: self.config.storageConfig, communicationConfig: self.config.communicationConfig}); callback(null, abiGenerator.generateABI({useEmbarkJS: true})); - }, - function buildPipeline(abi, 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 - }); - pipeline.build(abi); - callback(); } ], function(err, result) { if (err) { @@ -325,7 +342,7 @@ var Embark = { } else { self.logger.setStatus("Ready".green); } - done(result); + done(null, result); }); }, diff --git a/test_app/app/test2.html b/test_app/app/test2.html new file mode 100644 index 000000000..62cc71f88 --- /dev/null +++ b/test_app/app/test2.html @@ -0,0 +1,12 @@ + + + Embark - TestApp + + + + + +
+ + + diff --git a/test_app/config/blockchain.json b/test_app/config/blockchain.json index 94050ca97..b5f1e2d87 100644 --- a/test_app/config/blockchain.json +++ b/test_app/config/blockchain.json @@ -1,5 +1,6 @@ { "development": { + "enabled": false, "networkType": "custom", "genesisBlock": "config/development/genesis.json", "datadir": ".embark/development/datadir", diff --git a/test_app/embark.json b/test_app/embark.json index 3f5136de0..bed802dee 100644 --- a/test_app/embark.json +++ b/test_app/embark.json @@ -4,9 +4,11 @@ "css/app.css": ["app/css/**"], "images/": ["app/images/**"], "js/app.js": ["embark.js", "app/js/_vendor/jquery.min.js", "app/js/_vendor/bootstrap.min.js", "app/js/**"], + "js/embark.js": ["embark.js"], "js/abi.js": "abi.js", "index.html": "app/index.html", - "test.html": "app/test.html" + "test.html": "app/test.html", + "test2.html": "app/test2.html" }, "buildDir": "dist/", "config": "config/",