Skip to content

Commit

Permalink
fixes, externalize loadAndExtractUserTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
dk1a committed Sep 22, 2023
1 parent 64b947a commit 9675b0c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 38 deletions.
9 changes: 7 additions & 2 deletions packages/cli/src/utils/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chalk from "chalk";
import path from "path";
import { ethers } from "ethers";
import { getOutDirectory, cast } from "@latticexyz/common/foundry";
import { getOutDirectory, cast, getSrcDirectory } from "@latticexyz/common/foundry";
import { StoreConfig } from "@latticexyz/store";
import { WorldConfig, resolveWorldConfig } from "@latticexyz/world";
import { deployWorldContract } from "./world";
Expand Down Expand Up @@ -49,6 +50,7 @@ export async function deploy(
deployConfig;
const resolvedConfig = resolveWorldConfig(mudConfig, existingContractNames);
const forgeOutDirectory = await getOutDirectory(profile);
const outputBaseDirectory = path.join(await getSrcDirectory(profile), mudConfig.codegenDirectory);

// Set up signer for deployment
const provider = new ethers.providers.StaticJsonRpcProvider(rpc);
Expand Down Expand Up @@ -154,7 +156,10 @@ export async function deploy(
}

const tableIds = getTableIds(mudConfig);
const registerTableCalls = Object.values(mudConfig.tables).map((table) => getRegisterTableCallData(table, mudConfig));

const registerTableCalls = Object.values(mudConfig.tables).map((table) =>
getRegisterTableCallData(table, mudConfig, outputBaseDirectory)
);

console.log(chalk.blue("Registering tables"));
await Promise.all(
Expand Down
13 changes: 10 additions & 3 deletions packages/cli/src/utils/tables/getRegisterTableCallData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ import { resourceIdToHex } from "@latticexyz/common";
import { Table } from "./types";
import { fieldLayoutToHex } from "@latticexyz/protocol-parser";
import { CallData } from "../utils/types";
import { loadAndExtractUserTypes } from "@latticexyz/common/codegen";

export function getRegisterTableCallData(table: Table, storeConfig: StoreConfig): CallData {
export function getRegisterTableCallData(
table: Table,
storeConfig: StoreConfig,
outputBaseDirectory: string
): CallData {
const { name, valueSchema, keySchema } = table;
if (!name) throw Error("Table missing name");

const solidityUserTypes = loadAndExtractUserTypes(storeConfig.userTypes, outputBaseDirectory);

const schemaTypes = Object.values(valueSchema).map((abiOrUserType) => {
const { schemaType } = resolveAbiOrUserType(abiOrUserType, storeConfig);
const { schemaType } = resolveAbiOrUserType(abiOrUserType, storeConfig, solidityUserTypes);
return schemaType;
});

Expand All @@ -22,7 +29,7 @@ export function getRegisterTableCallData(table: Table, storeConfig: StoreConfig)
};

const keyTypes = Object.values(keySchema).map((abiOrUserType) => {
const { schemaType } = resolveAbiOrUserType(abiOrUserType, storeConfig);
const { schemaType } = resolveAbiOrUserType(abiOrUserType, storeConfig, solidityUserTypes);
return schemaType;
});

Expand Down
1 change: 1 addition & 0 deletions packages/common/src/codegen/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from "./contractToInterface";
export * from "./extractUserTypes";
export * from "./format";
export * from "./formatAndWrite";
export * from "./loadUserTypesFile";
export * from "./posixPath";
43 changes: 43 additions & 0 deletions packages/common/src/codegen/utils/loadUserTypesFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { readFileSync } from "fs";
import path from "path";
import { SolidityUserDefinedType, extractUserTypes } from "./extractUserTypes";

export function loadAndExtractUserTypes(
userTypes: Record<string, string>,
outputBaseDirectory: string
): Record<string, SolidityUserDefinedType> {
const userTypesPerFile: Record<string, string[]> = {};
for (const [userTypeName, unresolvedFilePath] of Object.entries(userTypes)) {
if (!(unresolvedFilePath in userTypesPerFile)) {
userTypesPerFile[unresolvedFilePath] = [];
}
userTypesPerFile[unresolvedFilePath].push(userTypeName);
}
let extractedUserTypes: Record<string, SolidityUserDefinedType> = {};
for (const [unresolvedFilePath, userTypeNames] of Object.entries(userTypesPerFile)) {
const { filePath, data } = loadUserTypesFile(outputBaseDirectory, unresolvedFilePath);
extractedUserTypes = Object.assign(userTypes, extractUserTypes(data, userTypeNames, filePath));
}
return extractedUserTypes;
}

function loadUserTypesFile(
outputBaseDirectory: string,
unresolvedFilePath: string
): {
filePath: string;
data: string;
} {
if (unresolvedFilePath.indexOf(".") === 0) {
return {
filePath: path.relative(outputBaseDirectory, unresolvedFilePath),
data: readFileSync(unresolvedFilePath, "utf8"),
};
} else {
// TODO support remappings
return {
filePath: unresolvedFilePath,
data: readFileSync(path.join("node_modules", unresolvedFilePath), "utf8"),
};
}
}
35 changes: 2 additions & 33 deletions packages/store/ts/codegen/tablegen.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import path from "path";
import { SolidityUserDefinedType, extractUserTypes, formatAndWriteSolidity } from "@latticexyz/common/codegen";
import { formatAndWriteSolidity, loadAndExtractUserTypes } from "@latticexyz/common/codegen";
import { getTableOptions } from "./tableOptions";
import { renderTable } from "./renderTable";
import { renderTypesFromConfig } from "./renderTypesFromConfig";
import { renderTableIndex } from "./renderTableIndex";
import { readFileSync, rmSync } from "fs";
import { rmSync } from "fs";
import { StoreConfig } from "../config";

export async function tablegen(config: StoreConfig, outputBaseDirectory: string) {
Expand Down Expand Up @@ -34,34 +34,3 @@ export async function tablegen(config: StoreConfig, outputBaseDirectory: string)
const output = renderTableIndex(allTableOptions);
await formatAndWriteSolidity(output, fullOutputPath, "Generated table index");
}

function loadAndExtractUserTypes(userTypes: StoreConfig["userTypes"], outputBaseDirectory: string) {
const userTypesPerFile: Record<string, string[]> = {};
for (const [userTypeName, unresolvedFilePath] of Object.entries(userTypes)) {
if (!(unresolvedFilePath in userTypesPerFile)) {
userTypesPerFile[unresolvedFilePath] = [];
}
userTypesPerFile[unresolvedFilePath].push(userTypeName);
}
let extractedUserTypes: Record<string, SolidityUserDefinedType> = {};
for (const [unresolvedFilePath, userTypeNames] of Object.entries(userTypesPerFile)) {
const { filePath, data } = loadUserTypesFile(outputBaseDirectory, unresolvedFilePath);
extractedUserTypes = Object.assign(userTypes, extractUserTypes(data, userTypeNames, filePath));
}
return extractedUserTypes;
}

function loadUserTypesFile(outputBaseDirectory: string, unresolvedFilePath: string) {
if (unresolvedFilePath.indexOf(".") === 0) {
return {
filePath: path.relative(outputBaseDirectory, unresolvedFilePath),
data: readFileSync(unresolvedFilePath, "utf8"),
};
} else {
// TODO support remappings
return {
filePath: unresolvedFilePath,
data: readFileSync(path.join("node_modules", unresolvedFilePath), "utf8"),
};
}
}

0 comments on commit 9675b0c

Please sign in to comment.