Skip to content

Commit 87ad988

Browse files
authored
Merge pull request #5274 from NomicFoundation/tasks-def
Tasks builders implementation and testing
2 parents 0305823 + 1e79c20 commit 87ad988

File tree

8 files changed

+1442
-127
lines changed

8 files changed

+1442
-127
lines changed

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

+208-106
Large diffs are not rendered by default.

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

+6
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ export function formatTaskId(taskId: string | string[]): string {
55

66
return taskId.join(" ");
77
}
8+
9+
const FILE_PROTOCOL_PATTERN = /^file:\/\/.+/;
10+
11+
export function isValidActionUrl(action: string): boolean {
12+
return FILE_PROTOCOL_PATTERN.test(action);
13+
}

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

+56
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,59 @@ export enum ParameterType {
99
FLOAT = "FLOAT",
1010
FILE = "FILE",
1111
}
12+
13+
/**
14+
* Maps all the `ParameterType` values to their corresponding value types.
15+
*/
16+
export interface ParameterToValueTypeMap {
17+
[ParameterType.STRING]: string;
18+
[ParameterType.BOOLEAN]: boolean;
19+
[ParameterType.INT]: number;
20+
[ParameterType.BIGINT]: bigint;
21+
[ParameterType.FLOAT]: number;
22+
[ParameterType.FILE]: string;
23+
}
24+
25+
/**
26+
* Maps a `ParameterType` to its corresponding value type.
27+
*
28+
* This type takes a `ParameterType` as a type parameter and returns the type
29+
* of the value that should be used for parameters of that type.
30+
*
31+
* @example
32+
* ParameterTypeToValueType<ParameterType.STRING>
33+
* // ^? "string"
34+
*
35+
* @example
36+
* ParameterTypeToValueType<ParameterType.INT>
37+
* // ^? "number"
38+
*/
39+
export type ParameterTypeToValueType<T extends ParameterType> =
40+
ParameterToValueTypeMap[T];
41+
42+
const parameterTypeValidators: Record<
43+
ParameterType,
44+
(value: unknown) => boolean
45+
> = {
46+
[ParameterType.STRING]: (value): value is string => typeof value === "string",
47+
[ParameterType.BOOLEAN]: (value): value is boolean =>
48+
typeof value === "boolean",
49+
[ParameterType.INT]: (value): value is number => Number.isInteger(value),
50+
[ParameterType.BIGINT]: (value): value is bigint => typeof value === "bigint",
51+
[ParameterType.FLOAT]: (value): value is number => typeof value === "number",
52+
[ParameterType.FILE]: (value): value is string => typeof value === "string",
53+
};
54+
55+
/**
56+
* Checks if a parameter value is valid for a given parameter type.
57+
*
58+
* This function uses a map of validators, where each validator is a function
59+
* that checks if a value is valid for a specific parameter type.
60+
*/
61+
export function isParameterValueValid(
62+
type: ParameterType,
63+
value: unknown,
64+
): boolean {
65+
const validator = parameterTypeValidators[type];
66+
return validator(value);
67+
}

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

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ParameterType } from "./common.js";
2-
import { HardhatRuntimeEnvironment } from "./hre.js";
1+
import type { ParameterType, ParameterTypeToValueType } from "./common.js";
2+
import type { HardhatRuntimeEnvironment } from "./hre.js";
33

44
// We add the TaskManager to the HRE with a module augmentation to avoid
55
// introducing a circular dependency that would look like this:
@@ -182,11 +182,11 @@ export interface NewTaskDefinitionBuilder {
182182
* The default value, if provided, should be of the same type as the
183183
* parameter.
184184
*/
185-
addNamedParameter(paramOptions: {
185+
addNamedParameter<T extends ParameterType>(paramOptions: {
186186
name: string;
187187
description?: string;
188-
type?: ParameterType;
189-
defaultValue?: any;
188+
type?: T;
189+
defaultValue?: ParameterTypeToValueType<T>;
190190
}): this;
191191

192192
/**
@@ -210,11 +210,11 @@ export interface NewTaskDefinitionBuilder {
210210
* optional, and any other positional parameters after it must also be
211211
* optional.
212212
*/
213-
addPositionalParameter(paramOptions: {
213+
addPositionalParameter<T extends ParameterType>(paramOptions: {
214214
name: string;
215215
description?: string;
216-
type?: ParameterType;
217-
defaultValue?: any;
216+
type?: T;
217+
defaultValue?: ParameterTypeToValueType<T>;
218218
}): this;
219219

220220
/**
@@ -231,11 +231,11 @@ export interface NewTaskDefinitionBuilder {
231231
* Note that this parameter must be the last positional parameter. No other
232232
* positional parameter can follow it, including variadic parameters.
233233
*/
234-
addVariadicParameter(paramOptions: {
234+
addVariadicParameter<T extends ParameterType>(paramOptions: {
235235
name: string;
236236
description?: string;
237-
type?: ParameterType;
238-
defaultValue?: any[];
237+
type?: T;
238+
defaultValue?: Array<ParameterTypeToValueType<T>>;
239239
}): this;
240240

241241
/**
@@ -253,30 +253,30 @@ export interface TaskOverrideDefinitionBuilder {
253253
*/
254254
setDescription(description: string): this;
255255

256+
/**
257+
* Sets a new action for the task.
258+
*
259+
* @see NewTaskDefinitionBuilder.setAction
260+
*/
261+
setAction(action: TaskOverrideActionFunction | string): this;
262+
256263
/**
257264
* Adds a new named parameter to the task.
258265
*
259266
* @see NewTaskDefinitionBuilder.addNamedParameter
260267
*/
261-
addNamedParameter(paramOptions: {
268+
addNamedParameter<T extends ParameterType>(paramOptions: {
262269
name: string;
263270
description?: string;
264-
type?: ParameterType;
265-
defaultValue: any;
271+
type?: T;
272+
defaultValue?: ParameterTypeToValueType<T>;
266273
}): this;
267274

268275
/**
269276
* Adds a named parameter of boolean type and default value false.
270277
*/
271278
addFlag(paramOptions: { name: string; description?: string }): this;
272279

273-
/**
274-
* Sets a new action for the task.
275-
*
276-
* @see NewTaskDefinitionBuilder.setAction
277-
*/
278-
setAction(action: TaskOverrideActionFunction | string): this;
279-
280280
/**
281281
* Builds the TaskOverrideDefinition.
282282
*/

0 commit comments

Comments
 (0)