public RunProfileState getState(
      @NotNull Executor executor, @NotNull ExecutionEnvironment environment)
      throws ExecutionException {
    final VirtualFile script = getScriptFile();
    if (script == null) {
      throw new CantRunException("Cannot find script " + scriptPath);
    }

    final GroovyScriptRunner scriptRunner = findConfiguration();
    if (scriptRunner == null) {
      throw new CantRunException("Unknown script type " + scriptPath);
    }

    final Module module = getModule();
    if (!scriptRunner.ensureRunnerConfigured(module, this, executor, getProject())) {
      return null;
    }

    final boolean tests =
        ProjectRootManager.getInstance(getProject()).getFileIndex().isInTestSourceContent(script);

    final JavaCommandLineState state =
        new JavaCommandLineState(environment) {
          @NotNull
          @Override
          protected OSProcessHandler startProcess() throws ExecutionException {
            final OSProcessHandler handler = super.startProcess();
            handler.setShouldDestroyProcessRecursively(true);
            if (scriptRunner.shouldRefreshAfterFinish()) {
              handler.addProcessListener(
                  new ProcessAdapter() {
                    @Override
                    public void processTerminated(ProcessEvent event) {
                      if (!ApplicationManager.getApplication().isDisposed()) {
                        VirtualFileManager.getInstance().refresh(true);
                      }
                    }
                  });
            }

            return handler;
          }

          protected JavaParameters createJavaParameters() throws ExecutionException {
            JavaParameters params = createJavaParametersWithSdk(module);
            ProgramParametersUtil.configureConfiguration(params, GroovyScriptRunConfiguration.this);
            scriptRunner.configureCommandLine(
                params, module, tests, script, GroovyScriptRunConfiguration.this);

            return params;
          }
        };

    state.setConsoleBuilder(TextConsoleBuilderFactory.getInstance().createBuilder(getProject()));
    return state;
  }
Пример #2
0
  @Override
  public RunProfileState getState(@NotNull Executor executor, @NotNull ExecutionEnvironment env)
      throws ExecutionException {
    final JavaCommandLineState state =
        new JavaApplicationCommandLineState<JavaScratchConfiguration>(this, env) {
          @Override
          protected void setupJavaParameters(JavaParameters params) throws ExecutionException {
            super.setupJavaParameters(params);
            final File scrachesOutput =
                JavaScratchCompilationSupport.getScratchOutputDirectory(getProject());
            if (scrachesOutput != null) {
              params
                  .getClassPath()
                  .addFirst(
                      FileUtil.toCanonicalPath(scrachesOutput.getAbsolutePath())
                          .replace('/', File.separatorChar));
            }
          }

          @NotNull
          @Override
          protected OSProcessHandler startProcess() throws ExecutionException {
            final OSProcessHandler handler = super.startProcess();
            if (getRunnerSettings() instanceof DebuggingRunnerData) {
              final VirtualFile vFile = getConfiguration().getScratchVirtualFile();
              if (vFile != null) {
                DebuggerManager.getInstance(getProject())
                    .addDebugProcessListener(
                        handler,
                        new DebugProcessListener() {
                          @Override
                          public void processAttached(DebugProcess process) {
                            if (vFile.isValid()) {
                              process.appendPositionManager(
                                  new JavaScratchPositionManager(
                                      (DebugProcessImpl) process, vFile));
                            }
                            process.removeDebugProcessListener(this);
                          }
                        });
              }
            }
            return handler;
          }
        };
    state.setConsoleBuilder(
        TextConsoleBuilderFactory.getInstance()
            .createBuilder(getProject(), getConfigurationModule().getSearchScope()));
    return state;
  }