From a56431d19a88bb99c3879c676f7b6e6ccf8b220d Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 20 Aug 2018 17:25:30 -0400 Subject: [PATCH] refactor run actions for events to a reduce; add initial plug for shouldDeploy using deployIf --- lib/core/plugins.js | 13 +++++++++---- lib/modules/deployment/contract_deployer.js | 15 ++++++++++++--- lib/modules/deploytracker/index.js | 13 +++++++++++-- lib/modules/specialconfigs/index.js | 21 +++++++++++++++++++++ 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/lib/core/plugins.js b/lib/core/plugins.js index bbf717c2..9c80276d 100644 --- a/lib/core/plugins.js +++ b/lib/core/plugins.js @@ -138,15 +138,20 @@ Plugins.prototype.runActionsForEvent = function(eventName, args, cb) { let actionPlugins = this.getPluginsProperty('eventActions', 'eventActions', eventName); if (actionPlugins.length === 0) { - return cb(); + return cb(args); } - async.eachLimit(actionPlugins, 1, function(plugin, nextEach) { + //async.eachLimit(actionPlugins, 1, function(plugin, nextEach) { + async.reduce(actionPlugins, args, function(current_args, plugin, nextEach) { if (typeof (args) === 'function') { - plugin.call(plugin, nextEach); + plugin.call(plugin, (params) => { + nextEach(null, (params || current_args)); + }); } else { //plugin.call(plugin, ...args, nextEach); - plugin.call(plugin, args, nextEach); + plugin.call(plugin, args, (params) => { + nextEach(null, (params || current_args)); + }); } }, cb); }; diff --git a/lib/modules/deployment/contract_deployer.js b/lib/modules/deployment/contract_deployer.js index 635aaaa4..32acc7e6 100644 --- a/lib/modules/deployment/contract_deployer.js +++ b/lib/modules/deployment/contract_deployer.js @@ -145,8 +145,15 @@ class ContractDeployer { return self.deployContract(contract, next); } // TODO: this should be a plugin API instead, if not existing, it should by default deploy the contract - self.events.request("deploy:contract:shouldDeploy", contract, function(trackedContract) { - if (!trackedContract) { + //self.events.request("deploy:contract:shouldDeploy", contract, function(trackedContract) { + self.plugins.emitAndRunActionsForEvent('deploy:contract:shouldDeploy', {contract: contract, shouldDeploy: true}, function(_err, params) { + console.dir(contract.className); + console.dir(arguments); + let trackedContract = params.contract; + if (!params.shouldDeploy) { + return next(); + } + if (!trackedContract.address) { return self.deployContract(contract, next); } @@ -213,7 +220,9 @@ class ContractDeployer { }); }, function applyBeforeDeploy(next) { - self.plugins.emitAndRunActionsForEvent('deploy:contract:beforeDeploy', {contract: contract}, next); + self.plugins.emitAndRunActionsForEvent('deploy:contract:beforeDeploy', {contract: contract}, () => { + next(); + }); }, function getGasPriceForNetwork(next) { self.events.request("blockchain:gasPrice", (gasPrice) => { diff --git a/lib/modules/deploytracker/index.js b/lib/modules/deploytracker/index.js index 1d37f531..7406edb0 100644 --- a/lib/modules/deploytracker/index.js +++ b/lib/modules/deploytracker/index.js @@ -6,6 +6,7 @@ class DeployTracker { constructor(embark, options) { this.logger = embark.logger; this.events = embark.events; + this.embark = embark; // TODO: unclear where it comes from this.env = options.env; @@ -24,9 +25,17 @@ class DeployTracker { self.save(); }); - this.events.setCommandHandler("deploy:contract:shouldDeploy", (contract, cb) => { + //this.events.setCommandHandler("deploy:contract:shouldDeploy", (contract, cb) => { + self.embark.registerActionForEvent("deploy:contract:shouldDeploy", (params, cb) => { + let contract = params.contract; let trackedContract = self.getContract(contract.className, contract.realRuntimeBytecode, contract.realArgs); - cb(trackedContract); + if (trackedContract) { + params.contract.address = trackedContract.address; + } + if (params.shouldDeploy && trackedContract) { + params.shouldDeploy = true; + } + cb(params); }); } diff --git a/lib/modules/specialconfigs/index.js b/lib/modules/specialconfigs/index.js index 154bbda3..e3fa9b1a 100644 --- a/lib/modules/specialconfigs/index.js +++ b/lib/modules/specialconfigs/index.js @@ -12,6 +12,7 @@ class SpecialConfigs { this.registerAfterDeployAction(); this.registerOnDeployAction(); + this.registerDeployIfAction() } replaceWithAddresses(cmd, cb) { @@ -104,6 +105,26 @@ class SpecialConfigs { }); } + registerDeployIfAction() { + const self = this; + + self.embark.registerActionForEvent("deploy:contract:shouldDeploy", (params, cb) => { + console.dir(params.contract.className) + console.dir(params.contract.classNideployIf) + self.embark.logger.info("===applying shouldDeploy plugin..."); + if (params.contract.deployIf === 'hello') { + console.dir("-------------") + console.dir("-------------") + console.dir("contract has the param") + console.dir("-------------") + console.dir("-------------") + } + params.shouldDeploy = (params.contract.deployIf !== 'hello'); + //cb(true, contract); + cb(params); + }); + } + } module.exports = SpecialConfigs;