<commit_before>/*
 * Copyright 2000-2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jonnyzzz.teamcity.virtual.run;

import com.jonnyzzz.teamcity.virtual.VMConstants;
import com.jonnyzzz.teamcity.virtual.util.util.BuildProcessBase;
import com.jonnyzzz.teamcity.virtual.util.util.CommandlineBuildProcessFactory;
import com.jonnyzzz.teamcity.virtual.util.util.CompositeBuildProcess;
import com.jonnyzzz.teamcity.virtual.util.util.TryFinallyBuildProcessImpl;
import com.jonnyzzz.teamcity.virtual.util.util.impl.CompositeBuildProcessImpl;
import jetbrains.buildServer.RunBuildException;
import jetbrains.buildServer.agent.BuildFinishedStatus;
import jetbrains.buildServer.agent.BuildProcess;
import jetbrains.buildServer.agent.BuildRunnerContext;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.Collection;
import java.util.Collections;

import static jetbrains.buildServer.agent.BuildFinishedStatus.FINISHED_SUCCESS;

/**
 * @author <NAME> (<EMAIL>)
 */
public class VMRunnerFactory {
  private final VMRunner[] myRunners;
  private CommandlineBuildProcessFactory myCmd;

  public VMRunnerFactory(@NotNull final VMRunner[] runners, @NotNull final CommandlineBuildProcessFactory cmd) {
    myRunners = runners;
    myCmd = cmd;
  }

  @NotNull
  public BuildProcess createBuildRunnerProcess(@NotNull final BuildRunnerContext context) throws RunBuildException {
    final CompositeBuildProcess proc = new CompositeBuildProcessImpl();

    final VMRunner runner = getVMBuildProcess(context);

    proc.pushBuildProcess(new BuildProcessBase() {
      @NotNull
      @Override
      protected BuildFinishedStatus waitForImpl() throws RunBuildException {
        context.getBuild().getBuildLogger().message("Staring " + runner.getCaption() + "\u2026");
        return FINISHED_SUCCESS;
      }
    });

    final CommandlineExecutor exec = new CommandlineExecutor() {
      @NotNull
      @Override
      public BuildProcess commandline(@NotNull final File workdir, @NotNull Collection<String> arguments) throws RunBuildException {
        return myCmd.executeCommandLine(context, arguments, workdir, Collections.<String, String>emptyMap());
      }
    };

    proc.pushBuildProcess(new BuildProcessBase() {
      @NotNull
      @Override
      protected BuildFinishedStatus waitForImpl() throws RunBuildException {
        final TryFinallyBuildProcessImpl tf = new TryFinallyBuildProcessImpl(context.getBuild().getBuildLogger());
        runner.constructBuildProcess(context, exec, tf);
        proc.pushBuildProcess(tf.asBuildProcess());
        return FINISHED_SUCCESS;
      }
    });

    return proc;
  }
<commit_msg>wrap in delegating build process to ensure proper execution order<commit_after>/*
 * Copyright 2000-2014 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jonnyzzz.teamcity.virtual.run;

import com.jonnyzzz.teamcity.virtual.VMConstants;
import com.jonnyzzz.teamcity.virtual.util.util.*;
import com.jonnyzzz.teamcity.virtual.util.util.impl.CompositeBuildProcessImpl;
import jetbrains.buildServer.RunBuildException;
import jetbrains.buildServer.agent.BuildFinishedStatus;
import jetbrains.buildServer.agent.BuildProcess;
import jetbrains.buildServer.agent.BuildRunnerContext;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.Collection;
import java.util.Collections;

import static jetbrains.buildServer.agent.BuildFinishedStatus.FINISHED_SUCCESS;

/**
 * @author <NAME> (<EMAIL>)
 */
public class VMRunnerFactory {
  private final VMRunner[] myRunners;
  private CommandlineBuildProcessFactory myCmd;

  public VMRunnerFactory(@NotNull final VMRunner[] runners, @NotNull final CommandlineBuildProcessFactory cmd) {
    myRunners = runners;
    myCmd = cmd;
  }

  @NotNull
  public BuildProcess createBuildRunnerProcess(@NotNull final BuildRunnerContext context) throws RunBuildException {
    final CompositeBuildProcess proc = new CompositeBuildProcessImpl();

    final VMRunner runner = getVMBuildProcess(context);

    proc.pushBuildProcess(new BuildProcessBase() {
      @NotNull
      @Override
      protected BuildFinishedStatus waitForImpl() throws RunBuildException {
        context.getBuild().getBuildLogger().message("Staring " + runner.getCaption() + "\u2026");
        return FINISHED_SUCCESS;
      }
    });

    final CommandlineExecutor exec = new CommandlineExecutor() {
      @NotNull
      @Override
      public BuildProcess commandline(@NotNull final File workdir, @NotNull final Collection<String> arguments) throws RunBuildException {
        return new DelegatingBuildProcess(new DelegatingBuildProcess.Action() {
          @NotNull
          @Override
          public BuildProcess startImpl() throws RunBuildException {
            return myCmd.executeCommandLine(context, arguments, workdir, Collections.<String, String>emptyMap());
          }

          @Override
          public void finishedImpl() {

          }
        });
      }
    };

    proc.pushBuildProcess(new BuildProcessBase() {
      @NotNull
      @Override
      protected BuildFinishedStatus waitForImpl() throws RunBuildException {
        final TryFinallyBuildProcessImpl tf = new TryFinallyBuildProcessImpl(context.getBuild().getBuildLogger());
        runner.constructBuildProcess(context, exec, tf);
        proc.pushBuildProcess(tf.asBuildProcess());
        return FINISHED_SUCCESS;
      }
    });

    return proc;
  }

{'fraction_non_alphanumeric': 0.08482694684796044, 'fraction_numerical': 0.004944375772558714, 'mean_word_length': 4.215954875100725, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5011599', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1694, 'n_words': 457}