Skip to content

Commit

Permalink
improve structure
Browse files Browse the repository at this point in the history
  • Loading branch information
nedsalk committed Oct 4, 2024
1 parent f1e6544 commit cdc480a
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 56 deletions.
19 changes: 10 additions & 9 deletions packages/abi/src/gen/renderers/ts/render-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,25 @@ export const renderTs: Renderer = (details: ProgramDetails[]): AbiGenResult[] =>
.flat();

results.push({
filename: 'common',
extension: 'ts',
filename: 'common.ts',
content: renderHbsTemplate({ template: commonTemplate }),
});

results.push({
filename: 'index',
extension: 'ts',
filename: 'index.ts',
content: renderHbsTemplate({
template: indexTemplate,
data: {
members: results.filter((r) => r.exportInIndex).map((r) => r.filename),
members: results
.filter((r) => r.exportInIndexFile)
.map(
(r) =>
// remove .ts extension
r.filename.split('.')[0]
),
},
}),
});

return results.map((r) => ({
content: r.content,
filename: `${r.filename}.${r.extension}`,
}));
return results;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { assertUnreachable } from '@fuel-ts/utils';

import type { Abi } from '../../../../parser';

export function getParentDirWrapper(programType: Abi['programType']): (file: string) => string {
let directory: string = '';
switch (programType) {
case 'contract':
directory = 'contracts';
break;
case 'predicate':
directory = 'predicates';
break;
case 'script':
directory = 'scripts';
break;
case 'library':
break;
default:
assertUnreachable(programType);
}

return (file) => `${directory}/${file}`;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { assertUnreachable } from '@fuel-ts/utils';

import type { ProgramDetails } from '../../../utils/get-program-details';
import type { TsAbiGenResult } from '../../types';
import bytecodeTemplate from '../templates/bytecode.hbs';
Expand All @@ -6,20 +8,19 @@ import contractTemplate from '../templates/contract.hbs';
import predicateTemplate from '../templates/predicate.hbs';
import scriptTemplate from '../templates/script.hbs';

import { getParentDirWrapper } from './get-parent-dir-wrapper';
import { renderHbsTemplate } from './render-hbs-template';

export function renderProgramTemplates(programDetails: ProgramDetails): TsAbiGenResult[] {
const { abi, binCompressed, name } = programDetails;

const results: TsAbiGenResult[] = [
{
filename: `${name}-abi`,
extension: 'json',
filename: `${name}-abi.json`,
content: programDetails.abiContents,
},
{
filename: `${name}-bytecode`,
extension: 'ts',
filename: `${name}-bytecode.ts`,
content: renderHbsTemplate({ template: bytecodeTemplate, data: { binCompressed } }),
},
];
Expand All @@ -28,55 +29,56 @@ export function renderProgramTemplates(programDetails: ProgramDetails): TsAbiGen
case 'contract':
results.push(
{
filename: name,
extension: 'ts',
filename: `${name}.ts`,
content: renderHbsTemplate({
template: contractTemplate,
data: { name },
}),
exportInIndex: true,
exportInIndexFile: true,
},
{
filename: `${name}Factory`,
extension: 'ts',
filename: `${name}Factory.ts`,
content: renderHbsTemplate({
template: contractFactoryTemplate,
data: { name },
}),
exportInIndex: true,
exportInIndexFile: true,
},
{
filename: `${name}-storage-slots`,
extension: 'json',
filename: `${name}-storage-slots.json`,
content: programDetails.storageSlots as string,
}
);
break;
case 'predicate':
results.push({
filename: name,
extension: 'ts',
filename: `${name}.ts`,
content: renderHbsTemplate({
template: predicateTemplate,
data: { name },
}),
exportInIndex: true,
exportInIndexFile: true,
});
break;
case 'script':
results.push({
filename: name,
extension: 'ts',
filename: `${name}.ts`,
content: renderHbsTemplate({
template: scriptTemplate,
data: { name },
}),
exportInIndex: true,
exportInIndexFile: true,
});
break;
case 'library':
// we do nothing for library
break;
default:
assertUnreachable(abi.programType);
break;
}

return results;
const withParentDir = getParentDirWrapper(abi.programType);

return results.map((r) => ({ ...r, filename: withParentDir(r.filename) }));
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import { generateTsType } from '../typers/generate-ts-type';
import { flattenImports } from '../typers/helpers';
import type { TyperReturn } from '../typers/types';

import { getParentDirWrapper } from './get-parent-dir-wrapper';
import { renderHbsTemplate } from './render-hbs-template';

const metadataTypeFilter = createMatcher<boolean>({
enum: true,
struct: true,
assetId: true,
string: false,
void: false,
bool: false,
Expand All @@ -23,15 +27,12 @@ const metadataTypeFilter = createMatcher<boolean>({
stdString: false,
option: false,
result: false,
enum: true,
struct: true,
str: false,
b512: false,
bytes: false,
vector: false,
tuple: false,
array: false,
assetId: true,
evmAddress: false,
rawUntypedPtr: false,
rawUntypedSlice: false,
Expand Down Expand Up @@ -120,9 +121,10 @@ export function renderTypesTemplate({ name, abi }: ProgramDetails): TsAbiGenResu
},
});

const withParentDir = getParentDirWrapper(abi.programType);

return {
filename: `${name}Types`,
extension: 'ts',
filename: withParentDir(`${name}Types.ts`),
content,
};
}
2 changes: 1 addition & 1 deletion packages/abi/src/gen/renderers/ts/templates/index.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{header}}

