public static void executeConfiguration(
      @NotNull Project project,
      @NotNull RunnerAndConfigurationSettings configuration,
      @NotNull Executor executor) {
    ExecutionEnvironmentBuilder builder;
    try {
      builder = ExecutionEnvironmentBuilder.create(executor, configuration);
    } catch (ExecutionException e) {
      LOG.error(e);
      return;
    }

    executeConfiguration(
        builder.contentToReuse(null).dataContext(null).activeTarget().build(), true, true);
  }
 private static JavaParameters checkCanRun(RunConfiguration configuration)
     throws ExecutionException {
   final RunProfileState state;
   state =
       ExecutionEnvironmentBuilder.create(
               DefaultRunExecutor.getRunExecutorInstance(), configuration)
           .build()
           .getState();
   assertNotNull(state);
   assertTrue(state instanceof JavaCommandLine);
   if (state instanceof TestPackage) {
     @SuppressWarnings("UnusedDeclaration")
     final JavaParameters parameters = ((TestPackage) state).getJavaParameters();
     final SearchForTestsTask task = ((TestPackage) state).createSearchingForTestsTask();
     assertNotNull(task);
     task.startSearch();
   }
   try {
     configuration.checkConfiguration();
   } catch (RuntimeConfigurationError e) {
     fail("cannot run: " + e.getMessage());
   } catch (RuntimeConfigurationException e) {
     // ignore
   }
   return ((JavaCommandLine) state).getJavaParameters();
 }
 private static void performAction(@NotNull ExecutionEnvironmentBuilder builder) {
   ExecutionEnvironment environment = builder.build();
   try {
     environment.getRunner().execute(environment);
   } catch (ExecutionException e) {
     LOG.error(e);
   } finally {
     ((MyRunProfile) environment.getRunProfile()).clear();
   }
 }
  @NotNull
  private static ExecutionEnvironmentBuilder createEnvironmentBuilder(
      @NotNull Project project,
      @NotNull Executor executor,
      @Nullable RunnerAndConfigurationSettings configuration) {
    ExecutionEnvironmentBuilder builder = new ExecutionEnvironmentBuilder(project, executor);

    ProgramRunner runner =
        RunnerRegistry.getInstance()
            .getRunner(
                executor.getId(), configuration != null ? configuration.getConfiguration() : null);
    if (runner == null && configuration != null) {
      LOG.error("Cannot find runner for " + configuration.getName());
    } else if (runner != null) {
      assert configuration != null;
      builder.runnerAndSettings(runner, configuration);
    }
    return builder;
  }
 @Override
 public void restartRunProfile(
     @NotNull Project project,
     @NotNull Executor executor,
     @NotNull ExecutionTarget target,
     @Nullable RunnerAndConfigurationSettings configuration,
     @Nullable ProcessHandler processHandler) {
   ExecutionEnvironmentBuilder builder =
       createEnvironmentBuilder(project, executor, configuration);
   if (processHandler != null) {
     for (RunContentDescriptor descriptor : getContentManager().getAllDescriptors()) {
       if (descriptor.getProcessHandler() == processHandler) {
         builder.contentToReuse(descriptor);
         break;
       }
     }
   }
   restartRunProfile(builder.target(target).build());
 }
 @Override
 public void actionPerformed(AnActionEvent e) {
   final Project project = e.getProject();
   LOG.assertTrue(project != null);
   final VirtualFile file = getFile(project);
   if (file != null) {
     try {
       final ImportRunProfile profile = new ImportRunProfile(file, project);
       SMTRunnerConsoleProperties properties = profile.getProperties();
       if (properties == null) {
         properties = myProperties;
         LOG.info(
             "Failed to detect test framework in "
                 + file.getPath()
                 + "; use "
                 + (properties != null
                     ? properties.getTestFrameworkName() + " from toolbar"
                     : "no properties"));
       }
       final Executor executor =
           properties != null
               ? properties.getExecutor()
               : ExecutorRegistry.getInstance().getExecutorById(DefaultRunExecutor.EXECUTOR_ID);
       ExecutionEnvironmentBuilder builder =
           ExecutionEnvironmentBuilder.create(project, executor, profile);
       final RunConfiguration initialConfiguration = profile.getInitialConfiguration();
       final ProgramRunner runner =
           initialConfiguration != null
               ? RunnerRegistry.getInstance().getRunner(executor.getId(), initialConfiguration)
               : null;
       if (runner != null) {
         builder = builder.runner(runner);
       }
       builder.buildAndExecute();
     } catch (ExecutionException e1) {
       Messages.showErrorDialog(project, e1.getMessage(), "Import Failed");
     }
   }
 }
  void execute(@NotNull AnActionEvent e, @NotNull ExecutionEnvironment environment) {
    MyRunProfile profile = getRunProfile(environment);
    if (profile == null) {
      return;
    }

    final ExecutionEnvironmentBuilder environmentBuilder =
        new ExecutionEnvironmentBuilder(environment).runProfile(profile);

    final InputEvent event = e.getInputEvent();
    if (!(event instanceof MouseEvent) || !event.isShiftDown()) {
      performAction(environmentBuilder);
      return;
    }

    final LinkedHashMap<Executor, ProgramRunner> availableRunners = new LinkedHashMap<>();
    for (Executor ex :
        new Executor[] {
          DefaultRunExecutor.getRunExecutorInstance(),
          DefaultDebugExecutor.getDebugExecutorInstance()
        }) {
      final ProgramRunner runner = RunnerRegistry.getInstance().getRunner(ex.getId(), profile);
      if (runner != null) {
        availableRunners.put(ex, runner);
      }
    }

    if (availableRunners.isEmpty()) {
      LOG.error(environment.getExecutor().getActionName() + " is not available now");
    } else if (availableRunners.size() == 1) {
      //noinspection ConstantConditions
      performAction(environmentBuilder.runner(availableRunners.get(environment.getExecutor())));
    } else {
      final JBList list = new JBList(availableRunners.keySet());
      list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      list.setSelectedValue(environment.getExecutor(), true);
      list.setCellRenderer(
          new DefaultListCellRenderer() {
            @NotNull
            @Override
            public Component getListCellRendererComponent(
                @NotNull JList list,
                Object value,
                int index,
                boolean isSelected,
                boolean cellHasFocus) {
              final Component component =
                  super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
              if (value instanceof Executor) {
                setText(UIUtil.removeMnemonic(((Executor) value).getStartActionText()));
                setIcon(((Executor) value).getIcon());
              }
              return component;
            }
          });
      //noinspection ConstantConditions
      JBPopupFactory.getInstance()
          .createListPopupBuilder(list)
          .setTitle("Restart Failed Tests")
          .setMovable(false)
          .setResizable(false)
          .setRequestFocus(true)
          .setItemChoosenCallback(
              () -> {
                final Object value = list.getSelectedValue();
                if (value instanceof Executor) {
                  //noinspection ConstantConditions
                  performAction(
                      environmentBuilder
                          .runner(availableRunners.get(value))
                          .executor((Executor) value));
                }
              })
          .createPopup()
          .showUnderneathOf(event.getComponent());
    }
  }
