From 0855738a9eb05cf4023f2f51058733533ae79fa2 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Fri, 12 Jul 2024 14:41:28 +0100 Subject: [PATCH] move tablegen off of old config --- .../render-solidity/renderEnums.test.ts | 25 +++++++++++++++ .../codegen/render-solidity/renderEnums.ts | 31 ++++++++++--------- packages/store/test/codegen/common.sol | 1 + .../store/ts/codegen/renderTypesFromConfig.ts | 11 ++----- packages/store/ts/codegen/tablegen.ts | 13 +++++--- 5 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 packages/common/src/codegen/render-solidity/renderEnums.test.ts diff --git a/packages/common/src/codegen/render-solidity/renderEnums.test.ts b/packages/common/src/codegen/render-solidity/renderEnums.test.ts new file mode 100644 index 0000000000..e9a41c4472 --- /dev/null +++ b/packages/common/src/codegen/render-solidity/renderEnums.test.ts @@ -0,0 +1,25 @@ +import { describe, expect, it } from "vitest"; +import { renderEnums } from "./renderEnums"; + +describe("renderEnums", () => { + it("renders a Solidity file with enums", () => { + expect( + renderEnums({ + Direction: ["left", "up", "right", "down"], + }), + ).toMatchInlineSnapshot(` + " + // SPDX-License-Identifier: MIT + pragma solidity >=0.8.24; + + /* Autogenerated file. Do not edit manually. */ + + + enum Direction { + left, up, right, down + } + + " + `); + }); +}); diff --git a/packages/common/src/codegen/render-solidity/renderEnums.ts b/packages/common/src/codegen/render-solidity/renderEnums.ts index 74d456d81a..6eec0df37c 100644 --- a/packages/common/src/codegen/render-solidity/renderEnums.ts +++ b/packages/common/src/codegen/render-solidity/renderEnums.ts @@ -1,20 +1,23 @@ -import { renderArguments, renderList, renderedSolidityHeader } from "./common"; -import { RenderEnum } from "./types"; +import { renderedSolidityHeader } from "./common"; + +// importing this from config or store would be a cyclic dependency :( +type Enums = { + readonly [name: string]: readonly [string, ...string[]]; +}; /** * Render a list of enum data as solidity enum definitions */ -export function renderEnums(enums: RenderEnum[]): string { - let result = renderedSolidityHeader; - - result += renderList( - enums, - ({ name, memberNames }) => ` - enum ${name} { - ${renderArguments(memberNames)} - } - `, - ); +export function renderEnums(enums: Enums): string { + return ` + ${renderedSolidityHeader} - return result; + ${Object.entries(enums).map( + ([name, values]) => ` + enum ${name} { + ${values.join(", ")} + } + `, + )} + `; } diff --git a/packages/store/test/codegen/common.sol b/packages/store/test/codegen/common.sol index 88e23a3c6d..3479f63bab 100644 --- a/packages/store/test/codegen/common.sol +++ b/packages/store/test/codegen/common.sol @@ -2,6 +2,7 @@ pragma solidity >=0.8.24; /* Autogenerated file. Do not edit manually. */ + enum ExampleEnum { None, First, diff --git a/packages/store/ts/codegen/renderTypesFromConfig.ts b/packages/store/ts/codegen/renderTypesFromConfig.ts index 94d660a5ab..982ce0f1e5 100644 --- a/packages/store/ts/codegen/renderTypesFromConfig.ts +++ b/packages/store/ts/codegen/renderTypesFromConfig.ts @@ -1,14 +1,9 @@ import { renderEnums } from "@latticexyz/common/codegen"; -import { StoreConfig } from "../config"; +import { Store } from "../config/v2"; /** * Renders Solidity code for enums defined in the provided config */ -export function renderTypesFromConfig(config: StoreConfig) { - const enums = Object.keys(config.enums).map((name) => ({ - name, - memberNames: config.enums[name], - })); - - return renderEnums(enums); +export function renderTypesFromConfig(config: Store) { + return renderEnums(config.enums); } diff --git a/packages/store/ts/codegen/tablegen.ts b/packages/store/ts/codegen/tablegen.ts index fe547032c3..bcce73ab7d 100644 --- a/packages/store/ts/codegen/tablegen.ts +++ b/packages/store/ts/codegen/tablegen.ts @@ -6,7 +6,7 @@ import { renderTypesFromConfig } from "./renderTypesFromConfig"; import { renderTableIndex } from "./renderTableIndex"; import { rm } from "fs/promises"; import { Store as StoreConfig } from "../config/v2/output"; -import { storeToV1 } from "../config/v2/compat"; +import { mapObject } from "@latticexyz/common/utils"; export type TablegenOptions = { /** @@ -19,8 +19,11 @@ export type TablegenOptions = { export async function tablegen({ rootDir, config, remappings }: TablegenOptions) { const outputDirectory = path.join(rootDir, config.sourceDirectory, config.codegen.outputDirectory); - const configV1 = storeToV1(config); - const solidityUserTypes = loadAndExtractUserTypes(configV1.userTypes, outputDirectory, remappings); + const solidityUserTypes = loadAndExtractUserTypes( + mapObject(config.userTypes, (type) => ({ ...type, internalType: type.type })), + outputDirectory, + remappings, + ); const allTableOptions = getTableOptions(config, solidityUserTypes); const uniqueTableDirectories = Array.from(new Set(allTableOptions.map(({ outputPath }) => path.dirname(outputPath)))); @@ -47,9 +50,9 @@ export async function tablegen({ rootDir, config, remappings }: TablegenOptions) } // write types to file - if (Object.keys(configV1.enums).length > 0) { + if (Object.keys(config.enums).length > 0) { const fullOutputPath = path.join(outputDirectory, config.codegen.userTypesFilename); - const output = renderTypesFromConfig(configV1); + const output = renderTypesFromConfig(config); await formatAndWriteSolidity(output, fullOutputPath, "Generated types file"); } }