Skip to content

Commit

Permalink
move tablegen off of old config
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Jul 12, 2024
1 parent 706986b commit 0855738
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 27 deletions.
25 changes: 25 additions & 0 deletions packages/common/src/codegen/render-solidity/renderEnums.test.ts
Original file line number Diff line number Diff line change
@@ -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
}
"
`);
});
});
31 changes: 17 additions & 14 deletions packages/common/src/codegen/render-solidity/renderEnums.ts
Original file line number Diff line number Diff line change
@@ -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(", ")}
}
`,
)}
`;
}
1 change: 1 addition & 0 deletions packages/store/test/codegen/common.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 3 additions & 8 deletions packages/store/ts/codegen/renderTypesFromConfig.ts
Original file line number Diff line number Diff line change
@@ -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);
}
13 changes: 8 additions & 5 deletions packages/store/ts/codegen/tablegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
/**
Expand All @@ -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))));
Expand All @@ -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");
}
}

0 comments on commit 0855738

Please sign in to comment.