-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Clean task #5540
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
v-next/hardhat/src/internal/builtin-plugins/clean/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
v-next/hardhat/src/internal/builtin-plugins/clean/task-action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?.(); | ||
schaable marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
export default cleanAction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
118
v-next/hardhat/test/internal/builtin-plugins/clean/task-action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 coreThere was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kanej can you confirm?