Ejemplo n.º 1
0
 @Nullable
 @Override
 public ExecutionResult execute(Executor executor, @NotNull ProgramRunner programRunner)
     throws ExecutionException {
   stopRunningMultirunConfiguration.beginStaringConfigurations();
   runConfigurations(executor, runConfigurations, 0);
   return null;
 }
Ejemplo n.º 2
0
  private void runConfigurations(
      final Executor executor, final List<RunConfiguration> runConfigurations, final int index) {
    if (index >= runConfigurations.size()) {
      stopRunningMultirunConfiguration.doneStaringConfigurations();
      return;
    }
    if (!stopRunningMultirunConfiguration.canContinueStartingConfigurations()) {
      stopRunningMultirunConfiguration.doneStaringConfigurations();
      // don't start more configurations if user stopped the plugin work.
      return;
    }

    final RunConfiguration runConfiguration = runConfigurations.get(index);
    final Project project = runConfiguration.getProject();
    final RunnerAndConfigurationSettings configuration =
        new RunnerAndConfigurationSettingsImpl(
            RunManagerImpl.getInstanceImpl(project), runConfiguration, false);

    boolean started = false;
    try {
      ProgramRunner runner =
          RunnerRegistry.getInstance().getRunner(executor.getId(), runConfiguration);
      if (runner == null) return;
      if (!checkRunConfiguration(executor, project, configuration)) return;

      runTriggers(executor, configuration);
      RunContentDescriptor runContentDescriptor =
          getRunContentDescriptor(runConfiguration, project);
      ExecutionEnvironment executionEnvironment =
          new ExecutionEnvironment(
              runner,
              DefaultExecutionTarget.INSTANCE,
              configuration,
              runContentDescriptor,
              project);

      runner.execute(
          executor,
          executionEnvironment,
          new ProgramRunner.Callback() {
            @SuppressWarnings("ConstantConditions")
            @Override
            public void processStarted(final RunContentDescriptor descriptor) {
              if (descriptor == null) {
                if (startOneByOne) {
                  // start next configuration..
                  runConfigurations(executor, runConfigurations, index + 1);
                }
                return;
              }

              final ProcessHandler processHandler = descriptor.getProcessHandler();
              if (processHandler != null) {
                processHandler.addProcessListener(
                    new ProcessAdapter() {
                      @SuppressWarnings("ConstantConditions")
                      @Override
                      public void startNotified(ProcessEvent processEvent) {
                        Content content = descriptor.getAttachedContent();
                        if (content != null) {
                          content.setIcon(descriptor.getIcon());
                          if (!stopRunningMultirunConfiguration
                              .canContinueStartingConfigurations()) {
                            // Multirun was stopped - destroy processes that are still starting up
                            processHandler.destroyProcess();

                            if (!content.isPinned() && !startOneByOne) {
                              // checks if not pinned, to avoid destroying already existed tab
                              // checks if start one by one - no need to close the console tab, as
                              // it's won't be shown
                              // as other checks disallow starting it

                              // content.getManager() can be null, if content is removed already as
                              // part of destroy above
                              if (content.getManager() != null) {
                                content.getManager().removeContent(content, false);
                              }
                            }
                          } else {
                            // mark all current console tab as pinned
                            content.setPinned(true);

                            // mark running process tab with *
                            content.setDisplayName(descriptor.getDisplayName() + "*");
                          }
                        }
                      }

                      @Override
                      public void processTerminated(final ProcessEvent processEvent) {
                        onTermination(processEvent, true);
                      }

                      @Override
                      public void processWillTerminate(
                          ProcessEvent processEvent, boolean willBeDestroyed) {
                        onTermination(processEvent, false);
                      }

                      private void onTermination(
                          final ProcessEvent processEvent, final boolean terminated) {
                        if (descriptor.getAttachedContent() == null) {
                          return;
                        }

                        LaterInvocator.invokeLater(
                            new Runnable() {
                              @Override
                              public void run() {
                                final Content content = descriptor.getAttachedContent();
                                if (content == null) return;

                                // exit code is 0 if the process completed successfully
                                final boolean completedSuccessfully =
                                    (terminated && processEvent.getExitCode() == 0);

                                if (hideSuccessProcess && completedSuccessfully) {
                                  // close the tab for the success process and exit - nothing else
                                  // could be done
                                  if (content.getManager() != null) {
                                    content.getManager().removeContent(content, false);
                                    return;
                                  }
                                }

                                if (!separateTabs && completedSuccessfully) {
                                  // un-pin the console tab if re-use is allowed and process
                                  // completed successfully,
                                  // so the tab could be re-used for other processes
                                  content.setPinned(false);
                                }

                                // remove the * used to identify running process
                                content.setDisplayName(descriptor.getDisplayName());

                                // add the alert icon in case if process existed with non-0 status
                                if (markFailedProcess && processEvent.getExitCode() != 0) {
                                  LaterInvocator.invokeLater(
                                      new Runnable() {
                                        @Override
                                        public void run() {
                                          content.setIcon(
                                              LayeredIcon.create(
                                                  content.getIcon(), AllIcons.Nodes.TabAlert));
                                        }
                                      });
                                }
                              }
                            });
                      }
                    });
              }
              stopRunningMultirunConfiguration.addProcess(project, processHandler);

              if (startOneByOne) {
                // start next configuration..
                runConfigurations(executor, runConfigurations, index + 1);
              }
            }
          });
      started = true;
    } catch (ExecutionException e) {
      ExecutionUtil.handleExecutionError(
          project, executor.getToolWindowId(), configuration.getConfiguration(), e);
    } finally {
      // start the next one
      if (!startOneByOne) {
        runConfigurations(executor, runConfigurations, index + 1);
      } else if (!started) {
        // failed to start current, means the chain is broken
        runConfigurations(executor, runConfigurations, index + 1);
      }
    }
  }