2018-05-20 16:23:48 +00:00
|
|
|
const stringReplaceAsync = require('string-replace-async');
|
|
|
|
const async = require('async');
|
2018-05-20 14:46:36 +00:00
|
|
|
|
|
|
|
class SpecialConfigs {
|
|
|
|
|
|
|
|
constructor(embark, options) {
|
|
|
|
this.logger = embark.logger;
|
|
|
|
this.events = embark.events;
|
|
|
|
this.buildDir = options.buildDir;
|
|
|
|
this.embark = embark;
|
|
|
|
this.contractsConfig = embark.config.contractsConfig;
|
|
|
|
|
|
|
|
this.registerAfterDeployAction();
|
2018-05-20 23:29:26 +00:00
|
|
|
this.registerOnDeployAction();
|
2018-08-23 22:00:52 +00:00
|
|
|
this.registerDeployIfAction();
|
2018-05-20 14:46:36 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 16:23:48 +00:00
|
|
|
replaceWithAddresses(cmd, cb) {
|
2018-05-20 14:46:36 +00:00
|
|
|
const self = this;
|
2018-05-20 16:23:48 +00:00
|
|
|
let regex = /\$\w+/g;
|
|
|
|
stringReplaceAsync.seq(cmd, regex, (match) => {
|
|
|
|
return (new Promise((resolve, reject) => {
|
|
|
|
let referedContractName = match.slice(1);
|
|
|
|
self.events.request('contracts:contract', referedContractName, (referedContract) => {
|
2018-05-20 14:46:36 +00:00
|
|
|
if (!referedContract) {
|
|
|
|
self.logger.error(referedContractName + ' does not exist');
|
2018-05-20 23:29:26 +00:00
|
|
|
self.logger.error("error running cmd: " + cmd);
|
2018-05-20 16:23:48 +00:00
|
|
|
return reject(new Error("ReferedContractDoesNotExist"));
|
2018-05-20 14:46:36 +00:00
|
|
|
}
|
|
|
|
if (referedContract && referedContract.deploy === false) {
|
|
|
|
self.logger.error(referedContractName + " exists but has been set to not deploy");
|
2018-05-20 23:29:26 +00:00
|
|
|
self.logger.error("error running cmd: " + cmd);
|
2018-05-20 16:23:48 +00:00
|
|
|
return reject(new Error("ReferedContracSetToNotdeploy"));
|
2018-05-20 14:46:36 +00:00
|
|
|
}
|
|
|
|
if (referedContract && !referedContract.deployedAddress) {
|
|
|
|
self.logger.error("couldn't find a valid address for " + referedContractName + ". has it been deployed?");
|
2018-05-20 23:29:26 +00:00
|
|
|
self.logger.error("error running cmd: " + cmd);
|
2018-05-20 16:23:48 +00:00
|
|
|
return reject(new Error("ReferedContractAddressNotFound"));
|
2018-05-20 14:46:36 +00:00
|
|
|
}
|
2018-05-20 16:23:48 +00:00
|
|
|
return resolve(referedContract.deployedAddress);
|
2018-05-20 14:46:36 +00:00
|
|
|
});
|
2018-05-20 16:23:48 +00:00
|
|
|
}));
|
|
|
|
}).then((result) => {
|
|
|
|
cb(null, result);
|
|
|
|
}).catch(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
registerAfterDeployAction() {
|
|
|
|
const self = this;
|
|
|
|
|
2018-05-28 22:49:18 +00:00
|
|
|
this.embark.registerActionForEvent("contracts:deploy:afterAll", (cb) => {
|
2018-05-20 16:23:48 +00:00
|
|
|
let afterDeployCmds = self.contractsConfig.afterDeploy || [];
|
2018-05-20 14:46:36 +00:00
|
|
|
|
2018-05-20 16:23:48 +00:00
|
|
|
async.mapLimit(afterDeployCmds, 1, (cmd, nextMapCb) => {
|
|
|
|
self.replaceWithAddresses(cmd, nextMapCb);
|
2018-06-14 15:10:31 +00:00
|
|
|
}, (err, onDeployCode) => {
|
2018-05-20 16:23:48 +00:00
|
|
|
if (err) {
|
2018-06-01 04:06:02 +00:00
|
|
|
self.logger.trace(err);
|
2018-05-20 16:23:48 +00:00
|
|
|
return cb(new Error("error running afterDeploy"));
|
|
|
|
}
|
2018-06-14 15:10:31 +00:00
|
|
|
|
|
|
|
self.runOnDeployCode(onDeployCode, cb);
|
2018-05-20 16:23:48 +00:00
|
|
|
});
|
2018-05-20 14:46:36 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-07 20:04:26 +00:00
|
|
|
runOnDeployCode(onDeployCode, callback, silent) {
|
2018-06-14 15:10:31 +00:00
|
|
|
const self = this;
|
2018-08-07 20:04:26 +00:00
|
|
|
const logFunction = silent ? self.logger.trace.bind(self.logger) : self.logger.info.bind(self.logger);
|
2018-06-14 15:10:31 +00:00
|
|
|
async.each(onDeployCode, (cmd, eachCb) => {
|
2018-08-07 20:04:26 +00:00
|
|
|
logFunction("==== executing: " + cmd);
|
2018-06-14 15:10:31 +00:00
|
|
|
self.events.request('runcode:eval', cmd, (err) => {
|
|
|
|
if (err && err.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');
|
|
|
|
}
|
|
|
|
eachCb(err);
|
|
|
|
});
|
|
|
|
}, callback);
|
|
|
|
}
|
|
|
|
|
2018-05-20 23:29:26 +00:00
|
|
|
registerOnDeployAction() {
|
|
|
|
const self = this;
|
|
|
|
|
2018-05-28 23:40:55 +00:00
|
|
|
this.embark.registerActionForEvent("deploy:contract:deployed", (params, cb) => {
|
|
|
|
let contract = params.contract;
|
|
|
|
|
2018-05-20 23:29:26 +00:00
|
|
|
if (!contract.onDeploy) {
|
|
|
|
return cb();
|
|
|
|
}
|
2018-08-07 20:04:26 +00:00
|
|
|
if (!contract.silent) {
|
|
|
|
self.logger.info(__('executing onDeploy commands'));
|
|
|
|
}
|
2018-05-20 23:29:26 +00:00
|
|
|
|
|
|
|
let onDeployCmds = contract.onDeploy;
|
|
|
|
|
|
|
|
async.mapLimit(onDeployCmds, 1, (cmd, nextMapCb) => {
|
|
|
|
self.replaceWithAddresses(cmd, nextMapCb);
|
2018-06-14 15:10:31 +00:00
|
|
|
}, (err, onDeployCode) => {
|
2018-05-20 23:29:26 +00:00
|
|
|
if (err) {
|
|
|
|
return cb(new Error("error running onDeploy for " + contract.className.cyan));
|
|
|
|
}
|
2018-06-14 15:10:31 +00:00
|
|
|
|
2018-08-07 20:04:26 +00:00
|
|
|
self.runOnDeployCode(onDeployCode, cb, contract.silent);
|
2018-05-20 23:29:26 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-08-20 21:25:30 +00:00
|
|
|
registerDeployIfAction() {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
self.embark.registerActionForEvent("deploy:contract:shouldDeploy", (params, cb) => {
|
2018-08-23 22:00:52 +00:00
|
|
|
let cmd = params.contract.deployIf;
|
|
|
|
if (!cmd) {
|
|
|
|
return cb(params);
|
2018-08-20 21:25:30 +00:00
|
|
|
}
|
2018-08-23 22:00:52 +00:00
|
|
|
|
|
|
|
self.events.request('runcode:eval', cmd, (err, result) => {
|
|
|
|
if (err) {
|
|
|
|
self.logger.error(params.contract.className + ' deployIf directive has an error; contract will not deploy');
|
|
|
|
self.logger.error(err);
|
|
|
|
params.shouldDeploy = false;
|
|
|
|
} else if (!result) {
|
|
|
|
self.logger.info(params.contract.className + ' deployIf directive returned false; contract will not deploy');
|
|
|
|
params.shouldDeploy = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(params);
|
|
|
|
});
|
2018-08-20 21:25:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-20 14:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SpecialConfigs;
|