public final boolean executeTask()

in plugin_dev/src/com/google/idea/blaze/plugin/run/BuildPluginBeforeRunTaskProvider.java [147:299]


  public final boolean executeTask(
      final DataContext dataContext,
      final RunConfiguration configuration,
      final ExecutionEnvironment env,
      Task task) {
    if (!canExecuteTask(configuration, task)) {
      return false;
    }
    BlazeUserSettings userSettings = BlazeUserSettings.getInstance();
    return Scope.root(
        context -> {
          WorkspaceRoot workspaceRoot = WorkspaceRoot.fromProject(project);
          context
              .push(new ExperimentScope())
              .push(new ProblemsViewScope(project, userSettings.getShowProblemsViewOnRun()))
              .push(
                  new BlazeConsoleScope.Builder(project)
                      .setPopupBehavior(userSettings.getShowBlazeConsoleOnRun())
                      .addConsoleFilters(
                          new IssueOutputFilter(
                              project, workspaceRoot, ContextType.RunConfiguration, true))
                      .build())
              .push(new IdeaLogScope());

          BlazeIntellijPluginDeployer deployer =
              env.getUserData(BlazeIntellijPluginDeployer.USER_DATA_KEY);
          if (deployer == null) {
            IssueOutput.error("Could not find BlazeIntellijPluginDeployer in env.").submit(context);
            return false;
          }
          deployer.buildStarted();

          final ProjectViewSet projectViewSet =
              ProjectViewManager.getInstance(project).getProjectViewSet();
          if (projectViewSet == null) {
            IssueOutput.error("Could not load project view. Please resync project").submit(context);
            return false;
          }

          final ScopedTask<Void> buildTask =
              new ScopedTask<Void>(context) {
                @Override
                protected Void execute(BlazeContext context) {
                  String binaryPath = Blaze.getBuildSystemProvider(project).getBinaryPath(project);
                  BlazeIntellijPluginConfiguration config =
                      (BlazeIntellijPluginConfiguration) configuration;

                  ListenableFuture<String> executionRootFuture =
                      BlazeInfoRunner.getInstance()
                          .runBlazeInfo(
                              context,
                              binaryPath,
                              workspaceRoot,
                              config.getBlazeFlagsState().getFlagsForExternalProcesses(),
                              BlazeInfo.EXECUTION_ROOT_KEY);

                  String executionRoot;
                  try {
                    executionRoot = executionRootFuture.get();
                  } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    context.setCancelled();
                    return null;
                  } catch (ExecutionException e) {
                    IssueOutput.error(e.getMessage()).submit(context);
                    context.setHasError();
                    return null;
                  }
                  if (executionRoot == null) {
                    IssueOutput.error("Could not determine execution root").submit(context);
                    return null;
                  }
                  BlazeProjectData blazeProjectData =
                      BlazeProjectDataManager.getInstance(project).getBlazeProjectData();
                  if (blazeProjectData == null) {
                    IssueOutput.error("Could not determine execution root").submit(context);
                    return null;
                  }

                  // Explicitly create a local build helper because deployer.reportBuildComplete
                  // expects the outputs to be available locally
                  try (BuildResultHelper buildResultHelper =
                      BuildResultHelperProvider.createForLocalBuild(project)) {
                    BlazeCommand command =
                        BlazeCommand.builder(binaryPath, BlazeCommandName.BUILD)
                            .addTargets(config.getTargets())
                            .addBlazeFlags(
                                BlazeFlags.blazeFlags(
                                    project,
                                    projectViewSet,
                                    BlazeCommandName.BUILD,
                                    BlazeInvocationContext.runConfigContext(
                                        ExecutorType.fromExecutor(env.getExecutor()),
                                        config.getType(),
                                        true)))
                            .addBlazeFlags(
                                config.getBlazeFlagsState().getFlagsForExternalProcesses())
                            .addExeFlags(config.getExeFlagsState().getFlagsForExternalProcesses())
                            .addBlazeFlags(buildResultHelper.getBuildFlags())
                            .build();
                    if (command == null || context.hasErrors() || context.isCancelled()) {
                      return null;
                    }
                    SaveUtil.saveAllFiles();
                    int retVal =
                        ExternalTask.builder(workspaceRoot)
                            .addBlazeCommand(command)
                            .context(context)
                            .stderr(
                                LineProcessingOutputStream.of(
                                    BlazeConsoleLineProcessorProvider.getAllStderrLineProcessors(
                                        context)))
                            .build()
                            .run();
                    if (retVal != 0) {
                      context.setHasError();
                    }
                    ListenableFuture<Void> unusedFuture =
                        FileCaches.refresh(
                            project,
                            context,
                            BlazeBuildOutputs.noOutputs(BuildResult.fromExitCode(retVal)));
                    try {
                      deployer.reportBuildComplete(new File(executionRoot), buildResultHelper);
                    } catch (GetArtifactsException e) {
                      IssueOutput.error("Failed to get build artifacts: " + e.getMessage())
                          .submit(context);
                      return null;
                    }
                    return null;
                  }
                }
              };

          ListenableFuture<Void> buildFuture =
              ProgressiveTaskWithProgressIndicator.builder(
                      project, "Executing blaze build for IntelliJ plugin jar")
                  .submitTaskWithResult(buildTask);

          try {
            Futures.getChecked(buildFuture, ExecutionException.class);
          } catch (ExecutionException e) {
            context.setHasError();
          } catch (CancellationException e) {
            context.setCancelled();
          }

          if (context.hasErrors() || context.isCancelled()) {
            return false;
          }
          return true;
        });
  }