Merge pull request #397 from embark-framework/graph-skipElements

Improvements to graph functionality
This commit is contained in:
Iuri Matias 2018-05-04 22:12:26 -04:00 committed by GitHub
commit 204684ef07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 18 deletions

View File

@ -191,11 +191,17 @@ class Cmd {
graph() {
program
.command('graph [environment]')
.option('--skip-undeployed', 'Graph will not include undeployed contracts')
.option('--skip-functions', 'Graph will not include functions')
.option('--skip-events', 'Graph will not include events')
.description('generates documentation based on the smart contracts configured')
.action(function (env, options) {
embark.graph({
env: env || 'development',
logFile: options.logfile
logFile: options.logfile,
skipUndeployed: options.skipUndeployed,
skipFunctions: options.skipFunctions,
skipEvents: options.skipEvents
});
});
}

View File

@ -6,15 +6,16 @@ class GraphGenerator {
this.engine = engine;
}
generate() {
generate(options) {
let id = 0;
let contractString = "";
let relationshipString = "";
let idMapping = {};
let contractInheritance = {};
for (let contract in this.engine.contractsManager.contracts) {
if(options.skipUndeployed && !this.engine.contractsManager.contracts[contract].deploy) continue;
id++;
idMapping[contract] = id;
@ -30,23 +31,27 @@ class GraphGenerator {
contractLabel += ": " + this.engine.contractsManager.contracts[contract].instanceOf;
tooltip += " instance of " + this.engine.contractsManager.contracts[contract].instanceOf;
} else {
contractLabel += "|";
if(!(options.skipFunctions === true && options.skipEvents === true)) contractLabel += "|";
for(let i = 0; i < this.engine.contractsManager.contracts[contract].abiDefinition.length; i++){
switch(this.engine.contractsManager.contracts[contract].abiDefinition[i].type){
let abiDef = this.engine.contractsManager.contracts[contract].abiDefinition[i];
if(abiDef.type == 'event' && options.skipEvents) continue;
if(['constructor', 'fallback'].indexOf(abiDef.type) > -1 && options.skipFunctions) continue;
switch(abiDef.type){
case 'fallback':
contractLabel += "«fallback»()\\l";
break;
case 'constructor':
case 'constructor':
contractLabel += "«constructor»(";
this.engine.contractsManager.contracts[contract].abiDefinition[i].inputs.forEach(function(elem, index){
abiDef.inputs.forEach(function(elem, index){
contractLabel += (index == 0 ? "" : ", ") + elem.type;
});
contractLabel += ")\\l";
break;
case 'event':
contractLabel += "«event»" + this.engine.contractsManager.contracts[contract].abiDefinition[i].name + "(";
this.engine.contractsManager.contracts[contract].abiDefinition[i].inputs.forEach(function(elem, index){
case 'event':
contractLabel += "«event»" + abiDef.name + "(";
abiDef.inputs.forEach(function(elem, index){
contractLabel += (index == 0 ? "" : ", ") + elem.type;
});
contractLabel += ")\\l";
@ -56,7 +61,7 @@ class GraphGenerator {
}
let fHashes = this.engine.contractsManager.contracts[contract].functionHashes;
if(fHashes != {} && fHashes != undefined){
if(fHashes != {} && fHashes != undefined && !options.skipFunctions){
for(let method in this.engine.contractsManager.contracts[contract].functionHashes){
contractLabel += method + '\\l';
}
@ -73,20 +78,23 @@ class GraphGenerator {
}
for (let c in this.engine.contractsManager.contractDependencies){
for (let c in this.engine.contractsManager.contractDependencies){
let contractDependencies = Array.from(new Set(this.engine.contractsManager.contractDependencies[c]));
contractDependencies.forEach(function(d){
contractDependencies.forEach((d) => {
if(idMapping[c] !== undefined && idMapping[d] !== undefined){
relationshipString += `${idMapping[d]}->${idMapping[c]}[constraint=true, arrowtail=diamond, tooltip="${c} uses ${d}"]\n`;
if(options.skipUndeployed && this.engine.contractsManager.contracts[c].deploy && this.engine.contractsManager.contracts[d].deploy){
relationshipString += `${idMapping[d]}->${idMapping[c]}[constraint=true, arrowtail=diamond, tooltip="${c} uses ${d}"]\n`;
}
}
});
}
for (let c in contractInheritance){
if(options.skipUndeployed && !this.engine.contractsManager.contracts[contractInheritance[c]].deploy) continue;
relationshipString += `${idMapping[contractInheritance[c]]}->${idMapping[c]}[tooltip="${c} instance of ${contractInheritance[c]}"]\n`;
}
let dot = `
digraph Contracts {
node[shape=record,style=filled]

View File

@ -254,7 +254,7 @@ class Embark {
callback(err);
});
}
], function (err, _result) {
], (err, _result) => {
if (err) {
engine.logger.error(err.message);
engine.logger.info(err.stack);
@ -262,9 +262,9 @@ class Embark {
const GraphGenerator = require('./cmds/graph.js');
let graphGen = new GraphGenerator(engine);
graphGen.generate();
graphGen.generate(options);
engine.logger.info("Done".underline);
engine.logger.info("Done. ./diagram.svg generated".underline);
process.exit();
}
});