runTask function

Future<TaskResult> runTask(
  1. TaskInvocation invocation, {
  2. required bool runInIsolate,
  3. bool allowIncremental = true,
})

Run a task unconditionally.

The task's Task.runCondition is not checked or used by this method.

Implementation

Future<TaskResult> runTask(TaskInvocation invocation,
    {required bool runInIsolate, bool allowIncremental = true}) async {
  final task = invocation.task;
  final runCondition = task.runCondition;

  final stopwatch = Stopwatch()..start();

  ChangeSet? changeSet;

  if (allowIncremental &&
      runCondition is RunOnChanges &&
      task.action is IncrementalAction) {
    changeSet =
        await _prepareIncrementalAction(stopwatch, task.name, runCondition);
  }

  final action = _createTaskAction(task, runInIsolate && task.isParallelizable);

  logger.log(task.name.startsWith('_') ? Level.FINE : Level.INFO,
      "Running task '${task.name}'");

  stopwatch.reset();

  TaskResult result;
  try {
    final args = invocation.args;
    await action(args, changeSet);
    stopwatch.stop();
    result = TaskResult(invocation);
  } catch (e, st) {
    stopwatch.stop();
    result = TaskResult(invocation,
        ExceptionAndStackTrace(e is Exception ? e : Exception(e), st));

    // other tasks should be cancelled if there's a failure
    currentCancellableContext()?.cancel();
  }
  if (logger.isLoggable(profile)) {
    final completionReason = result.isSuccess
        ? 'successfully'
        : result.isCancelled
            ? 'due to being cancelled'
            : 'with errors';
    logger.log(
        profile,
        "Task '${task.name}' completed "
        '$completionReason in ${elapsedTime(stopwatch)}');
  }
  return result;
}