Skip to content
Open
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
11 changes: 6 additions & 5 deletions cli-commands/commands/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
}

Expand Down
24 changes: 17 additions & 7 deletions cli-commands/commands/compile/options/path-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] }] : [];
}
}

Expand Down
40 changes: 29 additions & 11 deletions tests/cli-commands/compile-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand Down Expand Up @@ -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',
{
Expand All @@ -155,15 +155,23 @@ 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`
}, {
fileName: 'test2.cpp',
fullPath: `./custom/test2.cpp`
}
]
];
}
}
}
Expand All @@ -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 () => {
Expand Down