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

Clean task #5540

Merged
merged 5 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion v-next/core/src/global-dir.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ensureDir } from "@ignored/hardhat-vnext-utils/fs";

/**
* Returns the path to the hardhat configuration directory.
* Returns the path to the hardhat configuration directory.
*
* @returns The path to the hardhat configuration directory.
*/
Expand All @@ -11,6 +11,17 @@ export async function getConfigDir(): Promise<string> {
return config;
}

/**
* Returns the path to the hardhat cache directory.
*
* @returns The path to the hardhat cache directory.
*/
export async function getCacheDir(): Promise<string> {
const { cache } = await generatePaths();
await ensureDir(cache);
return cache;
}

async function generatePaths(packageName = "hardhat") {
const { default: envPaths } = await import("env-paths");
return envPaths(packageName);
Expand Down
2 changes: 2 additions & 0 deletions v-next/core/src/internal/hre.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ async function resolveUserConfig(
tasks: config.tasks ?? [],
paths: {
root: projectRoot,
cache: config.paths?.cache ?? "", // TODO: resolve cache path
Copy link
Member

@alcuadrado alcuadrado Jul 27, 2024

Choose a reason for hiding this comment

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

Do we have a task to properly implement this resolution? Can you create one?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe these things should be defined in hardhat, not core

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's part of #5524, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

@kanej can you confirm?

artifacts: config.paths?.artifacts ?? "", // TODO: resolve artifacts path
},
};

Expand Down
9 changes: 7 additions & 2 deletions v-next/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ export interface HardhatUserConfig {
/**
* The different paths that conform a Hardhat project.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-interface -- TODO: add the paths
export interface ProjectPathsUserConfig {}
export interface ProjectPathsUserConfig {
root?: string;
cache?: string;
artifacts?: string;
}

/**
* The resolved Hardhat configuration.
Expand All @@ -64,4 +67,6 @@ export interface HardhatConfig {

export interface ProjectPathsConfig {
root: string;
cache: string;
artifacts: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ describe("ResolvedConfigurationVariable", () => {
plugins: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
},
hooks: hookManager,
Expand Down
28 changes: 28 additions & 0 deletions v-next/core/test/internal/hook-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ describe("HookManager", () => {
plugins: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
},
hooks: hookManager,
Expand Down Expand Up @@ -171,6 +173,8 @@ describe("HookManager", () => {
tasks: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
};

Expand Down Expand Up @@ -224,6 +228,8 @@ describe("HookManager", () => {
tasks: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
};

Expand Down Expand Up @@ -327,6 +333,8 @@ describe("HookManager", () => {
plugins: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
},
hooks: hookManager,
Expand All @@ -345,6 +353,8 @@ describe("HookManager", () => {
tasks: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
};

Expand Down Expand Up @@ -429,6 +439,8 @@ describe("HookManager", () => {
tasks: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
};

Expand Down Expand Up @@ -550,6 +562,8 @@ describe("HookManager", () => {
plugins: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
},
hooks: hookManager,
Expand Down Expand Up @@ -642,6 +656,8 @@ describe("HookManager", () => {
tasks: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
};

Expand Down Expand Up @@ -684,6 +700,8 @@ describe("HookManager", () => {
plugins: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
},
hooks: hookManager,
Expand All @@ -700,6 +718,8 @@ describe("HookManager", () => {
tasks: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
};

Expand All @@ -718,6 +738,8 @@ describe("HookManager", () => {
tasks: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
};

Expand Down Expand Up @@ -801,6 +823,8 @@ describe("HookManager", () => {
tasks: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
};

Expand Down Expand Up @@ -843,6 +867,8 @@ describe("HookManager", () => {
plugins: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
},
hooks: hookManager,
Expand Down Expand Up @@ -1002,6 +1028,8 @@ function buildMockHardhatRuntimeEnvironment(
plugins: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
},
tasks: mockTaskManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ describe("UserInterruptionManager", () => {
plugins: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
},
globalOptions: {},
Expand Down Expand Up @@ -74,6 +76,8 @@ describe("UserInterruptionManager", () => {
plugins: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
},
globalOptions: {},
Expand Down Expand Up @@ -120,6 +124,8 @@ describe("UserInterruptionManager", () => {
plugins: [],
paths: {
root: projectRoot,
cache: "",
artifacts: "",
},
},
globalOptions: {},
Expand Down
18 changes: 18 additions & 0 deletions v-next/hardhat/src/internal/builtin-plugins/clean/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { HardhatPlugin } from "@ignored/hardhat-vnext-core/types/plugins";

import { task } from "@ignored/hardhat-vnext-core/config";

const hardhatPlugin: HardhatPlugin = {
id: "clean",
tasks: [
task("clean", "Clears the cache and deletes all artifacts")
.addFlag({
name: "global",
description: "Clear the global cache",
})
.setAction(import.meta.resolve("./task-action.js"))
.build(),
],
};

export default hardhatPlugin;
26 changes: 26 additions & 0 deletions v-next/hardhat/src/internal/builtin-plugins/clean/task-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { NewTaskActionFunction } from "@ignored/hardhat-vnext-core/types/tasks";

import { getCacheDir } from "@ignored/hardhat-vnext-core/global-dir";
import { emptyDir, remove } from "@ignored/hardhat-vnext-utils/fs";

interface CleanActionArguments {
global: boolean;
}

const cleanAction: NewTaskActionFunction<CleanActionArguments> = async (
{ global },
{ config },
) => {
if (global) {
const globalCacheDir = await getCacheDir();
await emptyDir(globalCacheDir);
}

await emptyDir(config.paths.cache);
await remove(config.paths.artifacts);

// TODO: Uncomment this when we have a clearCache method in the artifacts module
// artifacts.clearCache?.();
};

export default cleanAction;
4 changes: 3 additions & 1 deletion v-next/hardhat/src/internal/builtin-plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { HardhatPlugin } from "@ignored/hardhat-vnext-core/types/plugins";

import clean from "./clean/index.js";
import hardhatFoo from "./hardhat-foo/index.js";
import run from "./run/index.js";

// Note: When importing a plugin, you have to export its types, so that its
// type extensions, if any, also get loaded.
export type * from "./clean/index.js";
export type * from "./hardhat-foo/index.js";
export type * from "./run/index.js";

export const builtinPlugins: HardhatPlugin[] = [hardhatFoo, run];
export const builtinPlugins: HardhatPlugin[] = [clean, hardhatFoo, run];
118 changes: 118 additions & 0 deletions v-next/hardhat/test/internal/builtin-plugins/clean/task-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import type { HardhatRuntimeEnvironment } from "@ignored/hardhat-vnext-core/types/hre";

import assert from "node:assert/strict";
import path from "node:path";
import { before, beforeEach, describe, it } from "node:test";

import { getCacheDir } from "@ignored/hardhat-vnext-core/global-dir";
import {
exists,
mkdir,
readdir,
remove,
writeUtf8File,
} from "@ignored/hardhat-vnext-utils/fs";

import { createHardhatRuntimeEnvironment } from "../../../../src/hre.js";
import cleanAction from "../../../../src/internal/builtin-plugins/clean/task-action.js";
import { useFixtureProject } from "../../../helpers/project.js";

let hre: HardhatRuntimeEnvironment;
let globalCacheDir: string;
let cacheDir: string;
let artifactsDir: string;

function assertCleanBehavior(global: boolean) {
it("should clean the cache and artifacts directories", async () => {
await cleanAction({ global }, hre);

// If the cache dir exists, it should be empty
if (await exists(cacheDir)) {
const cacheContents = await readdir(cacheDir);
assert.ok(cacheContents.length === 0, "Cache dir is not empty");
}

// The artifacts dir should not exist
assert.ok(!(await exists(artifactsDir)), "Artifacts dir exists");

// If the global cache dir exists, it should be empty if the global flag is
// true, and not empty otherwise
if (await exists(globalCacheDir)) {
const globalCacheContents = await readdir(globalCacheDir);
if (global) {
assert.ok(
globalCacheContents.length === 0,
"Global cache dir is not empty",
);
} else {
assert.ok(
globalCacheContents.length > 0,
"Global cache dir is empty when it shouldn't be",
);
}
}
});
}

describe("clean/task-action", () => {
describe("cleanAction", () => {
useFixtureProject("loaded-config");

before(async function () {
globalCacheDir = await getCacheDir();
cacheDir = path.join(process.cwd(), "cache");
artifactsDir = path.join(process.cwd(), "artifacts");
hre = await createHardhatRuntimeEnvironment({
// TODO remove this once cache and artifacts are resolved in the config
paths: { cache: cacheDir, artifacts: artifactsDir },
});
});

describe("when cache and artifact dirs don't exist", async () => {
beforeEach(async () => {
await remove(globalCacheDir);
await remove(cacheDir);
await remove(artifactsDir);
});

assertCleanBehavior(true);
});

describe("when cache and artifact are empty dirs", async () => {
beforeEach(async () => {
await remove(globalCacheDir);
await remove(cacheDir);
await remove(artifactsDir);
await getCacheDir(); // Calling this recreates the cache dir
await mkdir(cacheDir);
await mkdir(artifactsDir);
});

assertCleanBehavior(true);
});

describe("when cache and artifact dirs aren't empty", async () => {
beforeEach(async () => {
await remove(globalCacheDir);
await remove(cacheDir);
await remove(artifactsDir);
await getCacheDir(); // Calling this recreates the cache dir
await writeUtf8File(path.join(globalCacheDir, "a"), "");
await writeUtf8File(path.join(cacheDir, "a"), "");
await writeUtf8File(path.join(artifactsDir, "a"), "");
});

assertCleanBehavior(true);
});

describe("when global flag is false", async () => {
beforeEach(async () => {
await remove(globalCacheDir);
await getCacheDir(); // Calling this recreates the cache dir
await writeUtf8File(path.join(globalCacheDir, "a"), "");
});

assertCleanBehavior(false);
});
});
});
Loading