From 85059ffef28cb55c63a553d922c4f853b730e450 Mon Sep 17 00:00:00 2001 From: app Date: Mon, 25 Mar 2019 17:02:44 +0530 Subject: [PATCH] fixed #1 --- bin/sol-profiler.js | 108 +++--------------- .../ERC721Metadata_Profile.txt | 0 {example => profiles}/sample_Profile.txt | 0 utils/data.js | 81 +++++++++++++ utils/file.js | 55 +++++++++ utils/index.js | 64 ++--------- utils/profile.js | 12 ++ 7 files changed, 170 insertions(+), 150 deletions(-) rename {example => profiles}/ERC721Metadata_Profile.txt (100%) rename {example => profiles}/sample_Profile.txt (100%) create mode 100644 utils/data.js create mode 100644 utils/file.js create mode 100644 utils/profile.js diff --git a/bin/sol-profiler.js b/bin/sol-profiler.js index 909bdf8..b09578e 100644 --- a/bin/sol-profiler.js +++ b/bin/sol-profiler.js @@ -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: { @@ -42,28 +42,24 @@ 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]); } }); @@ -71,94 +67,16 @@ async function generateReport(path){ }); /* 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); diff --git a/example/ERC721Metadata_Profile.txt b/profiles/ERC721Metadata_Profile.txt similarity index 100% rename from example/ERC721Metadata_Profile.txt rename to profiles/ERC721Metadata_Profile.txt diff --git a/example/sample_Profile.txt b/profiles/sample_Profile.txt similarity index 100% rename from example/sample_Profile.txt rename to profiles/sample_Profile.txt diff --git a/utils/data.js b/utils/data.js new file mode 100644 index 0000000..e6b363b --- /dev/null +++ b/utils/data.js @@ -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) + }; + } \ No newline at end of file diff --git a/utils/file.js b/utils/file.js new file mode 100644 index 0000000..b3b60df --- /dev/null +++ b/utils/file.js @@ -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; \ No newline at end of file diff --git a/utils/index.js b/utils/index.js index 7c68c0a..1594e3d 100644 --- a/utils/index.js +++ b/utils/index.js @@ -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; \ No newline at end of file +const data = require('./data'), + file = require('./file'), + profile = require('./profile'); + +module.exports = { + data, + file, + profile +}; \ No newline at end of file diff --git a/utils/profile.js b/utils/profile.js new file mode 100644 index 0000000..e1f9815 --- /dev/null +++ b/utils/profile.js @@ -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); +} \ No newline at end of file