Beispiel #8
0
  @Override
  public boolean executeTask(
      final DataContext dataContext,
      RunConfiguration configuration,
      final ExecutionEnvironment env,
      RunConfigurableBeforeRunTask task) {
    RunnerAndConfigurationSettings settings = task.getSettings();
    if (settings == null) {
      return false;
    }
    final Executor executor = DefaultRunExecutor.getRunExecutorInstance();
    final String executorId = executor.getId();
    ExecutionEnvironmentBuilder builder =
        ExecutionEnvironmentBuilder.createOrNull(executor, settings);
    if (builder == null) {
      return false;
    }
    final ExecutionEnvironment environment = builder.build();
    environment.setExecutionId(env.getExecutionId());
    if (!ExecutionTargetManager.canRun(settings, env.getExecutionTarget())) {
      return false;
    }

    if (!environment.getRunner().canRun(executorId, environment.getRunProfile())) {
      return false;
    } else {
      final Semaphore targetDone = new Semaphore();
      final Ref<Boolean> result = new Ref<Boolean>(false);
      final Disposable disposable = Disposer.newDisposable();

      myProject
          .getMessageBus()
          .connect(disposable)
          .subscribe(
              ExecutionManager.EXECUTION_TOPIC,
              new ExecutionAdapter() {
                @Override
                public void processStartScheduled(
                    final String executorIdLocal, final ExecutionEnvironment environmentLocal) {
                  if (executorId.equals(executorIdLocal) && environment.equals(environmentLocal)) {
                    targetDone.down();
                  }
                }

                @Override
                public void processNotStarted(
                    final String executorIdLocal,
                    @NotNull final ExecutionEnvironment environmentLocal) {
                  if (executorId.equals(executorIdLocal) && environment.equals(environmentLocal)) {
                    targetDone.up();
                  }
                }

                @Override
                public void processStarted(
                    final String executorIdLocal,
                    @NotNull final ExecutionEnvironment environmentLocal,
                    @NotNull final ProcessHandler handler) {
                  if (executorId.equals(executorIdLocal) && environment.equals(environmentLocal)) {
                    handler.addProcessListener(
                        new ProcessAdapter() {
                          @Override
                          public void processTerminated(ProcessEvent event) {
                            result.set(event.getExitCode() == 0);
                            targetDone.up();
                          }
                        });
                  }
                }
              });

      try {
        ApplicationManager.getApplication()
            .invokeAndWait(
                new Runnable() {
                  @Override
                  public void run() {
                    try {
                      environment.getRunner().execute(environment);
                    } catch (ExecutionException e) {
                      targetDone.up();
                      LOG.error(e);
                    }
                  }
                },
                ModalityState.NON_MODAL);
      } catch (Exception e) {
        LOG.error(e);
        Disposer.dispose(disposable);
        return false;
      }

      targetDone.waitFor();
      Disposer.dispose(disposable);

      return result.get();
    }
  }