{{#each members}}
export { {{this}} } from './{{this}}';
export * from './{{this}}';
{{/each}}
2 changes: 1 addition & 1 deletion packages/abi/src/gen/renderers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import type { ProgramDetails } from '../utils/get-program-details';

export type Renderer = (details: ProgramDetails[]) => AbiGenResult[];

export type TsAbiGenResult = AbiGenResult & { exportInIndex?: boolean; extension: 'ts' | 'json' };
export type TsAbiGenResult = AbiGenResult & { exportInIndexFile?: boolean };
2 changes: 1 addition & 1 deletion packages/abi/src/parser/abi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface Abi {
specVersion: string;
encodingVersion: string;
programType: string;
programType: 'contract' | 'script' | 'predicate' | 'library';
metadataTypes: AbiTypeMetadata[];
types: AbiType[];
functions: AbiFunction[];
Expand Down
2 changes: 1 addition & 1 deletion packages/abi/src/parser/specifications/v1/specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
export interface AbiSpecificationV1 {
readonly specVersion: '1';
readonly encodingVersion: string;
readonly programType: string;
readonly programType: 'contract' | 'script' | 'predicate' | 'library';
readonly concreteTypes: readonly AbiConcreteTypeV1[];
readonly metadataTypes: readonly AbiMetadataTypeV1[];
readonly functions: readonly AbiFunctionV1[];
Expand Down
36 changes: 21 additions & 15 deletions packages/fuel-gauge/src/abi/abi-gen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,33 @@ import { AbiProjectsEnum, getAbiForcProject } from './utils';
* @group node
*/
describe('AbiGen', () => {
test('contract', () => {
test('AbiGen generates all files correctly', () => {
const fixtureResultMap = new Map([
['common', 'common.ts'],
['contract', 'AbiContract.ts'],
['contract-types', 'AbiContractTypes.ts'],
['contract-factory', 'AbiContractFactory.ts'],
['contract-bytecode', 'AbiContract-bytecode.ts'],
['contract-abi', 'AbiContract-abi.json'],
['contract-storage-slots', 'AbiContract-storage-slots.json'],
['predicate', 'AbiPredicate.ts'],
['predicate-types', 'AbiPredicateTypes.ts'],
['predicate-abi', 'AbiPredicate-abi.json'],
['script', 'AbiScript.ts'],
['script-types', 'AbiScriptTypes.ts'],
['script-abi', 'AbiScript-abi.json'],
['index', 'index.ts'],
['common', 'common.ts'],

['contract', 'contracts/AbiContract.ts'],
['contract-types', 'contracts/AbiContractTypes.ts'],
['contract-factory', 'contracts/AbiContractFactory.ts'],
['contract-bytecode', 'contracts/AbiContract-bytecode.ts'],
['contract-abi', 'contracts/AbiContract-abi.json'],
['contract-storage-slots', 'contracts/AbiContract-storage-slots.json'],

['predicate', 'predicates/AbiPredicate.ts'],
['predicate-types', 'predicates/AbiPredicateTypes.ts'],
['predicate-abi', 'predicates/AbiPredicate-abi.json'],
['script', 'scripts/AbiScript.ts'],

['script-types', 'scripts/AbiScriptTypes.ts'],
['script-abi', 'scripts/AbiScript-abi.json'],
]);

const { buildDir: contractDir } = getAbiForcProject(AbiProjectsEnum.ABI_CONTRACT);
const { buildDir: predicateDir } = getAbiForcProject(AbiProjectsEnum.ABI_PREDICATE);
const { buildDir: scriptDir } = getAbiForcProject(AbiProjectsEnum.ABI_SCRIPT);

const programDetails = getProgramDetails([contractDir, predicateDir, scriptDir]);
const { results } = new AbiGen({ programDetails });
const results = AbiGen.generate({ programDetails });

fixtureResultMap.forEach((filename, fixture) => {
const fixtureFile = join(
Expand All @@ -42,6 +45,9 @@ describe('AbiGen', () => {

const result = results.find((r) => r.filename === filename);
expect(result?.content).toEqual(fixtureContent);

// verify only one file generated
expect(results.filter((r) => r.filename === filename)).toHaveLength(1);
});
});
});
8 changes: 4 additions & 4 deletions packages/fuel-gauge/src/abi/fixtures/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/


export { AbiContract } from './AbiContract';
export { AbiContractFactory } from './AbiContractFactory';
export { AbiPredicate } from './AbiPredicate';
export { AbiScript } from './AbiScript';
export * from './contracts/AbiContract';
export * from './contracts/AbiContractFactory';
export * from './predicates/AbiPredicate';
export * from './scripts/AbiScript';

0 comments on commit cdc480a

Please sign in to comment.