Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Profiles are stored in a directory #4

Merged
merged 1 commit into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 13 additions & 95 deletions bin/sol-profiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const parser= require("solparse"),
clc = require("cli-color"),
table = require('table'),
utils = require('./../utils');
{data, file, profile} = require('./../utils');

let config = {
border: {
Expand Down Expand Up @@ -42,123 +42,41 @@ async function generateReport(path){

// Adding filename and solidity version
let version;

let pragma = await utils.getPragma(path);
let code = await utils.processFile(path);
let pragma = await file.getPragma(path);
let code = await file.process(path);
let source = parser.parse(pragma + '\n\n' + code);
/* jshint ignore:end */
if(source.body[0].type == 'PragmaStatement')
version = source.body[0].start_version.version;

let fileArray = path.split('/');
let file = fileArray[fileArray.length -1];
let contractName = file.substr(0, file.length - 4);
tableRows.push(['',clc.greenBright("File: " + file +
" , Solidity Pragma: " + version), '','','','']);
let fileName = fileArray[fileArray.length -1];
let contractName = fileName.substr(0, fileName.length - 4);
tableRows.push(['',clc.greenBright("File: " + fileName + " , Solidity Pragma: " + version), '','','','']);

// Adding header row
tableRows.push([clc.whiteBright.bold('Contract/Library'), clc.whiteBright.bold('Function/Constructor'), clc.whiteBright.bold('Visibility'), clc.whiteBright.bold('View/Pure'), clc.whiteBright.bold('Returns'), clc.whiteBright.bold('Modifiers')]);

source.body.forEach(function(contract) {
if(contract.type == 'ContractStatement' || contract.type == 'LibraryStatement') {
contract.body.forEach(function(part) {
if(part.type == 'ConstructorDeclaration' || (part.type == 'FunctionDeclaration' && part.is_abstract == false)) {
let {contractName, functionName, visibility, viewOrPure, returns, modifiers} = parsePartData(contract, part);
let {contractName, functionName, visibility, viewOrPure, returns, modifiers} = data.parseData(contract, part);
tableRows.push([contractName, functionName, visibility, viewOrPure, returns, modifiers]);
}
});
}
});
/* jshint ignore:start */
var tableData = table.table(tableRows, config); /* jshint ignore:end */
let fileData = tableData.replace(
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, ''); // clearing color formatting
require("fs").writeFileSync(contractName + "_Profile.txt", fileData);
console.log(tableData);

// Store profile in profiles folder
profile.store(tableData, contractName);

//Render profile on console
console.log(tableData);
}catch(error){
console.log(clc.red(error.message));
};

}

function parsePartData(contract, part) {
let contractName = clc.cyanBright(contract.name);
if(contract.type == 'LibraryStatement')
contractName = contractName + clc.white(' -lib');

let funcName = null;
if(part.type == 'ConstructorDeclaration')
funcName = 'constructor';
else if(part.type == 'FunctionDeclaration'){
funcName = part.name || '';
}

let params = [];
if(part.params) {
part.params.forEach(function(param) {
if(param.storage_location)
params.push(param.literal.literal + ' ' + clc.cyan(param.storage_location));
else
params.push(param.literal.literal);
});
funcName += '(' + params.join(',') + ')';
}
else {
//Check fallback
if(!funcName && !part.name && !part.params && !part.returnParams)
funcName = '()' + clc.white(' -fallback');
else
funcName += '()';
}

// Default is public
let visibility = clc.magentaBright("public");
let viewOrPure = '';
let returns = [];
let custom = [];

if(part.modifiers) {
part.modifiers.forEach(function(mod) {
switch(mod.name) {
case "public":
break;
case "private":
visibility = clc.redBright("private");
break;
case "internal":
visibility = clc.red("internal");
break;
case "external":
visibility = clc.magenta("external");
break;
case "view":
viewOrPure = clc.yellow("view");
break;
case "pure":
viewOrPure = clc.yellowBright("pure");
break;
default:
custom.push(mod.name);
}
});
}
if(part.returnParams) {
part.returnParams.params.forEach(function(param) {
if(param.storage_location)
returns.push(param.literal.literal + ' ' + clc.cyan(param.storage_location));
else
returns.push(param.literal.literal);
});
}

return {
contractName: contractName,
functionName: clc.blueBright(funcName),
visibility : visibility,
viewOrPure : viewOrPure,
returns : clc.white(returns),
modifiers : clc.white(custom)
};
}

module.exports = generateReport(filePath);
File renamed without changes.
File renamed without changes.
81 changes: 81 additions & 0 deletions utils/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const clc = require("cli-color");

module.exports.parseData = (contract, part) => {
let contractName = clc.cyanBright(contract.name);
if(contract.type == 'LibraryStatement')
contractName = contractName + clc.white(' -lib');

let funcName = null;
if(part.type == 'ConstructorDeclaration')
funcName = 'constructor';
else if(part.type == 'FunctionDeclaration'){
funcName = part.name || '';
}

let params = [];
if(part.params) {
part.params.forEach(function(param) {
if(param.storage_location)
params.push(param.literal.literal + ' ' + clc.cyan(param.storage_location));
else
params.push(param.literal.literal);
});
funcName += '(' + params.join(',') + ')';
}
else {
//Check fallback
if(!funcName && !part.name && !part.params && !part.returnParams)
funcName = '()' + clc.white(' -fallback');
else
funcName += '()';
}

// Default is public
let visibility = clc.magentaBright("public");
let viewOrPure = '';
let returns = [];
let custom = [];

if(part.modifiers) {
part.modifiers.forEach(function(mod) {
switch(mod.name) {
case "public":
break;
case "private":
visibility = clc.redBright("private");
break;
case "internal":
visibility = clc.red("internal");
break;
case "external":
visibility = clc.magenta("external");
break;
case "view":
viewOrPure = clc.yellow("view");
break;
case "pure":
viewOrPure = clc.yellowBright("pure");
break;
default:
custom.push(mod.name);
}
});
}
if(part.returnParams) {
part.returnParams.params.forEach(function(param) {
if(param.storage_location)
returns.push(param.literal.literal + ' ' + clc.cyan(param.storage_location));
else
returns.push(param.literal.literal);
});
}

return {
contractName: contractName,
functionName: clc.blueBright(funcName),
visibility : visibility,
viewOrPure : viewOrPure,
returns : clc.white(returns),
modifiers : clc.white(custom)
};
}
55 changes: 55 additions & 0 deletions utils/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const path = require('path'),
fs = require('fs');

var regEx = {
pragma : /(pragma solidity (.+?);)/g,
import : /import ['"](.+?)['"];/g
};

var processedFiles = [];

var processFile = async(file) => {

if (processedFiles.indexOf(file) !== -1)
return;

processedFiles.push(file);
let result = '';

let contents = fs.readFileSync(file, { encoding: 'utf-8' });
contents = contents.replace(regEx.pragma, '').trim();
let imports = await processImports(file, contents);

for (let i = 0; i < imports.length; i++) {
result += imports[i] + '\n\n';
}
contents = contents.replace(regEx.import, '').trim();
result += contents;
return result;
}

var processImports = async (file, content) => {
let group='';
let result = [];
regEx.import.exec(''); // Resetting state of RegEx
while (group = regEx.import.exec(content)) {
let _importFile = group[1];
let filePath = path.join(path.dirname(file), _importFile);
filePath = path.normalize(filePath);
let fileContents = await processFile(filePath);
if (fileContents) {
result.push(fileContents);
}
}
return result;

}

var getPragma = async(path) => {
let contents = fs.readFileSync(path, { encoding: 'utf-8' });
let group = regEx.pragma.exec(contents);
return group && group[1];
}

module.exports.process = processFile;
module.exports.getPragma = getPragma;
64 changes: 9 additions & 55 deletions utils/index.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,9 @@
const path = require('path'),
fs = require('fs');

var regEx = {
pragma : /(pragma solidity (.+?);)/g,
import : /import ['"](.+?)['"];/g
};

var processedFiles = [];

var processFile = async(file) => {

if (processedFiles.indexOf(file) !== -1)
return;

processedFiles.push(file);
let result = '';

let contents = fs.readFileSync(file, { encoding: 'utf-8' });
contents = contents.replace(regEx.pragma, '').trim();
let imports = await processImports(file, contents);

for (let i = 0; i < imports.length; i++) {
result += imports[i] + '\n\n';
}
contents = contents.replace(regEx.import, '').trim();
result += contents;
return result;
}

var processImports = async (file, content) => {
let group='';
let result = [];
regEx.import.exec(''); // Resetting state of RegEx
while (group = regEx.import.exec(content)) {
let _importFile = group[1];
let filePath = path.join(path.dirname(file), _importFile);
filePath = path.normalize(filePath);
let fileContents = await processFile(filePath);
if (fileContents) {
result.push(fileContents);
}
}
return result;

}

var getPragma = async(path) => {
let contents = fs.readFileSync(path, { encoding: 'utf-8' });
let group = regEx.pragma.exec(contents);
return group && group[1];
}

module.exports.processFile = processFile;
module.exports.getPragma = getPragma;
const data = require('./data'),
file = require('./file'),
profile = require('./profile');

module.exports = {
data,
file,
profile
};
12 changes: 12 additions & 0 deletions utils/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const path = require('path'),
fs = require('fs');


module.exports.store = (data, contractName) => {
if(!fs.existsSync(path.join(process.cwd(), '/profiles')))
fs.mkdirSync(path.join(process.cwd(), '/profiles'));
let fileData = data.replace(
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, ''); // clearing color formatting

fs.writeFileSync(path.join(process.cwd(), 'profiles', contractName + '_Profile.txt'), fileData);
}