Skip to content

Commit

Permalink
feat(store): codegen index and common files (#1318)
Browse files Browse the repository at this point in the history
Co-authored-by: alvarius <alvarius@lattice.xyz>
  • Loading branch information
holic and alvrs committed Sep 17, 2023
1 parent a0341da commit ac508bf
Show file tree
Hide file tree
Showing 59 changed files with 143 additions and 52 deletions.
29 changes: 29 additions & 0 deletions .changeset/shy-monkeys-wonder.md
@@ -0,0 +1,29 @@
---
"@latticexyz/cli": major
"@latticexyz/store": major
"create-mud": patch
---

Renamed the default filename of generated user types from `Types.sol` to `common.sol` and the default filename of the generated table index file from `Tables.sol` to `index.sol`.

Both can be overridden via the MUD config:

```ts
export default mudConfig({
/** Filename where common user types will be generated and imported from. */
userTypesFilename: "common.sol",
/** Filename where codegen index will be generated. */
codegenIndexFilename: "index.sol",
});
```

Note: `userTypesFilename` was renamed from `userTypesPath` and `.sol` is not appended automatically anymore but needs to be part of the provided filename.

To update your existing project, update all imports from `Tables.sol` to `index.sol` and all imports from `Types.sol` to `common.sol`, or override the defaults in your MUD config to the previous values.

```diff
- import { Counter } from "../src/codegen/Tables.sol";
+ import { Counter } from "../src/codegen/index.sol";
- import { ExampleEnum } from "../src/codegen/Types.sol";
+ import { ExampleEnum } from "../src/codegen/common.sol";
```
2 changes: 1 addition & 1 deletion e2e/packages/contracts/src/systems/NumberListSystem.sol
Expand Up @@ -4,7 +4,7 @@ pragma solidity >=0.8.0;
import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol";
import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol";
import { System } from "@latticexyz/world/src/System.sol";
import { NumberList, NumberListTableId } from "../codegen/Tables.sol";
import { NumberList, NumberListTableId } from "../codegen/index.sol";

contract NumberListSystem is System {
function set(uint32[] memory list) public {
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal/packages/contracts/package.json
Expand Up @@ -7,7 +7,7 @@
"build": "pnpm run build:mud && pnpm run build:abi && pnpm run build:abi-ts",
"build:abi": "forge clean && forge build --skip test script",
"build:abi-ts": "mud abi-ts && prettier --write '**/*.abi.json.d.ts'",
"build:mud": "mud tablegen && mud worldgen",
"build:mud": "rimraf src/codegen && mud tablegen && mud worldgen",
"deploy:local": "pnpm run build && mud deploy",
"deploy:testnet": "pnpm run build && mud deploy --profile=lattice-testnet",
"dev": "pnpm mud dev-contracts",
Expand Down
Expand Up @@ -6,7 +6,7 @@ import { console } from "forge-std/console.sol";
import { ResourceSelector } from "@latticexyz/world/src/ResourceSelector.sol";
import { IWorld } from "../src/codegen/world/IWorld.sol";

import { MessageTable, MessageTableTableId } from "../src/codegen/Tables.sol";
import { MessageTable, MessageTableTableId } from "../src/codegen/index.sol";
import { ChatNamespacedSystem } from "../src/systems/ChatNamespacedSystem.sol";

contract PostDeploy is Script {
Expand Down
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import { System } from "@latticexyz/world/src/System.sol";
import { MessageTable } from "../codegen/Tables.sol";
import { MessageTable } from "../codegen/index.sol";

// This system is supposed to have a different namespace, but otherwise be identical to ChatSystem
contract ChatNamespacedSystem is System {
Expand Down
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import { System } from "@latticexyz/world/src/System.sol";
import { MessageTable } from "../codegen/Tables.sol";
import { MessageTable } from "../codegen/index.sol";

contract ChatSystem is System {
function sendMessage(string memory message) public {
Expand Down
Expand Up @@ -2,7 +2,7 @@
pragma solidity >=0.8.0;
import { console } from "forge-std/console.sol";
import { System } from "@latticexyz/world/src/System.sol";
import { CounterTable } from "../codegen/Tables.sol";
import { CounterTable } from "../codegen/index.sol";

contract IncrementSystem is System {
error MyCustomError();
Expand Down
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import { System } from "@latticexyz/world/src/System.sol";
import { Inventory } from "../codegen/Tables.sol";
import { Inventory } from "../codegen/index.sol";

contract InventorySystem is System {
function pickUp(uint32 item, uint32 itemVariant) public {
Expand Down
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import { System } from "@latticexyz/world/src/System.sol";
import { CounterTable } from "../codegen/Tables.sol";
import { CounterTable } from "../codegen/index.sol";
import { BytesStruct, StringStruct } from "./structs.sol";

contract StructSystem is System {
Expand Down
Expand Up @@ -7,7 +7,7 @@ import { getKeysWithValue } from "@latticexyz/world/src/modules/keyswithvalue/ge
import { StoreCore } from "@latticexyz/store/src/StoreCore.sol";

import { IWorld } from "../src/codegen/world/IWorld.sol";
import { MessageTable, MessageTableTableId } from "../src/codegen/Tables.sol";
import { MessageTable, MessageTableTableId } from "../src/codegen/index.sol";
import { IChatNamespacedSystem } from "../src/interfaces/IChatNamespacedSystem.sol";

contract ChatNamespacedTest is MudTest {
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal/packages/contracts/test/CounterTest.t.sol
Expand Up @@ -6,7 +6,7 @@ import { MudTest } from "@latticexyz/world/test/MudTest.t.sol";
import { getKeysWithValue } from "@latticexyz/world/src/modules/keyswithvalue/getKeysWithValue.sol";

import { IWorld } from "../src/codegen/world/IWorld.sol";
import { CounterTable, CounterTableTableId } from "../src/codegen/Tables.sol";
import { CounterTable, CounterTableTableId } from "../src/codegen/index.sol";

contract CounterTest is MudTest {
IWorld world;
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal/packages/contracts/test/StructTest.t.sol
Expand Up @@ -6,7 +6,7 @@ import { MudTest } from "@latticexyz/world/test/MudTest.t.sol";
import { getKeysWithValue } from "@latticexyz/world/src/modules/keyswithvalue/getKeysWithValue.sol";

import { IWorld } from "../src/codegen/world/IWorld.sol";
import { CounterTable, CounterTableTableId } from "../src/codegen/Tables.sol";
import { CounterTable, CounterTableTableId } from "../src/codegen/index.sol";
import { BytesStruct, StringStruct } from "../src/systems/structs.sol";

contract StructTest is MudTest {
Expand Down
13 changes: 13 additions & 0 deletions packages/cli/contracts/src/codegen/common.sol

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

10 changes: 10 additions & 0 deletions packages/cli/contracts/src/codegen/index.sol

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

2 changes: 1 addition & 1 deletion packages/cli/contracts/src/codegen/tables/Statics.sol

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

4 changes: 2 additions & 2 deletions packages/cli/contracts/test/Tablegen.t.sol
Expand Up @@ -4,9 +4,9 @@ pragma solidity >=0.8.0;
import "forge-std/Test.sol";
import { StoreMock } from "@latticexyz/store/test/StoreMock.sol";

import { Statics, StaticsData, Dynamics1, Dynamics1Data, Dynamics2, Dynamics2Data, Singleton, Ephemeral } from "../src/codegen/Tables.sol";
import { Statics, StaticsData, Dynamics1, Dynamics1Data, Dynamics2, Dynamics2Data, Singleton, Ephemeral } from "../src/codegen/index.sol";

import { Enum1, Enum2 } from "../src/codegen/Types.sol";
import { Enum1, Enum2 } from "../src/codegen/common.sol";

contract TablegenTest is Test, StoreMock {
function testStaticsSetAndGet() public {
Expand Down
2 changes: 1 addition & 1 deletion packages/store/src/StoreCore.sol
Expand Up @@ -9,7 +9,7 @@ import { FieldLayout, FieldLayoutLib } from "./FieldLayout.sol";
import { Schema, SchemaLib } from "./Schema.sol";
import { PackedCounter } from "./PackedCounter.sol";
import { Slice, SliceLib } from "./Slice.sol";
import { StoreHooks, Tables, StoreHooksTableId } from "./codegen/Tables.sol";
import { StoreHooks, Tables, StoreHooksTableId } from "./codegen/index.sol";
import { IStoreErrors } from "./IStoreErrors.sol";
import { IStoreHook } from "./IStoreHook.sol";
import { StoreSwitch } from "./StoreSwitch.sol";
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/store/src/codegen/tables/KeyEncoding.sol

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

2 changes: 1 addition & 1 deletion packages/store/test/Gas.t.sol
Expand Up @@ -7,7 +7,7 @@ import { Bytes } from "../src/Bytes.sol";
import { SliceLib } from "../src/Slice.sol";
import { Storage } from "../src/Storage.sol";
import { PackedCounter } from "../src/PackedCounter.sol";
import { Mixed, MixedData } from "../src/codegen/Tables.sol";
import { Mixed, MixedData } from "../src/codegen/index.sol";

contract SomeContract {
function doSomethingWithBytes(bytes memory data) public {}
Expand Down
4 changes: 2 additions & 2 deletions packages/store/test/KeyEncoding.t.sol
Expand Up @@ -3,8 +3,8 @@ pragma solidity >=0.8.0;

import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { KeyEncoding, KeyEncodingTableId } from "../src/codegen/Tables.sol";
import { ExampleEnum } from "../src/codegen/Types.sol";
import { KeyEncoding, KeyEncodingTableId } from "../src/codegen/index.sol";
import { ExampleEnum } from "../src/codegen/common.sol";
import { StoreCore } from "../src/StoreCore.sol";
import { StoreMock } from "../test/StoreMock.sol";
import { FieldLayout } from "../src/FieldLayout.sol";
Expand Down
2 changes: 1 addition & 1 deletion packages/store/test/Mixed.t.sol
Expand Up @@ -3,7 +3,7 @@ pragma solidity >=0.8.0;

import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { Mixed, MixedData, MixedTableId } from "../src/codegen/Tables.sol";
import { Mixed, MixedData, MixedTableId } from "../src/codegen/index.sol";
import { StoreCore } from "../src/StoreCore.sol";
import { StoreMock } from "../test/StoreMock.sol";
import { FieldLayout } from "../src/FieldLayout.sol";
Expand Down
2 changes: 1 addition & 1 deletion packages/store/test/StoreCore.t.sol
Expand Up @@ -14,7 +14,7 @@ import { StoreMock } from "../test/StoreMock.sol";
import { IStoreErrors } from "../src/IStoreErrors.sol";
import { IStore } from "../src/IStore.sol";
import { StoreSwitch } from "../src/StoreSwitch.sol";
import { Tables, TablesTableId } from "../src/codegen/Tables.sol";
import { Tables, TablesTableId } from "../src/codegen/index.sol";
import { FieldLayoutEncodeHelper } from "./FieldLayoutEncodeHelper.sol";
import { BEFORE_SET_RECORD, AFTER_SET_RECORD, BEFORE_SET_FIELD, AFTER_SET_FIELD, BEFORE_DELETE_RECORD, AFTER_DELETE_RECORD } from "../src/storeHookTypes.sol";
import { SchemaEncodeHelper } from "./SchemaEncodeHelper.sol";
Expand Down
2 changes: 1 addition & 1 deletion packages/store/test/Vector2.t.sol
Expand Up @@ -3,7 +3,7 @@ pragma solidity >=0.8.0;

import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { Vector2, Vector2Data, Vector2TableId } from "../src/codegen/Tables.sol";
import { Vector2, Vector2Data, Vector2TableId } from "../src/codegen/index.sol";
import { StoreCore } from "../src/StoreCore.sol";
import { StoreMock } from "../test/StoreMock.sol";
import { FieldLayout } from "../src/FieldLayout.sol";
Expand Down
2 changes: 1 addition & 1 deletion packages/store/test/tables/Callbacks.t.sol
Expand Up @@ -4,7 +4,7 @@ pragma solidity >=0.8.0;
import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { StoreMock } from "../../test/StoreMock.sol";
import { Callbacks } from "../../src/codegen/Tables.sol";
import { Callbacks } from "../../src/codegen/index.sol";

contract CallbacksTest is Test, GasReporter, StoreMock {
function testSetAndGet() public {
Expand Down
2 changes: 1 addition & 1 deletion packages/store/test/tables/StoreHooks.t.sol
Expand Up @@ -4,7 +4,7 @@ pragma solidity >=0.8.0;
import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { StoreMock } from "../../test/StoreMock.sol";
import { StoreHooks } from "../../src/codegen/Tables.sol";
import { StoreHooks } from "../../src/codegen/index.sol";

contract StoreHooksTest is Test, GasReporter, StoreMock {
function testTable() public {
Expand Down
2 changes: 1 addition & 1 deletion packages/store/test/tables/StoreHooksColdLoad.t.sol
Expand Up @@ -4,7 +4,7 @@ pragma solidity >=0.8.0;
import { Test } from "forge-std/Test.sol";
import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { StoreMock } from "../../test/StoreMock.sol";
import { StoreHooks } from "../../src/codegen/Tables.sol";
import { StoreHooks } from "../../src/codegen/index.sol";

contract StoreHooksColdLoadTest is Test, GasReporter, StoreMock {
bytes21[] hooks;
Expand Down
4 changes: 2 additions & 2 deletions packages/store/ts/codegen/tablegen.ts
Expand Up @@ -24,12 +24,12 @@ export async function tablegen(config: StoreConfig, outputBaseDirectory: string)

// write types to file
if (Object.keys(config.enums).length > 0) {
const fullOutputPath = path.join(outputBaseDirectory, `${config.userTypesPath}.sol`);
const fullOutputPath = path.join(outputBaseDirectory, config.userTypesFilename);
const output = renderTypesFromConfig(config);
await formatAndWriteSolidity(output, fullOutputPath, "Generated types file");
}

const fullOutputPath = path.join(outputBaseDirectory, `Tables.sol`);
const fullOutputPath = path.join(outputBaseDirectory, config.codegenIndexFilename);
const output = renderTableIndex(allTableOptions);
await formatAndWriteSolidity(output, fullOutputPath, "Generated table index");
}
2 changes: 1 addition & 1 deletion packages/store/ts/codegen/userType.ts
Expand Up @@ -61,7 +61,7 @@ export function importForAbiOrUserType(
// user types
return {
symbol: abiOrUserType,
fromPath: config.userTypesPath + ".sol",
fromPath: config.userTypesFilename,
usedInPath: usedInDirectory,
};
}
Expand Down
3 changes: 2 additions & 1 deletion packages/store/ts/config/defaults.ts
@@ -1,7 +1,8 @@
export const PATH_DEFAULTS = {
storeImportPath: "@latticexyz/store/src/",
userTypesPath: "Types",
userTypesFilename: "common.sol",
codegenDirectory: "codegen",
codegenIndexFilename: "index.sol",
} as const;

export const DEFAULTS = {
Expand Down
9 changes: 6 additions & 3 deletions packages/store/ts/config/storeConfig.ts
Expand Up @@ -261,19 +261,22 @@ export type MUDUserConfig<
namespace?: string;
/** Path for store package imports. Default is "@latticexyz/store/src/" */
storeImportPath?: string;
/** Path to the file where common user types will be generated and imported from. Default is "Types" */
userTypesPath?: string;
/** Filename where common user types will be generated and imported from. Default is "common.sol" */
userTypesFilename?: string;
/** Path to the directory where generated files will be placed. (Default is "codegen") */
codegenDirectory?: string;
/** Filename where codegen index will be generated. Default is "index.sol" */
codegenIndexFilename?: string;
};

const StoreConfigUnrefined = z
.object({
namespace: zSelector.default(DEFAULTS.namespace),
storeImportPath: z.string().default(PATH_DEFAULTS.storeImportPath),
tables: zTablesConfig,
userTypesPath: z.string().default(PATH_DEFAULTS.userTypesPath),
userTypesFilename: z.string().default(PATH_DEFAULTS.userTypesFilename),
codegenDirectory: z.string().default(PATH_DEFAULTS.codegenDirectory),
codegenIndexFilename: z.string().default(PATH_DEFAULTS.codegenIndexFilename),
})
.merge(zEnumsConfig);

Expand Down
3 changes: 2 additions & 1 deletion packages/store/ts/register/mudConfig.test-d.ts
Expand Up @@ -50,7 +50,8 @@ describe("mudConfig", () => {
};
namespace: "";
storeImportPath: "@latticexyz/store/src/";
userTypesPath: "Types";
userTypesFilename: "common.sol";
codegenDirectory: "codegen";
codegenIndexFilename: "index.sol";
}>();
});
3 changes: 2 additions & 1 deletion packages/store/ts/register/typeExtensions.ts
Expand Up @@ -27,8 +27,9 @@ export interface ExpandMUDUserConfig<T extends MUDCoreUserConfig>
enums: typeof DEFAULTS.enums;
namespace: typeof DEFAULTS.namespace;
storeImportPath: typeof PATH_DEFAULTS.storeImportPath;
userTypesPath: typeof PATH_DEFAULTS.userTypesPath;
userTypesFilename: typeof PATH_DEFAULTS.userTypesFilename;
codegenDirectory: typeof PATH_DEFAULTS.codegenDirectory;
codegenIndexFilename: typeof PATH_DEFAULTS.codegenIndexFilename;
}
> {
tables: ExpandTablesConfig<T["tables"]>;
Expand Down
10 changes: 10 additions & 0 deletions packages/world/src/common.sol
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

/* Autogenerated file. Do not edit manually. */
enum Resource {
NONE,
NAMESPACE,
TABLE,
SYSTEM
}
23 changes: 23 additions & 0 deletions packages/world/src/index.sol
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

/* Autogenerated file. Do not edit manually. */

import { NamespaceOwner, NamespaceOwnerTableId } from "./tables/NamespaceOwner.sol";
import { ResourceAccess, ResourceAccessTableId } from "./tables/ResourceAccess.sol";
import { InstalledModules, InstalledModulesTableId } from "./tables/InstalledModules.sol";
import { Delegations, DelegationsTableId } from "./tables/Delegations.sol";
import { Balances, BalancesTableId } from "./modules/core/tables/Balances.sol";
import { Systems, SystemsTableId } from "./modules/core/tables/Systems.sol";
import { SystemRegistry, SystemRegistryTableId } from "./modules/core/tables/SystemRegistry.sol";
import { SystemHooks, SystemHooksTableId } from "./modules/core/tables/SystemHooks.sol";
import { ResourceType, ResourceTypeTableId } from "./modules/core/tables/ResourceType.sol";
import { FunctionSelectors, FunctionSelectorsTableId } from "./modules/core/tables/FunctionSelectors.sol";
import { KeysWithValue } from "./modules/keyswithvalue/tables/KeysWithValue.sol";
import { KeysInTable, KeysInTableData, KeysInTableTableId } from "./modules/keysintable/tables/KeysInTable.sol";
import { UsedKeysIndex, UsedKeysIndexTableId } from "./modules/keysintable/tables/UsedKeysIndex.sol";
import { UniqueEntity } from "./modules/uniqueentity/tables/UniqueEntity.sol";
import { CallboundDelegations, CallboundDelegationsTableId } from "./modules/std-delegations/tables/CallboundDelegations.sol";
import { TimeboundDelegations, TimeboundDelegationsTableId } from "./modules/std-delegations/tables/TimeboundDelegations.sol";
import { Bool } from "./../test/tables/Bool.sol";
import { AddressArray } from "./../test/tables/AddressArray.sol";
2 changes: 1 addition & 1 deletion packages/world/src/modules/core/CoreModule.sol
Expand Up @@ -3,7 +3,7 @@ pragma solidity >=0.8.0;

import { WorldContextProvider } from "../../WorldContext.sol";
import { ROOT_NAMESPACE } from "../../constants.sol";
import { Resource } from "../../Types.sol";
import { Resource } from "../../common.sol";
import { Module } from "../../Module.sol";

import { IBaseWorld } from "../../interfaces/IBaseWorld.sol";
Expand Down

0 comments on commit ac508bf

Please sign in to comment.