Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
abi-gen should regenerate contract-wrappers if template files change (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
xianny authored Jun 18, 2019
1 parent 01a1b19 commit 9775f8d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
9 changes: 9 additions & 0 deletions packages/abi-gen/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
[
{
"version": "2.0.11",
"changes": [
{
"note": "Watch template files for changes",
"pr": 1875
}
]
},
{
"timestamp": 1557507213,
"version": "2.0.10",
Expand Down
17 changes: 8 additions & 9 deletions packages/abi-gen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ const args = yargs
'Full usage example',
).argv;

function registerPartials(partialsGlob: string): void {
const partialTemplateFileNames = globSync(partialsGlob);
const mainTemplate = utils.getNamedContent(args.template);
const template = Handlebars.compile<ContextData>(mainTemplate.content);
const abiFileNames = globSync(args.abis);
const partialTemplateFileNames = globSync(args.partials);

function registerPartials(): void {
logUtils.log(`Found ${chalk.green(`${partialTemplateFileNames.length}`)} ${chalk.bold('partial')} templates`);
for (const partialTemplateFileName of partialTemplateFileNames) {
const namedContent = utils.getNamedContent(partialTemplateFileName);
Expand All @@ -70,12 +74,7 @@ function registerPartials(partialsGlob: string): void {
Handlebars.registerHelper('parameterType', utils.solTypeToTsType.bind(utils, ParamKind.Input, args.backend));
Handlebars.registerHelper('assertionType', utils.solTypeToAssertion.bind(utils));
Handlebars.registerHelper('returnType', utils.solTypeToTsType.bind(utils, ParamKind.Output, args.backend));
if (args.partials) {
registerPartials(args.partials);
}
const mainTemplate = utils.getNamedContent(args.template);
const template = Handlebars.compile<ContextData>(mainTemplate.content);
const abiFileNames = globSync(args.abis);
registerPartials();

if (_.isEmpty(abiFileNames)) {
logUtils.log(`${chalk.red(`No ABI files found.`)}`);
Expand Down Expand Up @@ -109,7 +108,7 @@ for (const abiFileName of abiFileNames) {
const outFileName = utils.makeOutputFileName(namedContent.name);
const outFilePath = `${args.output}/${outFileName}.ts`;

if (utils.isOutputFileUpToDate(abiFileName, outFilePath)) {
if (utils.isOutputFileUpToDate(outFilePath, [abiFileName, args.template, ...partialTemplateFileNames])) {
logUtils.log(`Already up to date: ${chalk.bold(outFilePath)}`);
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/abi-gen/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ export const utils = {
writeOutputFile(filePath: string, renderedTsCode: string): void {
fs.writeFileSync(filePath, renderedTsCode);
},
isOutputFileUpToDate(abiFile: string, outputFile: string): boolean {
const abiFileModTimeMs = fs.statSync(abiFile).mtimeMs;
isOutputFileUpToDate(outputFile: string, sourceFiles: string[]): boolean {
const sourceFileModTimeMs = sourceFiles.map(file => fs.statSync(file).mtimeMs);
try {
const outFileModTimeMs = fs.statSync(outputFile).mtimeMs;
return outFileModTimeMs > abiFileModTimeMs;
return sourceFileModTimeMs.find(sourceMs => sourceMs > outFileModTimeMs) === undefined;
} catch (err) {
if (err.code === 'ENOENT') {
return false;
Expand Down
20 changes: 16 additions & 4 deletions packages/abi-gen/test/utils_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('writeOutputFile()', () => {

describe('isOutputFileUpToDate()', () => {
it('should throw ENOENT when there is no abi file', () => {
expect(utils.isOutputFileUpToDate.bind('nonexistant1', 'nonexistant2')).to.throw('ENOENT');
expect(utils.isOutputFileUpToDate.bind('', 'nonexistant1', ['nonexistant2'])).to.throw('ENOENT');
});

describe('when the abi input file exists', () => {
Expand All @@ -55,7 +55,7 @@ describe('isOutputFileUpToDate()', () => {

describe('without an existing output file', () => {
it('should return false', () => {
expect(utils.isOutputFileUpToDate(abiFile, 'nonexistant_file')).to.be.false();
expect(utils.isOutputFileUpToDate('nonexistant_file', [abiFile])).to.be.false();
});
});

Expand All @@ -71,15 +71,27 @@ describe('isOutputFileUpToDate()', () => {
});

it('should return true when output file is newer than abi file', async () => {
expect(utils.isOutputFileUpToDate(abiFile, outputFile)).to.be.true();
expect(utils.isOutputFileUpToDate(outputFile, [abiFile])).to.be.true();
});

it('should return false when output file exists but is older than abi file', () => {
const outFileModTimeMs = fs.statSync(outputFile).mtimeMs;
const abiFileModTimeMs = outFileModTimeMs + 1;
fs.utimesSync(abiFile, abiFileModTimeMs, abiFileModTimeMs);

expect(utils.isOutputFileUpToDate(abiFile, outputFile)).to.be.false();
expect(utils.isOutputFileUpToDate(outputFile, [abiFile])).to.be.false();
});
it('should return false when any source file is newer than output file', () => {
const templateFile = tmp.fileSync(
{ discardDescriptor: true }, // close file (set timestamp)
).name;

const templateFileModTimeMs = fs.statSync(outputFile).mtimeMs + 1;
const abiFileModTimeMs = fs.statSync(outputFile).mtimeMs;
fs.utimesSync(templateFile, templateFileModTimeMs, templateFileModTimeMs);
fs.utimesSync(abiFile, abiFileModTimeMs, abiFileModTimeMs);

expect(utils.isOutputFileUpToDate(outputFile, [abiFile, templateFile])).to.be.false();
});
});
});
Expand Down

0 comments on commit 9775f8d

Please sign in to comment.