From c800598e9be6f33061afb227b17eb55ace6f9a3e Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Sun, 20 May 2018 10:46:36 -0400 Subject: [PATCH] move afterDeploy code into a special configs module --- lib/contracts/deploy_manager.js | 58 ++++------------------ lib/core/engine.js | 3 ++ lib/core/plugin.js | 6 +++ lib/modules/specialconfigs/index.js | 74 +++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 50 deletions(-) create mode 100644 lib/modules/specialconfigs/index.js diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js index 1c526b01..5c6e4bff 100644 --- a/lib/contracts/deploy_manager.js +++ b/lib/contracts/deploy_manager.js @@ -1,7 +1,6 @@ let async = require('async'); //require("../utils/debug_util.js")(__filename, async); let Deploy = require('./deploy.js'); -let RunCode = require('../core/runCode.js'); class DeployManager { constructor(options) { @@ -36,6 +35,8 @@ class DeployManager { callback(); }); }, + + // TODO: shouldn't be necessary function checkCompileOnly(callback){ if(self.onlyCompile){ self.events.emit('contractsDeployed', self.contractsManager); @@ -80,57 +81,14 @@ class DeployManager { callback(); }); }, - function runAfterDeployCommands(callback) { - // TODO: should instead emit a afterDeploy event and/or run a afterDeploy plugin - let afterDeployCmds = self.config.contractsConfig.afterDeploy || []; + function runAfterDeploy(callback) { + let afterDeployPlugins = self.plugins.getPluginsProperty('afterContractsDeployActions', 'afterContractsDeployActions'); - let withErrors = false; - let regex = /\$\w+/g; - let onDeployCode = afterDeployCmds.map((cmd) => { - let realCmd = cmd.replace(regex, (match) => { - let referedContractName = match.slice(1); - let referedContract = self.contractsManager.getContract(referedContractName); - if (!referedContract) { - self.logger.error(referedContractName + ' does not exist'); - self.logger.error(__("error running afterDeploy: ") + cmd); - withErrors = true; - return; - } - if (referedContract && referedContract.deploy === false) { - self.logger.error(referedContractName + " exists but has been set to not deploy"); - self.logger.error(__("error running afterDeploy: ") + cmd); - withErrors = true; - return; - } - if (referedContract && !referedContract.deployedAddress) { - self.logger.error("couldn't find a valid address for " + referedContractName + ". has it been deployed?"); - self.logger.error(__("error running afterDeploy: ") + cmd); - withErrors = true; - return; - } - return referedContract.deployedAddress; - }); - return realCmd; + async.eachLimit(afterDeployPlugins, 1, function(plugin, nextEach) { + plugin.call(plugin, nextEach); + }, () => { + callback(); }); - - if (withErrors) { - return callback(new Error("error running afterDeploy")); - } - - // TODO: convert to for to avoid repeated callback - for(let cmd of onDeployCode) { - self.logger.info(__("executing") + ": " + cmd); - try { - RunCode.doEval(cmd, {web3: self.blockchain.web3}); - } catch(e) { - if (e.message.indexOf("invalid opcode") >= 0) { - self.logger.error(__('the transaction was rejected; this usually happens due to a throw or a require, it can also happen due to an invalid operation')); - } - return callback(new Error(e)); - } - } - - callback(); } ], function (err, _result) { done(err); diff --git a/lib/core/engine.js b/lib/core/engine.js index ebb7afb4..f9339900 100644 --- a/lib/core/engine.js +++ b/lib/core/engine.js @@ -212,6 +212,9 @@ class Engine { this.registerModule('deploytracker', { }); + this.registerModule('specialconfigs', { + }); + this.contractsManager = new ContractsManager({ contractFiles: this.config.contractsFiles, contractsConfig: this.config.contractsConfig, diff --git a/lib/core/plugin.js b/lib/core/plugin.js index b7d61128..b3ba3e34 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -25,6 +25,7 @@ var Plugin = function(options) { this.imports = []; this.embarkjs_code = []; this.embarkjs_init_code = {}; + this.afterContractsDeployActions = []; this.logger = options.logger; this.events = options.events; this.config = options.config; @@ -203,6 +204,11 @@ Plugin.prototype.registerImportFile = function(importName, importLocation) { this.pluginTypes.push('imports'); }; +Plugin.prototype.registerAfterAllContractsDeploy = function(cb) { + this.afterContractsDeployActions.push(cb); + this.pluginTypes.push('afterContractsDeployActions'); +}; + Plugin.prototype.runFilePipeline = function() { var self = this; diff --git a/lib/modules/specialconfigs/index.js b/lib/modules/specialconfigs/index.js new file mode 100644 index 00000000..fb17b4b7 --- /dev/null +++ b/lib/modules/specialconfigs/index.js @@ -0,0 +1,74 @@ +let RunCode = require('../../core/runCode.js'); + +class SpecialConfigs { + + constructor(embark, options) { + this.logger = embark.logger; + this.events = embark.events; + this.buildDir = options.buildDir; + this.addCheck = options.addCheck; + this.embark = embark; + this.contractsConfig = embark.config.contractsConfig; + + this.registerAfterDeployAction(); + } + + registerAfterDeployAction() { + const self = this; + + this.embark.registerAfterAllContractsDeploy((cb) => { + let afterDeployCmds = self.contractsConfig.afterDeploy || []; + + let withErrors = false; + let regex = /\$\w+/g; + let onDeployCode = afterDeployCmds.map((cmd) => { + let realCmd = cmd.replace(regex, (match) => { + let referedContractName = match.slice(1); + let referedContract = self.contractsManager.getContract(referedContractName); + if (!referedContract) { + self.logger.error(referedContractName + ' does not exist'); + self.logger.error("error running afterDeploy: " + cmd); + withErrors = true; + return; + } + if (referedContract && referedContract.deploy === false) { + self.logger.error(referedContractName + " exists but has been set to not deploy"); + self.logger.error("error running afterDeploy: " + cmd); + withErrors = true; + return; + } + if (referedContract && !referedContract.deployedAddress) { + self.logger.error("couldn't find a valid address for " + referedContractName + ". has it been deployed?"); + self.logger.error("error running afterDeploy: " + cmd); + withErrors = true; + return; + } + return referedContract.deployedAddress; + }); + return realCmd; + }); + + if (withErrors) { + return cb(new Error("error running afterDeploy")); + } + + // TODO: convert to for to avoid repeated callback + for(let cmd of onDeployCode) { + self.logger.info("executing: " + cmd); + try { + RunCode.doEval(cmd, self.blockchain.web3); + } catch(e) { + if (e.message.indexOf("invalid opcode") >= 0) { + self.logger.error('the transaction was rejected; this usually happens due to a throw or a require, it can also happen due to an invalid operation'); + } + return cb(new Error(e)); + } + } + + cb(); + }); + } + +} + +module.exports = SpecialConfigs;