From 6b7af4b64769dca44d97e848b46d42688c11b83b Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Thu, 12 Apr 2018 20:46:04 +1000 Subject: [PATCH 1/5] #156326148 Build contracts and dapp before upload. --- lib/cmd.js | 8 ++++--- lib/index.js | 66 +++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/lib/cmd.js b/lib/cmd.js index e6e17848..12cd67b8 100644 --- a/lib/cmd.js +++ b/lib/cmd.js @@ -170,13 +170,15 @@ class Cmd { upload() { program .command('upload [platform] [environment]') + .option('--logfile [logfile]', 'filename to output logs (default: none)') .description('upload your dapp to a decentralized storage (e.g embark upload ipfs)') .action(function (platform, env, _options) { - // TODO: get env in cmd line as well - embark.initConfig(env || 'development', { + let environment = env || 'development'; + embark.initConfig(environment, { embarkConfig: 'embark.json', interceptLogs: false }); - embark.upload(platform); + _options.env = environment; + embark.upload(platform, _options); }); } diff --git a/lib/index.js b/lib/index.js index dcb5ac66..abf2f222 100644 --- a/lib/index.js +++ b/lib/index.js @@ -247,11 +247,18 @@ class Embark { } // TODO: should deploy if it hasn't already - upload(platform) { - let options = { - buildDir: 'dist/', - storageConfig: this.config.storageConfig - }; + upload(platform, options) { + + options.buildDir = 'dist/'; + options.storageConfig = this.config.storageConfig; + + let engine = new Engine({ + env: options.env, + version: this.version, + embarkConfig: options.embarkConfig || 'embark.json', + logfile: options.logfile + }); + engine.init(); this.plugins.loadInternalPlugin('ipfs', options); this.plugins.loadInternalPlugin('swarm', options); @@ -265,11 +272,54 @@ class Embark { } if (cmdPlugin) { - cmdPlugin.uploadCmds[0].cb(); + async.waterfall([ + + function buildDapp(callback){ + engine.logger.debug('building dapp...'); + engine.startMonitor(); + engine.startService("libraryManager"); + engine.startService("web3"); + engine.startService("pipeline"); + engine.startService("codeGenerator"); + engine.startService("deployment"); + engine.startService("ipfs"); + + engine.events.on('outputDone', function () { + engine.logger.debug('deploying...'); + cmdPlugin.uploadCmds[0].cb(); + callback(); + }); + + // build the contracts + engine.contractsManager.build(function(){ + // trigger code generation and dapp webpack + engine.events.emit('asset-changed', engine.contractsManager); + }); + + + }, + function upload(callback){ + + callback(); + } + ], function (err, _result) { + if (err) { + engine.logger.error(err.message); + engine.logger.debug(err.stack); + } else { + engine.logger.info("finished building dapp and deploying to " + platform.underline); + } + + // needed due to child processes + process.exit(); + }); + } else { - console.log(("unknown platform: " + platform).red); - console.log('try "embark upload ipfs" or "embark upload swarm"'.green); + engine.logger.error(("unknown platform: " + platform)); + engine.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green); } + + } runTests(file) { From 31a2a27e9a775410609ef7fa0e8d04c99881b51f Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Fri, 13 Apr 2018 15:06:13 +1000 Subject: [PATCH 2/5] #156326148 Updates to build and upload process and fixes for test_app/test.html * Updated build to include deployment of contracts * updated the plugin uploadCmd to be a Promise for better bubbling of errors and messaging. * updated test.js imports so functionality works correctly. --- lib/index.js | 29 +++++++----- lib/modules/ipfs/upload.js | 79 ++++++++++++++++--------------- lib/modules/swarm/upload.js | 78 +++++++++++++++--------------- test_apps/test_app/app/js/test.js | 9 ++++ 4 files changed, 109 insertions(+), 86 deletions(-) diff --git a/lib/index.js b/lib/index.js index abf2f222..8812a2d6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -274,7 +274,7 @@ class Embark { if (cmdPlugin) { async.waterfall([ - function buildDapp(callback){ + function (callback){ engine.logger.debug('building dapp...'); engine.startMonitor(); engine.startService("libraryManager"); @@ -284,23 +284,30 @@ class Embark { engine.startService("deployment"); engine.startService("ipfs"); + // 3. upload to storage (outputDone event triggered after webpack finished) engine.events.on('outputDone', function () { - engine.logger.debug('deploying...'); - cmdPlugin.uploadCmds[0].cb(); - callback(); + engine.logger.debug('deploying to ' + platform + '...'); + cmdPlugin.uploadCmds[0].cb() + .then((success) => { + callback(); + }) + .catch((err) => { + callback(err); + }); + }); - // build the contracts - engine.contractsManager.build(function(){ - // trigger code generation and dapp webpack + // 1. build the contracts + engine.deployManager.deployContracts(function (err) { + if(err){ + callback(err); + } + + // 2. trigger code generation and dapp webpack engine.events.emit('asset-changed', engine.contractsManager); }); - }, - function upload(callback){ - - callback(); } ], function (err, _result) { if (err) { diff --git a/lib/modules/ipfs/upload.js b/lib/modules/ipfs/upload.js index da9e6191..508b01ad 100644 --- a/lib/modules/ipfs/upload.js +++ b/lib/modules/ipfs/upload.js @@ -12,48 +12,51 @@ class IPFS { } deploy() { - console.log("deploying!"); - let self = this; - async.waterfall([ - function findBinary(callback) { - let ipfs_bin = shelljs.which(self.configIpfsBin); + return new Promise((resolve, reject) => { + console.log("deploying!"); + let self = this; + async.waterfall([ + function findBinary(callback) { + let ipfs_bin = shelljs.which(self.configIpfsBin); - if (ipfs_bin === 'ipfs not found' || !ipfs_bin) { - console.log(('=== WARNING: ' + self.configIpfsBin + ' not found or not in the path. Guessing ~/go/bin/ipfs for path').yellow); - ipfs_bin = "~/go/bin/ipfs"; + if (ipfs_bin === 'ipfs not found' || !ipfs_bin) { + console.log(('=== WARNING: ' + self.configIpfsBin + ' not found or not in the path. Guessing ~/go/bin/ipfs for path').yellow); + ipfs_bin = "~/go/bin/ipfs"; + } + + return callback(null, ipfs_bin); + }, + function runCommand(ipfs_bin, callback) { + let cmd = `"${ipfs_bin}" add -r ${self.buildDir}`; + console.log(("=== adding " + self.buildDir + " to ipfs").green); + console.log(cmd.green); + let result = shelljs.exec(cmd); + + return callback(null, result); + }, + function getHashFromOutput(result, callback) { + let rows = result.output.split("\n"); + let dir_row = rows[rows.length - 2]; + let dir_hash = dir_row.split(" ")[1]; + + return callback(null, dir_hash); + }, + function printUrls(dir_hash, callback) { + console.log(("=== DApp available at http://localhost:8080/ipfs/" + dir_hash + "/").green); + console.log(("=== DApp available at http://gateway.ipfs.io/ipfs/" + dir_hash + "/").green); + + return callback(); } - - return callback(null, ipfs_bin); - }, - function runCommand(ipfs_bin, callback) { - let cmd = `"${ipfs_bin}" add -r ${self.buildDir}`; - console.log(("=== adding " + self.buildDir + " to ipfs").green); - console.log(cmd.green); - let result = shelljs.exec(cmd); - - return callback(null, result); - }, - function getHashFromOutput(result, callback) { - let rows = result.output.split("\n"); - let dir_row = rows[rows.length - 2]; - let dir_hash = dir_row.split(" ")[1]; - - return callback(null, dir_hash); - }, - function printUrls(dir_hash, callback) { - console.log(("=== DApp available at http://localhost:8080/ipfs/" + dir_hash + "/").green); - console.log(("=== DApp available at http://gateway.ipfs.io/ipfs/" + dir_hash + "/").green); - - return callback(); - } - ], function (err, _result) { - if (err) { - console.log("error uploading to ipfs".red); - console.log(err); - } + ], function (err, _result) { + if (err) { + console.log("error uploading to ipfs".red); + console.log(err); + reject(err); + } + else resolve('successfully uploaded to ipfs'); + }); }); } - } module.exports = IPFS; diff --git a/lib/modules/swarm/upload.js b/lib/modules/swarm/upload.js index 4ba0a9b3..6d588f65 100644 --- a/lib/modules/swarm/upload.js +++ b/lib/modules/swarm/upload.js @@ -9,46 +9,50 @@ class Swarm { } deploy() { - let self = this; - async.waterfall([ - function findBinary(callback) { - let swarm_bin = shelljs.which('swarm'); + return new Promise((resolve, reject) => { + let self = this; + async.waterfall([ + function findBinary(callback) { + let swarm_bin = shelljs.which('swarm'); - if (swarm_bin === 'swarm not found' || !swarm_bin) { - console.log('=== WARNING: Swarm not in an executable path. Guessing ~/go/bin/swarm for path'.yellow); - swarm_bin = "~/go/bin/swarm"; + if (swarm_bin === 'swarm not found' || !swarm_bin) { + console.log('=== WARNING: Swarm not in an executable path. Guessing ~/go/bin/swarm for path'.yellow); + swarm_bin = "~/go/bin/swarm"; + } + + return callback(null, swarm_bin); + }, + function runCommand(swarm_bin, callback) { + let cmd = `"${swarm_bin}" --defaultpath ${self.buildDir} index.html --recursive up ${self.buildDir}`; + console.log(("=== adding " + self.buildDir + " to swarm").green); + console.log(cmd.green); + let result = shelljs.exec(cmd); + + return callback(null, result); + }, + function getHashFromOutput(result, callback) { + if (result.code !== 0) { + return callback("couldn't upload, is the swarm daemon running?"); + } + + let rows = result.output.split("\n"); + let dir_hash = rows.reverse()[1]; + + return callback(null, dir_hash); + }, + function printUrls(dir_hash, callback) { + console.log(("=== DApp available at http://localhost:8500/bzz:/" + dir_hash + "/").green); + + return callback(); } - - return callback(null, swarm_bin); - }, - function runCommand(swarm_bin, callback) { - let cmd = `"${swarm_bin}" --defaultpath ${self.buildDir} index.html --recursive up ${self.buildDir}`; - console.log(("=== adding " + self.buildDir + " to swarm").green); - console.log(cmd.green); - let result = shelljs.exec(cmd); - - return callback(null, result); - }, - function getHashFromOutput(result, callback) { - if (result.code !== 0) { - return callback("couldn't upload, is the swarm daemon running?"); + ], function (err, _result) { + if (err) { + console.log("error uploading to swarm".red); + console.log(err); + reject(err); } - - let rows = result.output.split("\n"); - let dir_hash = rows.reverse()[1]; - - return callback(null, dir_hash); - }, - function printUrls(dir_hash, callback) { - console.log(("=== DApp available at http://localhost:8500/bzz:/" + dir_hash + "/").green); - - return callback(); - } - ], function (err, _result) { - if (err) { - console.log("error uploading to swarm".red); - console.log(err); - } + else resolve('successfully uploaded to swarm'); + }); }); } } diff --git a/test_apps/test_app/app/js/test.js b/test_apps/test_app/app/js/test.js index a4ba623c..a4b9b260 100644 --- a/test_apps/test_app/app/js/test.js +++ b/test_apps/test_app/app/js/test.js @@ -1,3 +1,12 @@ +import $ from 'jquery'; +import AlreadyDeployedToken from 'Embark/contracts/AlreadyDeployedToken'; +import AnotherStorage from 'Embark/contracts/AnotherStorage'; +import async from 'async'; +import MyToken from 'Embark/contracts/MyToken'; +import MyToken2 from 'Embark/contracts/MyToken2'; +import SimpleStorage from 'Embark/contracts/SimpleStorage'; +import Token from 'Embark/contracts/Token'; + $(document).ready(function() { From c1c61de2d54c917a15b7c4b68477c1067e735331 Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Fri, 13 Apr 2018 15:30:45 +1000 Subject: [PATCH 3/5] Passed in success messaging to async callback. --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 8812a2d6..755a2b16 100644 --- a/lib/index.js +++ b/lib/index.js @@ -289,7 +289,7 @@ class Embark { engine.logger.debug('deploying to ' + platform + '...'); cmdPlugin.uploadCmds[0].cb() .then((success) => { - callback(); + callback(null, success); }) .catch((err) => { callback(err); From 8fc978eb50ed35d194a3d36d543a098d6066eb38 Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Sun, 15 Apr 2018 18:41:50 +1000 Subject: [PATCH 4/5] * Removed duplicated code to build and deploy contracts and replaced with a call to the build function. * Updated upload functions to use async `exec` and removed returns from the callbacks. --- lib/index.js | 121 ++++++++++++++++-------------------- lib/modules/ipfs/upload.js | 14 ++--- lib/modules/swarm/upload.js | 21 ++++--- 3 files changed, 71 insertions(+), 85 deletions(-) diff --git a/lib/index.js b/lib/index.js index 755a2b16..da22e054 100644 --- a/lib/index.js +++ b/lib/index.js @@ -147,15 +147,16 @@ class Embark { }); } - build(options) { - - let engine = new Engine({ - env: options.env, - version: this.version, - embarkConfig: 'embark.json', - interceptLogs: false - }); - engine.init(); + build(options, engine, continueProcessing) { + if(!engine){ + engine = new Engine({ + env: options.env, + version: this.version, + embarkConfig: 'embark.json', + interceptLogs: false + }); + engine.init(); + } async.waterfall([ function startServices(callback) { @@ -185,7 +186,9 @@ class Embark { engine.logger.info("finished building".underline); } // needed due to child processes - process.exit(); + if(!continueProcessing){ + process.exit(); + } }); } @@ -252,6 +255,7 @@ class Embark { options.buildDir = 'dist/'; options.storageConfig = this.config.storageConfig; + // initialise embark engine let engine = new Engine({ env: options.env, version: this.version, @@ -260,73 +264,54 @@ class Embark { }); engine.init(); + // load plugins this.plugins.loadInternalPlugin('ipfs', options); this.plugins.loadInternalPlugin('swarm', options); - let cmdPlugins = this.plugins.getPluginsFor('uploadCmds'); + let plugins = this.plugins; let cmdPlugin; - if (cmdPlugins.length > 0) { - cmdPlugin = cmdPlugins.find((pluginCmd) => { - return pluginCmd.name == platform; - }); - } - - if (cmdPlugin) { - async.waterfall([ - - function (callback){ - engine.logger.debug('building dapp...'); - engine.startMonitor(); - engine.startService("libraryManager"); - engine.startService("web3"); - engine.startService("pipeline"); - engine.startService("codeGenerator"); - engine.startService("deployment"); - engine.startService("ipfs"); - - // 3. upload to storage (outputDone event triggered after webpack finished) - engine.events.on('outputDone', function () { - engine.logger.debug('deploying to ' + platform + '...'); - cmdPlugin.uploadCmds[0].cb() - .then((success) => { - callback(null, success); - }) - .catch((err) => { - callback(err); - }); - + let self = this; + async.waterfall([ + function setupStoragePlugin(callback){ + // check use has input existing storage plugin + let cmdPlugins = plugins.getPluginsFor('uploadCmds'); + + if (cmdPlugins.length > 0) { + cmdPlugin = cmdPlugins.find((pluginCmd) => { + return pluginCmd.name == platform; }); - - // 1. build the contracts - engine.deployManager.deployContracts(function (err) { - if(err){ - callback(err); - } - - // 2. trigger code generation and dapp webpack - engine.events.emit('asset-changed', engine.contractsManager); - }); - - } - ], function (err, _result) { - if (err) { - engine.logger.error(err.message); - engine.logger.debug(err.stack); + if (!cmdPlugin) { + engine.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green); + callback({message: 'unknown platform: ' + platform}); } else { - engine.logger.info("finished building dapp and deploying to " + platform.underline); + callback(); } + }, + function buildAndDeployContracts(callback){ + // 2. upload to storage (outputDone event triggered after webpack finished) + engine.events.on('outputDone', function () { + engine.logger.info('deploying to ' + platform + '...'); + cmdPlugin.uploadCmds[0].cb() + .then((success) => { + callback(null, success); + }) + .catch(callback); + }); + // 1. build the contracts and dapp webpack + self.build(options, engine, true); + } + ], function (err, _result) { + if (err) { + engine.logger.error(err.message); + engine.logger.debug(err.stack); + } else { + engine.logger.info("finished building dapp and deploying to " + platform.underline); + } - // needed due to child processes - process.exit(); - }); - - } else { - engine.logger.error(("unknown platform: " + platform)); - engine.logger.info('try "embark upload ipfs" or "embark upload swarm"'.green); - } - - + // needed due to child processes + process.exit(); + }); } runTests(file) { diff --git a/lib/modules/ipfs/upload.js b/lib/modules/ipfs/upload.js index 508b01ad..3dd52f1a 100644 --- a/lib/modules/ipfs/upload.js +++ b/lib/modules/ipfs/upload.js @@ -24,28 +24,28 @@ class IPFS { ipfs_bin = "~/go/bin/ipfs"; } - return callback(null, ipfs_bin); + callback(null, ipfs_bin); }, function runCommand(ipfs_bin, callback) { let cmd = `"${ipfs_bin}" add -r ${self.buildDir}`; console.log(("=== adding " + self.buildDir + " to ipfs").green); console.log(cmd.green); - let result = shelljs.exec(cmd); - - return callback(null, result); + shelljs.exec(cmd, function(code, stdout, stderr){ + callback(stderr, stdout); + }); }, function getHashFromOutput(result, callback) { - let rows = result.output.split("\n"); + let rows = result.split("\n"); let dir_row = rows[rows.length - 2]; let dir_hash = dir_row.split(" ")[1]; - return callback(null, dir_hash); + callback(null, dir_hash); }, function printUrls(dir_hash, callback) { console.log(("=== DApp available at http://localhost:8080/ipfs/" + dir_hash + "/").green); console.log(("=== DApp available at http://gateway.ipfs.io/ipfs/" + dir_hash + "/").green); - return callback(); + callback(); } ], function (err, _result) { if (err) { diff --git a/lib/modules/swarm/upload.js b/lib/modules/swarm/upload.js index 6d588f65..5eed94b1 100644 --- a/lib/modules/swarm/upload.js +++ b/lib/modules/swarm/upload.js @@ -20,30 +20,31 @@ class Swarm { swarm_bin = "~/go/bin/swarm"; } - return callback(null, swarm_bin); + callback(null, swarm_bin); }, function runCommand(swarm_bin, callback) { let cmd = `"${swarm_bin}" --defaultpath ${self.buildDir} index.html --recursive up ${self.buildDir}`; console.log(("=== adding " + self.buildDir + " to swarm").green); console.log(cmd.green); - let result = shelljs.exec(cmd); - - return callback(null, result); + shelljs.exec(cmd, function(code, stdout, stderr){ + callback(stderr, {code: code, output: stdout}); + }); }, function getHashFromOutput(result, callback) { if (result.code !== 0) { - return callback("couldn't upload, is the swarm daemon running?"); + callback("couldn't upload, is the swarm daemon running?"); } + else{ + let rows = result.output.split("\n"); + let dir_hash = rows.reverse()[1]; - let rows = result.output.split("\n"); - let dir_hash = rows.reverse()[1]; - - return callback(null, dir_hash); + callback(null, dir_hash); + } }, function printUrls(dir_hash, callback) { console.log(("=== DApp available at http://localhost:8500/bzz:/" + dir_hash + "/").green); - return callback(); + callback(); } ], function (err, _result) { if (err) { From 8bdf79b19a58a72f42563294139331962c508e79 Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Sun, 15 Apr 2018 18:50:56 +1000 Subject: [PATCH 5/5] Handling for error during build. --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index da22e054..ef0dbffa 100644 --- a/lib/index.js +++ b/lib/index.js @@ -186,7 +186,7 @@ class Embark { engine.logger.info("finished building".underline); } // needed due to child processes - if(!continueProcessing){ + if(err || !continueProcessing){ process.exit(); } });