コード例 #1
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();
    }
  }