private void executeTask(@NotNull ExternalSystemExecuteTaskTask task,
                           @Nls String executionName,
                           @NotNull ProgressIndicator indicator,
                           @NotNull ExternalSystemProcessHandler processHandler,
                           @Nullable BuildProgressListener progressListener,
                           @NotNull ExternalSystemExecutionConsoleManager<ExecutionConsole, ProcessHandler> consoleManager,
                           @Nullable ExecutionConsole consoleView,
                           @NotNull DefaultBuildDescriptor buildDescriptor,
                           AnAction[] customActions,
                           AnAction[] restartActions,
                           AnAction[] contextActions) {
    if (indicator instanceof ProgressIndicatorEx indicatorEx) {
      indicatorEx.addStateDelegate(new AbstractProgressIndicatorExBase() {
        @Override
        public void cancel() {
          super.cancel();
          task.cancel();
        }
      });
    }
    final String startDateTime = DateFormatUtil.formatTimeWithSeconds(System.currentTimeMillis());
    final String settingsDescription = StringUtil.isEmpty(mySettings.toString()) ? "" : String.format(" '%s'", mySettings);
    final String greeting = ExternalSystemBundle.message("run.text.starting.task", startDateTime, settingsDescription) + "\n";
    processHandler.notifyTextAvailable(greeting + "\n", ProcessOutputTypes.SYSTEM);

    try (BuildEventDispatcher eventDispatcher = new ExternalSystemEventDispatcher(task.getId(), progressListener, false)) {
      ExternalSystemTaskNotificationListener taskListener = new ExternalSystemTaskNotificationListener() {
        @Override
        public void onStart(@NotNull ExternalSystemTaskId id, String workingDir) {
          if (progressListener != null) {
            AnAction rerunTaskAction = new ExternalSystemRunConfiguration.MyTaskRerunAction(progressListener, myEnv, myContentDescriptor);
            BuildViewSettingsProvider viewSettingsProvider =
              consoleView instanceof BuildViewSettingsProvider ?
              new BuildViewSettingsProviderAdapter((BuildViewSettingsProvider)consoleView) : null;
            buildDescriptor
              .withProcessHandler(processHandler, view -> ExternalSystemRunConfiguration
                .foldGreetingOrFarewell(consoleView, greeting, true))
              .withContentDescriptor(() -> myContentDescriptor)
              .withActions(customActions)
              .withRestartAction(rerunTaskAction)
              .withRestartActions(restartActions)
              .withContextActions(contextActions)
              .withExecutionEnvironment(myEnv);
            progressListener.onEvent(id,
                                     new StartBuildEventImpl(buildDescriptor, BuildBundle.message("build.status.running"))
                                       .withBuildViewSettingsProvider(viewSettingsProvider));
          }
        }

        @Override
        public void onTaskOutput(@NotNull ExternalSystemTaskId id, @NotNull String text, boolean stdOut) {
          if (consoleView != null) {
            consoleManager.onOutput(consoleView, processHandler, text, stdOut ? ProcessOutputTypes.STDOUT : ProcessOutputTypes.STDERR);
          }
          else {
            processHandler.notifyTextAvailable(text, stdOut ? ProcessOutputTypes.STDOUT : ProcessOutputTypes.STDERR);
          }
          eventDispatcher.setStdOut(stdOut);
          eventDispatcher.append(text);
        }

        @Override
        public void onFailure(@NotNull ExternalSystemTaskId id, @NotNull Exception e) {
          if (progressListener != null) {
            var eventTime = System.currentTimeMillis();
            var eventMessage = BuildBundle.message("build.status.failed");
            var title = executionName + " " + BuildBundle.message("build.status.failed");
            var externalSystemId = id.getProjectSystemId();
            var externalProjectPath = mySettings.getExternalProjectPath();
            var dataContext = BuildConsoleUtils.getDataContext(id, progressListener, consoleView);
            var eventResult = createFailureResult(title, e, externalSystemId, myProject, externalProjectPath, dataContext);
            eventDispatcher.onEvent(id, new FinishBuildEventImpl(id, null, eventTime, eventMessage, eventResult));
          }
          processHandler.notifyProcessTerminated(1);
        }

        @Override
        public void onCancel(@NotNull ExternalSystemTaskId id) {
          eventDispatcher.onEvent(id, new FinishBuildEventImpl(id, null, System.currentTimeMillis(),
                                                               BuildBundle.message("build.status.cancelled"), new FailureResultImpl()));
          processHandler.notifyProcessTerminated(1);
        }

        @Override
        public void onSuccess(@NotNull ExternalSystemTaskId id) {
          eventDispatcher.onEvent(id, new FinishBuildEventImpl(
            id, null, System.currentTimeMillis(), BuildBundle.message("build.event.message.successful"), new SuccessResultImpl()));
        }

        @Override
        public void onStatusChange(@NotNull ExternalSystemTaskNotificationEvent event) {
          if (consoleView != null) {
            consoleManager.onStatusChange(consoleView, event);
          }
          if (event instanceof ExternalSystemBuildEvent) {
            eventDispatcher.onEvent(event.getId(), ((ExternalSystemBuildEvent)event).getBuildEvent());
          }
          else if (event instanceof ExternalSystemTaskExecutionEvent) {
            BuildEvent buildEvent = convert(((ExternalSystemTaskExecutionEvent)event));
            eventDispatcher.onEvent(event.getId(), buildEvent);
          }
        }

        @Override
        public void onEnd(@NotNull ExternalSystemTaskId id) {
          final String endDateTime = DateFormatUtil.formatTimeWithSeconds(System.currentTimeMillis());
          final String farewell = ExternalSystemBundle.message("run.text.ended.task", endDateTime, settingsDescription);
          processHandler.notifyTextAvailable(farewell + "\n", ProcessOutputTypes.SYSTEM);
          ExternalSystemRunConfiguration.foldGreetingOrFarewell(consoleView, farewell, false);
          processHandler.notifyProcessTerminated(0);
        }

        @Override
        public void onEnvironmentPrepared(@NotNull ExternalSystemTaskId id) {
          RunConfigurationTaskState taskState = myConfiguration.getUserData(RunConfigurationTaskState.getKEY());
          if (taskState != null && consoleView != null) {
            taskState.processExecutionResult(processHandler, consoleView);
          }
        }
      };
      task.execute(indicator, taskListener);
      Throwable taskError = task.getError();
      if (taskError != null && !(taskError instanceof Exception)) {
        FinishBuildEventImpl failureEvent = new FinishBuildEventImpl(task.getId(), null, System.currentTimeMillis(),
                                                                     BuildBundle.message("build.status.failed"),
                                                                     new FailureResultImpl(taskError));
        eventDispatcher.onEvent(task.getId(), failureEvent);
      }
    }
  }
private void executeTask(@NotNull ExternalSystemExecuteTaskTask task,
                           @Nls String executionName,
                           @NotNull ProgressIndicator indicator,
                           @NotNull ExternalSystemProcessHandler processHandler,
                           @Nullable BuildProgressListener progressListener,
                           @NotNull ExternalSystemExecutionConsoleManager<ExecutionConsole, ProcessHandler> consoleManager,
                           @Nullable ExecutionConsole consoleView,
                           @NotNull DefaultBuildDescriptor buildDescriptor,
                           AnAction[] customActions,
                           AnAction[] restartActions,
                           AnAction[] contextActions)