|
1 |
| -import { HardhatRuntimeEnvironment } from "../../types/hre.js"; |
2 |
| -import { |
| 1 | +import type { ParameterValue } from "../../types/common.js"; |
| 2 | +import type { HardhatRuntimeEnvironment } from "../../types/hre.js"; |
| 3 | +import type { |
3 | 4 | NamedTaskParameter,
|
4 | 5 | NewTaskActionFunction,
|
5 | 6 | PositionalTaskParameter,
|
6 | 7 | Task,
|
7 | 8 | TaskActions,
|
8 | 9 | TaskArguments,
|
| 10 | + TaskOverrideActionFunction, |
| 11 | + TaskParameter, |
9 | 12 | } from "../../types/tasks.js";
|
10 | 13 |
|
| 14 | +import { HardhatError } from "@nomicfoundation/hardhat-errors"; |
| 15 | +import { ensureError } from "@nomicfoundation/hardhat-utils/error"; |
| 16 | + |
| 17 | +import { isParameterValueValid } from "../../types/common.js"; |
| 18 | + |
11 | 19 | import { formatTaskId } from "./utils.js";
|
12 | 20 |
|
13 | 21 | export class ResolvedTask implements Task {
|
@@ -69,24 +77,193 @@ export class ResolvedTask implements Task {
|
69 | 77 | return this.actions.length === 1 && this.actions[0].action === undefined;
|
70 | 78 | }
|
71 | 79 |
|
| 80 | + /** |
| 81 | + * This method runs the task with the given arguments. |
| 82 | + * It validates the arguments, resolves the file actions, and runs the task |
| 83 | + * actions by calling them in order. |
| 84 | + * |
| 85 | + * @param taskArguments The arguments to run the task with. |
| 86 | + * @returns The result of running the task. |
| 87 | + * @throws HardhatError if the task is empty, a required parameter is missing, |
| 88 | + * a parameter has an invalid type, or the file actions can't be resolved. |
| 89 | + */ |
72 | 90 | public async run(taskArguments: TaskArguments): Promise<any> {
|
73 |
| - // TODO: Run the task |
74 |
| - // - Validate the argument types |
75 |
| - // - Validate that there are no missing required arguments |
76 |
| - // - Resolve defaults for optional arguments |
77 |
| - // - Run the tasks actions with a chain of `runSuper`s |
78 |
| - console.log(`Running task "${formatTaskId(this.id)}"`); |
79 |
| - for (const action of this.actions) { |
80 |
| - if (action.pluginId !== undefined) { |
81 |
| - console.log( |
82 |
| - ` Running action from plugin ${action.pluginId}: ${action.action?.toString()}`, |
| 91 | + if (this.isEmpty) { |
| 92 | + throw new HardhatError(HardhatError.ERRORS.TASK_DEFINITIONS.EMPTY_TASK, { |
| 93 | + task: formatTaskId(this.id), |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + // Normalize parameters into a single iterable |
| 98 | + const allParameters: TaskParameter[] = [ |
| 99 | + ...this.namedParameters.values(), |
| 100 | + ...this.positionalParameters, |
| 101 | + ]; |
| 102 | + |
| 103 | + const providedArgumentNames = new Set(Object.keys(taskArguments)); |
| 104 | + for (const parameter of allParameters) { |
| 105 | + const value = taskArguments[parameter.name]; |
| 106 | + |
| 107 | + this.#validateRequiredParameter(parameter, value); |
| 108 | + this.#validateParameterType(parameter, value); |
| 109 | + |
| 110 | + // resolve defaults for optional parameters |
| 111 | + if (value === undefined && parameter.defaultValue !== undefined) { |
| 112 | + taskArguments[parameter.name] = parameter.defaultValue; |
| 113 | + } |
| 114 | + |
| 115 | + // Remove processed parameter from the set |
| 116 | + providedArgumentNames.delete(parameter.name); |
| 117 | + } |
| 118 | + |
| 119 | + // At this point, the set should be empty as all the task parameters have |
| 120 | + // been processed. If there are any extra parameters, an error is thrown |
| 121 | + this.#validateExtraArguments(providedArgumentNames); |
| 122 | + |
| 123 | + const next = async ( |
| 124 | + nextTaskArguments: TaskArguments, |
| 125 | + currentIndex = this.actions.length - 1, |
| 126 | + ): Promise<any> => { |
| 127 | + // The first action may be empty if the task was originally an empty task |
| 128 | + const currentAction = this.actions[currentIndex].action ?? (() => {}); |
| 129 | + |
| 130 | + const actionFn = |
| 131 | + typeof currentAction === "function" |
| 132 | + ? currentAction |
| 133 | + : await this.#resolveFileAction(currentAction, this.id); |
| 134 | + |
| 135 | + if (currentIndex === 0) { |
| 136 | + /* eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- |
| 137 | + We know that the first action in the array is a NewTaskActionFunction */ |
| 138 | + return (actionFn as NewTaskActionFunction)( |
| 139 | + nextTaskArguments, |
| 140 | + this.#hre, |
83 | 141 | );
|
84 |
| - } else { |
85 |
| - console.log(` Running action: ${action.action?.toString()}`); |
86 | 142 | }
|
| 143 | + |
| 144 | + return actionFn( |
| 145 | + nextTaskArguments, |
| 146 | + this.#hre, |
| 147 | + async (newTaskArguments: TaskArguments) => { |
| 148 | + return next(newTaskArguments, currentIndex - 1); |
| 149 | + }, |
| 150 | + ); |
| 151 | + }; |
| 152 | + |
| 153 | + return next(taskArguments); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Validates that a required parameter has a value. A parameter is required if |
| 158 | + * it doesn't have a default value. |
| 159 | + * |
| 160 | + * @throws HardhatError if the parameter is required and doesn't have a value. |
| 161 | + */ |
| 162 | + #validateRequiredParameter( |
| 163 | + parameter: TaskParameter, |
| 164 | + value: ParameterValue | ParameterValue[], |
| 165 | + ) { |
| 166 | + if (parameter.defaultValue === undefined && value === undefined) { |
| 167 | + throw new HardhatError( |
| 168 | + HardhatError.ERRORS.TASK_DEFINITIONS.MISSING_VALUE_FOR_PARAMETER, |
| 169 | + { |
| 170 | + parameter: parameter.name, |
| 171 | + task: formatTaskId(this.id), |
| 172 | + }, |
| 173 | + ); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * Validates that a parameter has the correct type. If the parameter is optional |
| 179 | + * and doesn't have a value, the type is not validated as it will be resolved |
| 180 | + * to the default value. |
| 181 | + * |
| 182 | + * @throws HardhatError if the parameter has an invalid type. |
| 183 | + */ |
| 184 | + #validateParameterType( |
| 185 | + parameter: TaskParameter, |
| 186 | + value: ParameterValue | ParameterValue[], |
| 187 | + ) { |
| 188 | + // skip type validation for optional parameters with undefined value |
| 189 | + if (value === undefined && parameter.defaultValue !== undefined) { |
| 190 | + return; |
| 191 | + } |
| 192 | + |
| 193 | + // check if the parameter is variadic |
| 194 | + const isPositionalParameter = ( |
| 195 | + param: TaskParameter, |
| 196 | + ): param is PositionalTaskParameter => "isVariadic" in param; |
| 197 | + const isVariadic = isPositionalParameter(parameter) && parameter.isVariadic; |
| 198 | + |
| 199 | + // check if the value is valid for the parameter type |
| 200 | + if (!isParameterValueValid(parameter.parameterType, value, isVariadic)) { |
| 201 | + throw new HardhatError( |
| 202 | + HardhatError.ERRORS.TASK_DEFINITIONS.INVALID_VALUE_FOR_TYPE, |
| 203 | + { |
| 204 | + value, |
| 205 | + name: parameter.name, |
| 206 | + type: parameter.parameterType, |
| 207 | + task: formatTaskId(this.id), |
| 208 | + }, |
| 209 | + ); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Validates that no extra arguments were provided in the task arguments. |
| 215 | + * |
| 216 | + * @throws HardhatError if extra arguments were provided. The error message |
| 217 | + * includes the name of the first extra argument. |
| 218 | + */ |
| 219 | + #validateExtraArguments(providedArgumentNames: Set<string>) { |
| 220 | + if (providedArgumentNames.size > 0) { |
| 221 | + throw new HardhatError( |
| 222 | + HardhatError.ERRORS.TASK_DEFINITIONS.UNRECOGNIZED_NAMED_PARAM, |
| 223 | + { |
| 224 | + parameter: [...providedArgumentNames][0], |
| 225 | + task: formatTaskId(this.id), |
| 226 | + }, |
| 227 | + ); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + /** |
| 232 | + * Resolves the action file for a task. The action file is imported and the |
| 233 | + * default export function is returned. |
| 234 | + * |
| 235 | + * @throws HardhatError if the module can't be imported or doesn't have a |
| 236 | + * default export function. |
| 237 | + */ |
| 238 | + async #resolveFileAction( |
| 239 | + actionFileUrl: string, |
| 240 | + taskId: string[], |
| 241 | + ): Promise<NewTaskActionFunction | TaskOverrideActionFunction> { |
| 242 | + let resolvedActionFn; |
| 243 | + try { |
| 244 | + resolvedActionFn = await import(actionFileUrl); |
| 245 | + } catch (error) { |
| 246 | + ensureError(error); |
| 247 | + throw new HardhatError( |
| 248 | + HardhatError.ERRORS.TASK_DEFINITIONS.INVALID_ACTION_URL, |
| 249 | + { |
| 250 | + action: actionFileUrl, |
| 251 | + task: formatTaskId(taskId), |
| 252 | + }, |
| 253 | + error, |
| 254 | + ); |
| 255 | + } |
| 256 | + |
| 257 | + if (typeof resolvedActionFn.default !== "function") { |
| 258 | + throw new HardhatError( |
| 259 | + HardhatError.ERRORS.TASK_DEFINITIONS.INVALID_ACTION, |
| 260 | + { |
| 261 | + action: actionFileUrl, |
| 262 | + task: formatTaskId(taskId), |
| 263 | + }, |
| 264 | + ); |
87 | 265 | }
|
88 | 266 |
|
89 |
| - void taskArguments; |
90 |
| - void this.#hre; |
| 267 | + return resolvedActionFn.default; |
91 | 268 | }
|
92 | 269 | }
|
0 commit comments