Skip to content

Commit

Permalink
fix: typegen reporting the correct versions (#2888)
Browse files Browse the repository at this point in the history
* fix: typegen reporting the correct versions

* chore: added changeset

* chore: reworking version passing

* chore: lint

* chore: fixing test

* chore: lint

---------

Co-authored-by: Sérgio Torres <30977845+Torres-ssf@users.noreply.github.com>
  • Loading branch information
petertonysmith94 and Torres-ssf authored Aug 12, 2024
1 parent d875416 commit 29c556d
Show file tree
Hide file tree
Showing 34 changed files with 266 additions and 90 deletions.
7 changes: 7 additions & 0 deletions .changeset/good-knives-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@fuel-ts/abi-typegen": patch
"@fuel-ts/versions": patch
"fuels": patch
---

fix: typegen reporting the correct versions
15 changes: 9 additions & 6 deletions packages/abi-typegen/src/AbiTypeGen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import type { BinaryVersions } from '@fuel-ts/versions';

import { Abi } from './abi/Abi';
import { ProgramTypeEnum } from './types/enums/ProgramTypeEnum';
Expand All @@ -17,23 +18,25 @@ export class AbiTypeGen {
public readonly binFiles: IFile[];
public readonly storageSlotsFiles: IFile[];
public readonly outputDir: string;

public readonly files: IFile[];
public readonly versions: BinaryVersions;

constructor(params: {
abiFiles: IFile[];
binFiles: IFile[];
storageSlotsFiles: IFile[];
outputDir: string;
programType: ProgramTypeEnum;
versions: BinaryVersions;
}) {
const { abiFiles, binFiles, outputDir, programType, storageSlotsFiles } = params;
const { abiFiles, binFiles, outputDir, programType, storageSlotsFiles, versions } = params;

this.outputDir = outputDir;

this.abiFiles = abiFiles;
this.binFiles = binFiles;
this.storageSlotsFiles = storageSlotsFiles;
this.versions = versions;

// Creates a `Abi` for each abi file
this.abis = this.abiFiles.map((abiFile) => {
Expand Down Expand Up @@ -71,16 +74,16 @@ export class AbiTypeGen {
}

private getAssembledFiles(params: { programType: ProgramTypeEnum }): IFile[] {
const { abis, outputDir } = this;
const { abis, outputDir, versions } = this;
const { programType } = params;

switch (programType) {
case ProgramTypeEnum.CONTRACT:
return assembleContracts({ abis, outputDir });
return assembleContracts({ abis, outputDir, versions });
case ProgramTypeEnum.SCRIPT:
return assembleScripts({ abis, outputDir });
return assembleScripts({ abis, outputDir, versions });
case ProgramTypeEnum.PREDICATE:
return assemblePredicates({ abis, outputDir });
return assemblePredicates({ abis, outputDir, versions });
default:
throw new FuelError(
ErrorCode.INVALID_INPUT_PARAMETERS,
Expand Down
4 changes: 2 additions & 2 deletions packages/abi-typegen/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { versions } from '@fuel-ts/versions';
import { versions as builtinVersion } from '@fuel-ts/versions';
import { Command, Option } from 'commander';

import { runTypegen } from './runTypegen';
Expand Down Expand Up @@ -84,7 +84,7 @@ export function run(params: { argv: string[]; programName: string }) {
const { argv, programName } = params;

program.name(programName);
program.version(versions.FUELS);
program.version(builtinVersion.FUELS);
program.usage(`-i ../out/*-abi.json -o ./generated/`);

configureCliOptions(program);
Expand Down
4 changes: 4 additions & 0 deletions packages/abi-typegen/src/runTypegen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { versions as builtinVersions, type BinaryVersions } from '@fuel-ts/versions';
import { readFileSync, writeFileSync } from 'fs';
import { globSync } from 'glob';
import mkdirp from 'mkdirp';
Expand All @@ -18,10 +19,12 @@ export interface IGenerateFilesParams {
output: string;
silent?: boolean;
programType: ProgramTypeEnum;
versions?: BinaryVersions;
}

export function runTypegen(params: IGenerateFilesParams) {
const { cwd, inputs, output, silent, programType, filepaths: inputFilepaths } = params;
const versions: BinaryVersions = { FUELS: builtinVersions.FUELS, ...params.versions };

const cwdBasename = basename(cwd);

Expand Down Expand Up @@ -76,6 +79,7 @@ export function runTypegen(params: IGenerateFilesParams) {
binFiles,
storageSlotsFiles,
programType,
versions,
});

/*
Expand Down
4 changes: 4 additions & 0 deletions packages/abi-typegen/src/templates/common/_header.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

/*
Fuels version: {{FUELS}}
{{#if FORC}}
Forc version: {{FORC}}
{{/if}}
{{#if FUEL_CORE}}
Fuel-Core version: {{FUEL_CORE}}
{{/if}}
*/
2 changes: 1 addition & 1 deletion packages/abi-typegen/src/templates/common/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('templates/common', () => {
const { versions, restore } = mockVersions();

// executing
const rendered = renderCommonTemplate();
const rendered = renderCommonTemplate({ versions });

// validating
restore();
Expand Down
7 changes: 5 additions & 2 deletions packages/abi-typegen/src/templates/common/common.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { BinaryVersions } from '@fuel-ts/versions';

import { renderHbsTemplate } from '../renderHbsTemplate';

import commonTemplate from './common.hbs';

export function renderCommonTemplate() {
const text = renderHbsTemplate({ template: commonTemplate });
export function renderCommonTemplate(params: { versions: BinaryVersions }) {
const { versions } = params;
const text = renderHbsTemplate({ template: commonTemplate, versions });
return text;
}
4 changes: 2 additions & 2 deletions packages/abi-typegen/src/templates/common/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { renderIndexTemplate } from './index';
describe('templates/index', () => {
test('should render index template for contracts', () => {
// mocking
const { restore } = mockVersions();
const { versions, restore } = mockVersions();

// executing
const files = [{ path: './Contract.ts' }, { path: './ContractFactory.ts' }];

const rendered = renderIndexTemplate({ files });
const rendered = renderIndexTemplate({ files, versions });

// validating
restore();
Expand Down
10 changes: 8 additions & 2 deletions packages/abi-typegen/src/templates/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import type { BinaryVersions } from '@fuel-ts/versions';

import type { IFile } from '../../types/interfaces/IFile';
import { renderHbsTemplate } from '../renderHbsTemplate';

import indexTemplate from './index.hbs';

export function renderIndexTemplate(params: { files: Pick<IFile, 'path'>[] }) {
const { files } = params;
export function renderIndexTemplate(params: {
files: Pick<IFile, 'path'>[];
versions: BinaryVersions;
}) {
const { files, versions } = params;

const members = files.map((f) => f.path.match(/([^/]+)\.ts$/m)?.[1]);

const text = renderHbsTemplate({
template: indexTemplate,
versions,
data: {
members,
},
Expand Down
4 changes: 2 additions & 2 deletions packages/abi-typegen/src/templates/contract/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { renderFactoryTemplate } from './factory';
describe('templates/factory', () => {
test('should render factory template', () => {
// mocking
const { restore } = mockVersions();
const { versions, restore } = mockVersions();

// executing
const project = getTypegenForcProject(AbiTypegenProjectsEnum.MINIMAL);
Expand All @@ -36,7 +36,7 @@ describe('templates/factory', () => {
programType: ProgramTypeEnum.CONTRACT,
});

let rendered = renderFactoryTemplate({ abi });
let rendered = renderFactoryTemplate({ abi, versions });

rendered = autoUpdateFixture(
join(__dirname, '../../../test/fixtures/templates/contract/factory.hbs'),
Expand Down
7 changes: 5 additions & 2 deletions packages/abi-typegen/src/templates/contract/factory.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
import { compressBytecode } from '@fuel-ts/utils';
import type { BinaryVersions } from '@fuel-ts/versions';

import type { Abi } from '../../abi/Abi';
import { renderHbsTemplate } from '../renderHbsTemplate';

import factoryTemplate from './factory.hbs';

export function renderFactoryTemplate(params: { abi: Abi }) {
export function renderFactoryTemplate(params: { abi: Abi; versions: BinaryVersions }) {
const { versions, abi } = params;
const {
camelizedName,
capitalizedName,
rawContents,
storageSlotsContents,
hexlifiedBinContents: hexlifiedBinString,
} = params.abi;
} = abi;

const abiJsonString = JSON.stringify(rawContents, null, 2);
const storageSlotsJsonString = storageSlotsContents ?? '[]';

const text = renderHbsTemplate({
template: factoryTemplate,
versions,
data: {
camelizedName,
capitalizedName,
Expand Down
25 changes: 12 additions & 13 deletions packages/abi-typegen/src/templates/contract/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { renderMainTemplate } from './main';
describe('templates/dts', () => {
test('should render main template', () => {
// mocking
const { restore } = mockVersions();
const { versions } = mockVersions();

// executing
const project = getTypegenForcProject(AbiTypegenProjectsEnum.FULL);
Expand All @@ -32,21 +32,18 @@ describe('templates/dts', () => {
programType: ProgramTypeEnum.CONTRACT,
});

let rendered = renderMainTemplate({ abi });
let rendered = renderMainTemplate({ abi, versions });

rendered = autoUpdateFixture(
join(__dirname, '../../../test/fixtures/templates/contract/main.hbs'),
rendered
);

// validating
restore();

expect(rendered).toEqual(expectedMainFullTemplate);
});

test('should render main template with configurable', () => {
const { restore } = mockVersions();
const { versions } = mockVersions();

const project = getTypegenForcProject(AbiTypegenProjectsEnum.MINIMAL_WITH_CONFIGURABLE);

Expand All @@ -59,19 +56,18 @@ describe('templates/dts', () => {
programType: ProgramTypeEnum.CONTRACT,
});

let rendered = renderMainTemplate({ abi });
let rendered = renderMainTemplate({ abi, versions });

rendered = autoUpdateFixture(
join(__dirname, '../../../test/fixtures/templates/contract-with-configurable/main.hbs'),
rendered
);

restore();

expect(rendered).toEqual(expectedMainMinimalConfigurableTemplate);
});

test('should render main template w/ custom common types', () => {
const { versions } = mockVersions();
const project = getTypegenForcProject(AbiTypegenProjectsEnum.VECTOR_SIMPLE);
const { abiContents: rawContents } = project;

Expand All @@ -82,12 +78,13 @@ describe('templates/dts', () => {
programType: ProgramTypeEnum.CONTRACT,
});

const rendered = renderMainTemplate({ abi });
const rendered = renderMainTemplate({ abi, versions });

expect(rendered).toMatch(/^import type.+from ".\/common";$/m);
});

test('should render dts cross-referencing for identical structs', () => {
const { versions } = mockVersions();
const project = getTypegenForcProject(AbiTypegenProjectsEnum.STRUCT_SIMPLE);
const { abiContents: rawContents } = project;

Expand All @@ -98,12 +95,13 @@ describe('templates/dts', () => {
programType: ProgramTypeEnum.CONTRACT,
});

const rendered = renderMainTemplate({ abi });
const rendered = renderMainTemplate({ abi, versions });

expect(rendered).toMatch(/export type StructBOutput<T> = StructBInput<T>;$/m);
});

test('should render dts cross-referencing for identical enums', () => {
const { versions } = mockVersions();
const project = getTypegenForcProject(AbiTypegenProjectsEnum.ENUM_SIMPLE);
const { abiContents: rawContents } = project;

Expand All @@ -114,12 +112,13 @@ describe('templates/dts', () => {
programType: ProgramTypeEnum.CONTRACT,
});

const rendered = renderMainTemplate({ abi });
const rendered = renderMainTemplate({ abi, versions });

expect(rendered).toMatch(/export type MyEnumOutput = MyEnumInput;$/m);
});

test('should not render same value for native identical enums', () => {
const { versions } = mockVersions();
const project = getTypegenForcProject(AbiTypegenProjectsEnum.ENUM_SIMPLE_NATIVE);
const { abiContents: rawContents } = project;

Expand All @@ -130,7 +129,7 @@ describe('templates/dts', () => {
programType: ProgramTypeEnum.CONTRACT,
});

const rendered = renderMainTemplate({ abi });
const rendered = renderMainTemplate({ abi, versions });

expect(rendered).toMatch(
/export enum MyEnumOutput { Checked = 'Checked', Pending = 'Pending' };$/m
Expand Down
9 changes: 6 additions & 3 deletions packages/abi-typegen/src/templates/contract/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { BinaryVersions } from '@fuel-ts/versions';

import type { Abi } from '../../abi/Abi';
import { renderHbsTemplate } from '../renderHbsTemplate';
import { formatEnums } from '../utils/formatEnums';
Expand All @@ -6,9 +8,9 @@ import { formatStructs } from '../utils/formatStructs';

import mainTemplate from './main.hbs';

export function renderMainTemplate(params: { abi: Abi }) {
const { camelizedName, capitalizedName, types, functions, commonTypesInUse, configurables } =
params.abi;
export function renderMainTemplate(params: { abi: Abi; versions: BinaryVersions }) {
const { versions, abi } = params;
const { camelizedName, capitalizedName, types, functions, commonTypesInUse, configurables } = abi;

/*
First we format all attributes
Expand Down Expand Up @@ -42,6 +44,7 @@ export function renderMainTemplate(params: { abi: Abi }) {
*/
const text = renderHbsTemplate({
template: mainTemplate,
versions,
data: {
camelizedName,
capitalizedName,
Expand Down
Loading

0 comments on commit 29c556d

Please sign in to comment.