Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: build step cleanup #2926

Merged
merged 13 commits into from
Jul 11, 2024
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"url": "https://github.com/latticexyz/mud.git"
},
"scripts": {
"all-codegen": "for dir in packages/store packages/world packages/world-modules packages/cli test/mock-game-contracts e2e/packages/contracts examples/*/packages/contracts templates/*/packages/contracts; do (cd \"$dir\" && pwd && pnpm build); done",
"all-build": "for dir in packages/store packages/world packages/world-modules packages/cli test/mock-game-contracts e2e/packages/contracts examples/*/packages/contracts templates/*/packages/contracts; do (cd \"$dir\" && pwd && pnpm build); done",
"all-install": "for dir in . docs e2e examples/* templates/*; do (cd \"$dir\" && pwd && pnpm install); done",
"bench": "pnpm run --recursive bench",
"build": "turbo run build",
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/scripts/generate-test-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { tablegen } from "@latticexyz/store/codegen";
import { defineStore } from "@latticexyz/store";
import { getRemappings } from "@latticexyz/common/foundry";
import { fileURLToPath } from "node:url";
import path from "node:path";

const configPath = fileURLToPath(import.meta.url);

Expand Down Expand Up @@ -97,4 +98,4 @@ const config = defineStore({

const remappings = await getRemappings();

await tablegen({ configPath, config, remappings });
await tablegen({ rootDir: path.dirname(configPath), config, remappings });
10 changes: 5 additions & 5 deletions packages/cli/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ type BuildOptions = {
foundryProfile?: string;
srcDir: string;
/**
* Path to `mud.config.ts`. We use this as the "project root" to resolve other relative paths.
* MUD project root directory where all other relative paths are resolved from.
*
* Defaults to finding the nearest `mud.config.ts`, looking in `process.cwd()` and moving up the directory tree.
* Defaults to the directory of the nearest `mud.config.ts`, looking in `process.cwd()` and moving up the directory tree.
*/
configPath: string;
rootDir: string;
config: WorldConfig;
};

export async function build({
configPath,
rootDir,
config,
srcDir,
foundryProfile = process.env.FOUNDRY_PROFILE,
Expand All @@ -28,7 +28,7 @@ export async function build({
const remappings = await getRemappings(foundryProfile);

await Promise.all([
tablegen({ configPath, config, remappings }),
tablegen({ rootDir, config, remappings }),
worldgen(config, getExistingContracts(srcDir), outPath),
]);

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { CommandModule } from "yargs";
import { loadConfig, resolveConfigPath } from "@latticexyz/config/node";
import { World as WorldConfig } from "@latticexyz/world";

import { getSrcDirectory } from "@latticexyz/common/foundry";
import { build } from "../build";
import path from "node:path";

type Options = {
configPath?: string;
Expand All @@ -27,7 +27,7 @@ const commandModule: CommandModule<Options, Options> = {
const config = (await loadConfig(configPath)) as WorldConfig;
const srcDir = await getSrcDirectory();

await build({ configPath, config, srcDir, foundryProfile: opts.profile });
await build({ rootDir: path.dirname(configPath), config, srcDir, foundryProfile: opts.profile });

process.exit(0);
},
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/commands/tablegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { loadConfig, resolveConfigPath } from "@latticexyz/config/node";
import { Store as StoreConfig } from "@latticexyz/store";
import { tablegen } from "@latticexyz/store/codegen";
import { getRemappings } from "@latticexyz/common/foundry";
import path from "node:path";

type Options = {
configPath?: string;
Expand All @@ -24,7 +25,7 @@ const commandModule: CommandModule<Options, Options> = {
const config = (await loadConfig(configPath)) as StoreConfig;
const remappings = await getRemappings();

await tablegen({ configPath, config, remappings });
await tablegen({ rootDir: path.dirname(configPath), config, remappings });

process.exit(0);
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/runDeploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export async function runDeploy(opts: DeployOptions): Promise<WorldDeploy> {

// Run build
if (!opts.skipBuild) {
await build({ configPath, config: configV2, srcDir, foundryProfile: profile });
await build({ rootDir: path.dirname(configPath), config: configV2, srcDir, foundryProfile: profile });
}

const resolvedConfig = resolveConfig({ config, forgeSourceDir: srcDir, forgeOutDir: outDir });
Expand Down
2 changes: 1 addition & 1 deletion packages/config/src/deprecated/node/loadConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function loadConfig(configPath?: string): Promise<unknown> {
}

/** @deprecated */
export async function resolveConfigPath(configPath: string | undefined, toFileURL?: boolean) {
export async function resolveConfigPath(configPath?: string, toFileURL?: boolean) {
if (configPath === undefined) {
configPath = await getUserConfigPath();
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"build:abi": "forge build",
"build:abi-ts": "abi-ts",
"build:js": "tsup",
"build:mud": "tsx ./ts/scripts/tablegen.ts && tsx ./ts/scripts/generate-test-tables.ts",
"build:mud": "tsx ./ts/scripts/build.ts && tsx ./ts/scripts/generate-test-tables.ts",
"build:tightcoder": "tsx ./ts/scripts/generate-tightcoder.ts",
"clean": "pnpm run clean:abi && pnpm run clean:js && pnpm run clean:mud",
"clean:abi": "forge clean",
Expand Down
9 changes: 6 additions & 3 deletions packages/store/ts/codegen/tablegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import { Store as StoreConfig } from "../config/v2/output";
import { storeToV1 } from "../config/v2/compat";

export type TablegenOptions = {
configPath: string;
/**
* MUD project root directory where all other relative paths are resolved from.
*/
rootDir: string;
config: StoreConfig;
remappings: [string, string][];
};

export async function tablegen({ configPath, config, remappings }: TablegenOptions) {
const outputDirectory = path.join(path.dirname(configPath), config.sourceDirectory, config.codegen.outputDirectory);
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 allTableOptions = getTableOptions(config, solidityUserTypes);
Expand Down
2 changes: 1 addition & 1 deletion packages/store/ts/config/v2/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export type Codegen = {

export type Store = {
/**
* Directory of contracts source (i.e. Solidity) relative to the MUD config.
* Directory of Solidity source relative to the MUD config.
* This is used to resolve other paths in the config, like codegen and user types.
*
* Defaults to `src` to match `foundry.toml`'s default. If you change this from the default, you may also need to configure foundry with the same source directory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import { loadConfig, resolveConfigPath } from "@latticexyz/config/node";
import { getRemappings } from "@latticexyz/common/foundry";
import { tablegen } from "../codegen";
import { Store as StoreConfig } from "../config/v2/output";
import path from "node:path";

const configPath = await resolveConfigPath(undefined);
const config = (await loadConfig(configPath)) as StoreConfig;
const remappings = await getRemappings();

await tablegen({
configPath,
config,
remappings,
});
await tablegen({ rootDir: path.dirname(configPath), config, remappings });
3 changes: 2 additions & 1 deletion packages/store/ts/scripts/generate-test-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getRemappings } from "@latticexyz/common/foundry";
import { tablegen } from "../codegen";
import { defineStore } from "../config/v2/store";
import { fileURLToPath } from "node:url";
import path from "node:path";

const configPath = fileURLToPath(import.meta.url);

Expand Down Expand Up @@ -53,4 +54,4 @@ const config = defineStore({

const remappings = await getRemappings();

await tablegen({ configPath, config, remappings });
await tablegen({ rootDir: path.dirname(configPath), config, remappings });
9 changes: 3 additions & 6 deletions packages/world-modules/ts/scripts/tablegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import { loadConfig, resolveConfigPath } from "@latticexyz/config/node";
import { getRemappings } from "@latticexyz/common/foundry";
import { Store as StoreConfig } from "@latticexyz/store";
import { tablegen } from "@latticexyz/store/codegen";
import path from "node:path";

const configPath = await resolveConfigPath(undefined);
const configPath = await resolveConfigPath();
const config = (await loadConfig(configPath)) as StoreConfig;
const remappings = await getRemappings();

await tablegen({
configPath,
config,
remappings,
});
await tablegen({ rootDir: path.dirname(configPath), config, remappings });
2 changes: 1 addition & 1 deletion packages/world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"build:abi": "forge build",
"build:abi-ts": "abi-ts",
"build:js": "tsup",
"build:mud": "tsx ./ts/scripts/tablegen.ts && tsx ./ts/scripts/worldgen.ts && tsx ./ts/scripts/generate-test-tables.ts",
"build:mud": "tsx ./ts/scripts/build.ts && tsx ./ts/scripts/generate-test-tables.ts",
"clean": "pnpm run clean:abi && pnpm run clean:js && pnpm run clean:mud",
"clean:abi": "forge clean",
"clean:js": "rimraf dist",
Expand Down
14 changes: 14 additions & 0 deletions packages/world/ts/node/findSolidityFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import path from "node:path";
import { glob } from "glob";
import { World } from "../config/v2/output";

export async function findSolidityFiles({ rootDir, config }: { rootDir: string; config: World }) {
Copy link
Member Author

@holic holic Jul 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

surprised this hasn't come up before but downstream usages may need to exclude .t.sol and .s.sol

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah likely because we're all good about putting source in src but technically possible to have test files, etc. live in this dir as well (and I personally like colocating tests with the files they're testing)

const files = await glob(path.join(config.sourceDirectory, "**", "*.sol"), {
cwd: rootDir,
});

return files.sort().map((filename) => ({
filename,
basename: path.basename(filename, ".sol"),
}));
}
41 changes: 41 additions & 0 deletions packages/world/ts/scripts/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import path from "node:path";
import { loadConfig, resolveConfigPath } from "@latticexyz/config/node";
import { getRemappings } from "@latticexyz/common/foundry";
import { tablegen } from "@latticexyz/store/codegen";
import { findSolidityFiles } from "../node/findSolidityFiles";
import { World } from "../config/v2";
import { worldgen } from "../node";

/**
* To avoid circular dependencies, we run a very similar `build` step as `cli` package here.
*/

// TODO: turn this into something we can run from CLI, then we can import/use it via CLI and here rather than duplicating.
// TODO: do the same for store build too

const configPath = await resolveConfigPath();
const rootDir = path.dirname(configPath);
const config = (await loadConfig(configPath)) as World;
const remappings = await getRemappings();

// TODO: move this into worldgen
const existingContracts = (await findSolidityFiles({ rootDir, config })).map((file) => ({
path: file.filename,
basename: file.basename,
}));
const codegenDirectory = path.join(config.sourceDirectory, config.codegen.outputDirectory);

// TODO: clean

await Promise.all([
tablegen({ rootDir, config, remappings }),
worldgen(
{
...config,
// override the namespace to be the root namespace for generating the core system interface
namespace: "",
},
existingContracts,
codegenDirectory,
),
]);
3 changes: 2 additions & 1 deletion packages/world/ts/scripts/generate-test-tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getRemappings } from "@latticexyz/common/foundry";
import { tablegen } from "@latticexyz/store/codegen";
import { defineWorld } from "../config/v2/world";
import { fileURLToPath } from "node:url";
import path from "node:path";

const configPath = fileURLToPath(import.meta.url);

Expand Down Expand Up @@ -42,4 +43,4 @@ const config = defineWorld({

const remappings = await getRemappings();

await tablegen({ configPath, config, remappings });
await tablegen({ rootDir: path.dirname(configPath), config, remappings });
14 changes: 0 additions & 14 deletions packages/world/ts/scripts/tablegen.ts

This file was deleted.

35 changes: 0 additions & 35 deletions packages/world/ts/scripts/worldgen.ts

This file was deleted.

Loading