add action events to pipeline; generate contracts json and js files

This commit is contained in:
Iuri Matias 2019-07-11 10:13:55 -04:00
parent a71a815035
commit bf91543b20
2 changed files with 104 additions and 18 deletions

View File

@ -44,31 +44,50 @@ class Pipeline {
this.build(options, callback);
});
this.events.setCommandHandler('pipeline:build:contracts', callback => this.buildContracts([], callback));
this.fs.removeSync(this.buildDir);
// TODO: action in the constructor, shoudn't be happening..
// this.fs.removeSync(this.buildDir);
this.api = new PipelineAPI(embark, options);
this.api.registerAPIs();
this.files = {}
this.events.setCommandHandler('pipeline:register', (params) => {
this.events.setCommandHandler('pipeline:register', (params, cb) => {
this.files[dappPath(...params.path, params.file)] = params;
if (cb) {
cb();
}
});
}
generateAll(cb) {
console.dir("generating all files");
// TODO: make this async
for (let fileParams of Object.values(this.files)) {
if (fileParams.format === 'json') {
this.writeJSONFile(fileParams)
} else {
// TODO: other/js
async.waterfall([
(next) => {
this.plugins.runActionsForEvent("pipeline:generateAll:before", {}, (err) => {
next(err);
});
},
(next) => {
// TODO: make this async
for (let fileParams of Object.values(this.files)) {
if (fileParams.format === 'json') {
this.writeJSONFile(fileParams)
} else {
this.writeFile(fileParams)
}
}
next();
},
(next) => {
this.plugins.runActionsForEvent("pipeline:generateAll:after", {}, (err) => {
next(err);
});
}
}
cb();
], () => {
cb();
});
}
writeJSONFile(params) {
@ -88,6 +107,29 @@ class Pipeline {
});
}
writeFile(params) {
const self = this;
const dir = dappPath(...params.path);
const filename = dappPath(...params.path, params.file);
const content = params.content;
async.waterfall([
function makeDirectory(next) {
self.fs.mkdirp(dir, err => next(err));
},
function writeFile(next) {
self.fs.writeFile(filename, content, (err) => { next(err, true) });
}
], () => {
});
}
// =================
// =================
// =================
// =================
// =================
build({modifiedAssets}, callback) {
let self = this;
const importsList = {};

View File

@ -15,7 +15,9 @@ class Web3Plugin {
let plugin = this.plugins.createPlugin('web3plugin', {});
plugin.registerActionForEvent("deploy:contract:deployed", this.registerInVm.bind(this));
plugin.registerActionForEvent("deploy:contract:deployed", this.addToPipeline.bind(this));
plugin.registerActionForEvent("deploy:contract:deployed", this.addContractJSONToPipeline.bind(this));
plugin.registerActionForEvent("deploy:contract:deployed", this.addContractFileToPipeline.bind(this));
plugin.registerActionForEvent("pipeline:generateAll:before", this.addContractIndexToPipeline.bind(this));
}
registerInVm(params, cb) {
@ -37,25 +39,67 @@ class Web3Plugin {
});
}
addToPipeline(params, cb) {
addContractJSONToPipeline(params, cb) {
// TODO: check if this is correct json object to generate
let contract = params.contract;
const contract = params.contract;
this.events.request("pipeline:register", {
path: [this.embarkConfig.buildDir, 'contracts'],
file: contract.className + '.json',
format: 'json',
content: contract
});
}, cb);
}
addContractFileToPipeline(params, cb) {
const contract = params.contract;
const contractName = contract.className;
const contractJSON = contract.abiDefinition;
const contractCode = `
"use strict";
const isNode = (typeof process !== 'undefined' && process.versions && process.versions.node);
const lib = isNode ? '../embarkjs.node' : '../embarkjs';
const EmbarkJSNode = isNode && require('../embarkjs.node');
let EmbarkJSBrowser;
try {
EmbarkJSBrowser = require('../embarkjs').default;
} catch(e) {};
const EmbarkJS = isNode ? EmbarkJSNode : EmbarkJSBrowser;
let ${contractName}JSONConfig = ${JSON.stringify(contractJSON)};
let ${contractName} = new EmbarkJS.Blockchain.Contract(${contractName}JSONConfig);
module.exports = ${contractName};
`.trim().replace(/^[\t\s]+/gm, '');
this.events.request("pipeline:register", {
path: [this.embarkConfig.generationDir, 'contracts'],
file: contract.className + '.js',
format: 'js',
content: contract
});
content: contractCode
}, cb);
}
cb();
addContractIndexToPipeline(_params, cb) {
this.events.request("contracts:list", (err, contracts) => {
let imports = contracts.map((c) => {
return `"${c.className}": require('./${c.className}').default)`;
}).join(",\n");
let code = 'module.exports = {\n';
code += imports;
code += '\n};';
this.events.request("pipeline:register", {
path: [this.embarkConfig.generationDir, 'contracts'],
file: 'index.js',
format: 'js',
content: code
}, cb);
});
}
}