From 19945fc4f019328a6d7fc0ec8ef64375549ebf1f Mon Sep 17 00:00:00 2001 From: prc Date: Thu, 17 Sep 2020 01:57:32 +0100 Subject: [PATCH 1/2] implemented compile multiple sources in same contract when in subfolder --- cli-commands/commands/compile/index.js | 11 +++++---- .../commands/compile/options/path-option.js | 24 +++++++++++++------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/cli-commands/commands/compile/index.js b/cli-commands/commands/compile/index.js index b68ef82..9938ca1 100644 --- a/cli-commands/commands/compile/index.js +++ b/cli-commands/commands/compile/index.js @@ -27,8 +27,8 @@ class CompileCommand extends Command { fileSysUtils.createDir(compiledDirectories.COMPILED); for (let i = 0; i < optionsResults.path.length; i++) { - const contractPath = optionsResults.path[i]; - await processCompilation(contractPath) + const contract = optionsResults.path[i]; + await processCompilation(contract) } } else { MESSAGE_CONTRACT.NotFound(); @@ -41,12 +41,13 @@ class CompileCommand extends Command { const processCompilation = async function (contract) { try { - const asyncSoftExec = new AsyncSoftExec(`eosio-cpp -I . -o ./compiled/${contract.fileName}.wasm ${contract.fullPath} --abigen`); + const sources = contract.files.join(' '); + const asyncSoftExec = new AsyncSoftExec(`eosio-cpp -I . -o ./compiled/${contract.name}.wasm ${sources} --abigen`); await asyncSoftExec.exec(); - MESSAGE_CONTRACT.Compiled(contract.fileName); + MESSAGE_CONTRACT.Compiled(contract.name); } catch (error) { - MESSAGE_CONTRACT.NotCompiled(error, contract.fileName); + MESSAGE_CONTRACT.NotCompiled(error, contract.name); } } diff --git a/cli-commands/commands/compile/options/path-option.js b/cli-commands/commands/compile/options/path-option.js index 1035a88..816609d 100644 --- a/cli-commands/commands/compile/options/path-option.js +++ b/cli-commands/commands/compile/options/path-option.js @@ -19,17 +19,27 @@ class PathOption extends Option { async process (optionValue) { if (fileSystemUtil.isDir(optionValue)) { const dirFiles = await fileSystemUtil.recursivelyReadDir(optionValue); - const contractsFiles = dirFiles.filter(dirFile => dirFile.fileName.endsWith('.cpp')); - // Return the contracts file names without the .cpp extension - return contractsFiles.map((contractFile) => { + const cppFiles = dirFiles.filter(dirFile => dirFile.fileName.endsWith('.cpp')); + // Get relative paths from base path + const relFiles = cppFiles.map(f => { f.relativePath = path.relative(optionValue,f.fullPath); return f; }); + // Group by contract + const contractsFiles = relFiles.reduce((groups, f) => { + let contract = path.dirname(f.relativePath); + if (contract === '' || contract === '.') contract = path.basename(f.fileName, '.cpp'); + if (!groups[contract]) groups[contract] = []; + groups[contract].push(f.fullPath); + return groups; + }, {}); + // Return the contracts files groups + return Object.keys(contractsFiles).map((contract) => { return { - fullPath: contractFile.fullPath, - fileName: contractFile.fileName.slice(0, -4) + name: contract, + files: contractsFiles[contract] } }); } - - return optionValue.endsWith('.cpp') ? [{ fullPath: optionValue, fileName: path.basename(optionValue, '.cpp') }] : []; + // Single file + return optionValue.endsWith('.cpp') ? [{ name: path.basename(optionValue, '.cpp'), files: [optionValue] }] : []; } } From 1b11735c86e5e8e7bb3a82ac089eae5eee761a94 Mon Sep 17 00:00:00 2001 From: prc Date: Thu, 17 Sep 2020 01:58:37 +0100 Subject: [PATCH 2/2] updated compile command unit tests and added a new test for subfolder with multiple sources --- tests/cli-commands/compile-tests.js | 40 +++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/tests/cli-commands/compile-tests.js b/tests/cli-commands/compile-tests.js index 6189abf..54ea32e 100644 --- a/tests/cli-commands/compile-tests.js +++ b/tests/cli-commands/compile-tests.js @@ -59,7 +59,7 @@ describe('Compile Command', function () { '../../cli-commands/commands/compile/index', { ...stubBaseCommand(() => { - return { path: [{ fileName: 'test', fullPath: './test.cpp' }] } + return { path: [{ name: 'test', files: ['./test.cpp'] }] } }), ...stubAsyncSoftExec(checkPoints), '../../helpers/file-system-util': { @@ -146,7 +146,7 @@ describe('Compile Command', function () { describe('Options', function () { describe('Path', function () { - function stubFileSystemUtils (isFolder) { + function stubFileSystemUtils (isFolder, subFolder = false) { const Option = proxyquire( '../../cli-commands/commands/compile/options/path-option', { @@ -155,7 +155,15 @@ describe('Compile Command', function () { return isFolder; }, recursivelyReadDir: () => { - return [ + return subFolder ? [ + { + fileName: 'test1.cpp', + fullPath: `./custom/test/test1.cpp` + }, { + fileName: 'test2.cpp', + fullPath: `./custom/test/test2.cpp` + } + ] : [ { fileName: 'test1.cpp', fullPath: `./custom/test1.cpp` @@ -163,7 +171,7 @@ describe('Compile Command', function () { fileName: 'test2.cpp', fullPath: `./custom/test2.cpp` } - ] + ]; } } } @@ -177,19 +185,29 @@ describe('Compile Command', function () { const result = await pathOption.process('./test.cpp'); assert(result.length == 1); - assert(result[0].fileName == 'test'); - assert(result[0].fullPath == './test.cpp'); + assert(result[0].name == 'test'); + assert(result[0].files[0] == './test.cpp'); }); it('Should return contracts paths from a folder', async () => { const pathOption = stubFileSystemUtils(true); - const result = await pathOption.process('./test.cpp'); + const result = await pathOption.process('./custom'); assert(result.length == 2); - assert(result[0].fileName == 'test1'); - assert(result[1].fileName == 'test2'); - assert(result[0].fullPath == './custom/test1.cpp'); - assert(result[1].fullPath == './custom/test2.cpp'); + assert(result[0].name == 'test1'); + assert(result[1].name == 'test2'); + assert(result[0].files[0] == './custom/test1.cpp'); + assert(result[1].files[0] == './custom/test2.cpp'); + }); + + it('Should group contracts paths in a subfolder', async () => { + const pathOption = stubFileSystemUtils(true, true); + const result = await pathOption.process('./custom'); + + assert(result.length == 1); + assert(result[0].name == 'test'); + assert(result[0].files[0] == './custom/test/test1.cpp'); + assert(result[0].files[1] == './custom/test/test2.cpp'); }); it('Should return empty array if any contracts was found', async () => {