Skip to content

Commit 1e53f2a

Browse files
committed
Add empty task builder
1 parent 16a860c commit 1e53f2a

File tree

5 files changed

+161
-28
lines changed

5 files changed

+161
-28
lines changed

v-next/core/src/config.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { buildGlobalParameterDefinition } from "./internal/global-parameters.js";
22
import {
3+
EmptyTaskDefinitionBuilderImplementation,
34
NewTaskDefinitionBuilderImplementation,
45
TaskOverrideDefinitionBuilderImplementation,
56
} from "./internal/tasks/builders.js";
67
import { ParameterType } from "./types/common.js";
78
import { ConfigurationVariable } from "./types/config.js";
89
import { GlobalParameter } from "./types/global-parameters.js";
910
import {
10-
EmptyTaskDefinition,
11+
EmptyTaskDefinitionBuilder,
1112
NewTaskDefinitionBuilder,
12-
TaskDefinitionType,
1313
TaskOverrideDefinitionBuilder,
1414
} from "./types/tasks.js";
1515

@@ -40,12 +40,8 @@ export function task(
4040
export function emptyTask(
4141
id: string | string[],
4242
description: string,
43-
): EmptyTaskDefinition {
44-
return {
45-
type: TaskDefinitionType.EMPTY_TASK,
46-
id: Array.isArray(id) ? id : [id],
47-
description,
48-
};
43+
): EmptyTaskDefinitionBuilder {
44+
return new EmptyTaskDefinitionBuilderImplementation(id, description);
4945
}
5046

5147
/**

v-next/core/src/internal/tasks/builders.ts

+34
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import type {
88
TaskOverrideActionFunction,
99
TaskOverrideDefinitionBuilder,
1010
TaskOverrideDefinition,
11+
EmptyTaskDefinitionBuilder,
12+
EmptyTaskDefinition,
1113
} from "../../types/tasks.js";
1214

1315
import { HardhatError } from "@nomicfoundation/hardhat-errors";
@@ -21,6 +23,38 @@ import {
2123

2224
import { formatTaskId, isValidActionUrl } from "./utils.js";
2325

26+
export class EmptyTaskDefinitionBuilderImplementation
27+
implements EmptyTaskDefinitionBuilder
28+
{
29+
readonly #id: string[];
30+
31+
#description: string;
32+
33+
constructor(id: string | string[], description: string = "") {
34+
if (id.length === 0) {
35+
throw new HardhatError(
36+
HardhatError.ERRORS.TASK_DEFINITIONS.EMPTY_TASK_ID,
37+
);
38+
}
39+
40+
this.#id = Array.isArray(id) ? id : [id];
41+
this.#description = description;
42+
}
43+
44+
public setDescription(description: string): this {
45+
this.#description = description;
46+
return this;
47+
}
48+
49+
public build(): EmptyTaskDefinition {
50+
return {
51+
type: TaskDefinitionType.EMPTY_TASK,
52+
id: this.#id,
53+
description: this.#description,
54+
};
55+
}
56+
}
57+
2458
export class NewTaskDefinitionBuilderImplementation
2559
implements NewTaskDefinitionBuilder
2660
{

v-next/core/src/types/tasks.ts

+15
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ export type TaskDefinition =
157157
| NewTaskDefinition
158158
| TaskOverrideDefinition;
159159

160+
/**
161+
* A builder for creating EmptyTaskDefinitions.
162+
*/
163+
export interface EmptyTaskDefinitionBuilder {
164+
/**
165+
* Sets the description of the task.
166+
*/
167+
setDescription(description: string): this;
168+
169+
/**
170+
* Builds the EmptyTaskDefinition.
171+
*/
172+
build(): EmptyTaskDefinition;
173+
}
174+
160175
/**
161176
* A builder for creating NewTaskDefinitions.
162177
*/

v-next/core/test/internal/tasks/builders.ts

+91
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,104 @@ import { HardhatError } from "@nomicfoundation/hardhat-errors";
55

66
import { RESERVED_PARAMETER_NAMES } from "../../../src/internal/parameters.js";
77
import {
8+
EmptyTaskDefinitionBuilderImplementation,
89
NewTaskDefinitionBuilderImplementation,
910
TaskOverrideDefinitionBuilderImplementation,
1011
} from "../../../src/internal/tasks/builders.js";
1112
import { ParameterType } from "../../../src/types/common.js";
1213
import { TaskDefinitionType } from "../../../src/types/tasks.js";
1314

1415
describe("Task builders", () => {
16+
describe("EmptyTaskDefinitionBuilderImplementation", () => {
17+
it("should create an empty task definition builder", () => {
18+
const builder = new EmptyTaskDefinitionBuilderImplementation("task-id");
19+
const taskDefinition = builder.build();
20+
21+
assert.deepEqual(taskDefinition, {
22+
type: TaskDefinitionType.EMPTY_TASK,
23+
id: ["task-id"],
24+
description: "",
25+
});
26+
});
27+
28+
it("should create an empty task definition builder with an array of ids", () => {
29+
const ids = ["task-id", "subtask-id", "sub-subtask-id"];
30+
const builder = new EmptyTaskDefinitionBuilderImplementation(ids);
31+
const taskDefinition = builder.build();
32+
33+
assert.deepEqual(taskDefinition, {
34+
type: TaskDefinitionType.EMPTY_TASK,
35+
id: ids,
36+
description: "",
37+
});
38+
});
39+
40+
describe("Task id validation", () => {
41+
it("should throw if the id is an empty string", () => {
42+
assert.throws(() => new EmptyTaskDefinitionBuilderImplementation(""), {
43+
name: "HardhatError",
44+
message:
45+
"HHE208: Task id cannot be an empty string or an empty array",
46+
});
47+
});
48+
49+
it("should throw if the array of ids is empty", () => {
50+
const ids: string[] = [];
51+
52+
assert.throws(() => new EmptyTaskDefinitionBuilderImplementation(ids), {
53+
name: "HardhatError",
54+
message:
55+
"HHE208: Task id cannot be an empty string or an empty array",
56+
});
57+
});
58+
});
59+
60+
describe("Adding a description", () => {
61+
it("should create an empty task definition builder with a description in the constructor", () => {
62+
const builder = new EmptyTaskDefinitionBuilderImplementation(
63+
"task-id",
64+
"Task description",
65+
);
66+
const taskDefinition = builder.build();
67+
68+
assert.deepEqual(taskDefinition, {
69+
type: TaskDefinitionType.EMPTY_TASK,
70+
id: ["task-id"],
71+
description: "Task description",
72+
});
73+
});
74+
75+
it("should set the task description", () => {
76+
const builder = new EmptyTaskDefinitionBuilderImplementation("task-id");
77+
const taskDefinition = builder
78+
.setDescription("Task description")
79+
.build();
80+
81+
assert.deepEqual(taskDefinition, {
82+
type: TaskDefinitionType.EMPTY_TASK,
83+
id: ["task-id"],
84+
description: "Task description",
85+
});
86+
});
87+
88+
it("should override the description set in the constructor", () => {
89+
const builder = new EmptyTaskDefinitionBuilderImplementation(
90+
"task-id",
91+
"Task description",
92+
);
93+
const taskDefinition = builder
94+
.setDescription("New task description")
95+
.build();
96+
97+
assert.deepEqual(taskDefinition, {
98+
type: TaskDefinitionType.EMPTY_TASK,
99+
id: ["task-id"],
100+
description: "New task description",
101+
});
102+
});
103+
});
104+
});
105+
15106
describe("NewTaskDefinitionBuilderImplementation", () => {
16107
it("should create a new task definition builder", () => {
17108
const builder = new NewTaskDefinitionBuilderImplementation("task-id");

v-next/core/test/internal/tasks/task-manager.ts

+17-20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ParameterType } from "../../../src/config.js";
77
import { createHardhatRuntimeEnvironment } from "../../../src/index.js";
88
import { buildGlobalParameterDefinition } from "../../../src/internal/global-parameters.js";
99
import {
10+
EmptyTaskDefinitionBuilderImplementation,
1011
NewTaskDefinitionBuilderImplementation,
1112
TaskOverrideDefinitionBuilderImplementation,
1213
} from "../../../src/internal/tasks/builders.js";
@@ -291,11 +292,10 @@ describe("TaskManagerImplementation", () => {
291292
{
292293
id: "plugin1",
293294
tasks: [
294-
{
295-
id: ["task1"],
296-
description: "description1",
297-
type: TaskDefinitionType.EMPTY_TASK,
298-
},
295+
new EmptyTaskDefinitionBuilderImplementation(
296+
"task1",
297+
"description1",
298+
).build(),
299299
],
300300
},
301301
],
@@ -312,11 +312,10 @@ describe("TaskManagerImplementation", () => {
312312
{
313313
id: "plugin1",
314314
tasks: [
315-
{
316-
id: ["task1"],
317-
description: "description1",
318-
type: TaskDefinitionType.EMPTY_TASK,
319-
},
315+
new EmptyTaskDefinitionBuilderImplementation(
316+
"task1",
317+
"description1",
318+
).build(),
320319
// adds a subtask to the empty task
321320
new NewTaskDefinitionBuilderImplementation(["task1", "subtask1"])
322321
.setAction(() => {})
@@ -1140,11 +1139,10 @@ describe("TaskManagerImplementation", () => {
11401139
{
11411140
id: "plugin1",
11421141
tasks: [
1143-
{
1144-
id: ["task1"],
1145-
description: "description1",
1146-
type: TaskDefinitionType.EMPTY_TASK,
1147-
},
1142+
new EmptyTaskDefinitionBuilderImplementation(
1143+
"task1",
1144+
"description1",
1145+
).build(),
11481146
new TaskOverrideDefinitionBuilderImplementation("task1")
11491147
.setAction(async (args, _hre, runSuper) => {
11501148
await runSuper(args);
@@ -1226,11 +1224,10 @@ describe("TaskManagerImplementation", () => {
12261224
{
12271225
id: "plugin1",
12281226
tasks: [
1229-
{
1230-
id: ["task1"],
1231-
description: "description1",
1232-
type: TaskDefinitionType.EMPTY_TASK,
1233-
},
1227+
new EmptyTaskDefinitionBuilderImplementation(
1228+
"task1",
1229+
"description1",
1230+
).build(),
12341231
],
12351232
},
12361233
],

0 commit comments

Comments
 (0)