move afterDeploy code into a special configs module

This commit is contained in:
Iuri Matias 2018-05-20 10:46:36 -04:00 committed by Jonathan Rainville
parent 21026e07ae
commit c800598e9b
4 changed files with 91 additions and 50 deletions

View File

@ -1,7 +1,6 @@
let async = require('async'); let async = require('async');
//require("../utils/debug_util.js")(__filename, async); //require("../utils/debug_util.js")(__filename, async);
let Deploy = require('./deploy.js'); let Deploy = require('./deploy.js');
let RunCode = require('../core/runCode.js');
class DeployManager { class DeployManager {
constructor(options) { constructor(options) {
@ -36,6 +35,8 @@ class DeployManager {
callback(); callback();
}); });
}, },
// TODO: shouldn't be necessary
function checkCompileOnly(callback){ function checkCompileOnly(callback){
if(self.onlyCompile){ if(self.onlyCompile){
self.events.emit('contractsDeployed', self.contractsManager); self.events.emit('contractsDeployed', self.contractsManager);
@ -80,57 +81,14 @@ class DeployManager {
callback(); callback();
}); });
}, },
function runAfterDeployCommands(callback) { function runAfterDeploy(callback) {
// TODO: should instead emit a afterDeploy event and/or run a afterDeploy plugin let afterDeployPlugins = self.plugins.getPluginsProperty('afterContractsDeployActions', 'afterContractsDeployActions');
let afterDeployCmds = self.config.contractsConfig.afterDeploy || [];
let withErrors = false; async.eachLimit(afterDeployPlugins, 1, function(plugin, nextEach) {
let regex = /\$\w+/g; plugin.call(plugin, nextEach);
let onDeployCode = afterDeployCmds.map((cmd) => { }, () => {
let realCmd = cmd.replace(regex, (match) => { callback();
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 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) { ], function (err, _result) {
done(err); done(err);

View File

@ -212,6 +212,9 @@ class Engine {
this.registerModule('deploytracker', { this.registerModule('deploytracker', {
}); });
this.registerModule('specialconfigs', {
});
this.contractsManager = new ContractsManager({ this.contractsManager = new ContractsManager({
contractFiles: this.config.contractsFiles, contractFiles: this.config.contractsFiles,
contractsConfig: this.config.contractsConfig, contractsConfig: this.config.contractsConfig,

View File

@ -25,6 +25,7 @@ var Plugin = function(options) {
this.imports = []; this.imports = [];
this.embarkjs_code = []; this.embarkjs_code = [];
this.embarkjs_init_code = {}; this.embarkjs_init_code = {};
this.afterContractsDeployActions = [];
this.logger = options.logger; this.logger = options.logger;
this.events = options.events; this.events = options.events;
this.config = options.config; this.config = options.config;
@ -203,6 +204,11 @@ Plugin.prototype.registerImportFile = function(importName, importLocation) {
this.pluginTypes.push('imports'); this.pluginTypes.push('imports');
}; };
Plugin.prototype.registerAfterAllContractsDeploy = function(cb) {
this.afterContractsDeployActions.push(cb);
this.pluginTypes.push('afterContractsDeployActions');
};
Plugin.prototype.runFilePipeline = function() { Plugin.prototype.runFilePipeline = function() {
var self = this; var self = this;

View File

@ -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;