view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BaseCommand.java @ 179:2b0eafc9bcf5 remote-run/dmitry.neverov/final_agent_caches

Mercurial mirrors on agent
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Tue, 01 Mar 2011 16:47:00 +0300
parents 5198b02fc5e9
children 64f1902552cc
line wrap: on
line source
/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * 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 jetbrains.buildServer.buildTriggers.vcs.mercurial.command;

import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.openapi.util.SystemInfo;
import jetbrains.buildServer.ExecResult;
import jetbrains.buildServer.util.StringUtil;
import jetbrains.buildServer.vcs.VcsException;
import org.jetbrains.annotations.NotNull;

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

/**
 * @author pavel
 */
public class BaseCommand {
  private Settings mySettings;
  private String myWorkDirectory;

  public BaseCommand(@NotNull final Settings settings, @NotNull File workingDir) {
    mySettings = settings;
    myWorkDirectory = workingDir.getAbsolutePath();
  }


  public Settings getSettings() {
    return mySettings;
  }

  /**
   * Sets new working directory, by default working directory is taken from the Settings#getLocalRepositoryDir
   * @param workDirectory work dir
   */
  public void setWorkDirectory(final String workDirectory) {
    myWorkDirectory = workDirectory;
  }

  protected GeneralCommandLine createCommandLine() {
    GeneralCommandLine cli = new GeneralCommandLine();
    setupExecutable(cli);
    cli.setWorkDirectory(myWorkDirectory);
    cli.setPassParentEnvs(true);
    return cli;
  }

  /**
   * Since mercurial 1.7 on Windows the only file inside '<mercurial_install_dir>/bin' is 'hg.cmd' 
   * which run hg.exe placed in the parent dir. GeneralCommandLine will not find hg.cmd, in the 
   * case when $PATH contains <mercurial_install_dir>/bin and doesn't contain <mercurial_install_dir> 
   * and hg executable is set to 'hg'. To fix it - run hg using windows shell which expand
   * hg to hg.cmd correctly.
   */
  private void setupExecutable(GeneralCommandLine cli) {
    if (SystemInfo.isWindows && getSettings().getHgCommandPath().equals("hg")) {
      setupCmd(cli);
    } else {
      setupHg(cli);
    }
  }

  private void setupCmd(GeneralCommandLine cli) {
    cli.setExePath("cmd");
    cli.addParameter("/c");
    cli.addParameter("hg");
  }

  private void setupHg(GeneralCommandLine cli) {
    cli.setExePath(getSettings().getHgCommandPath());
  }

  protected ExecResult runCommand(@NotNull GeneralCommandLine cli) throws VcsException {
    return CommandUtil.runCommand(cli, getPrivateData());
  }

  protected ExecResult runCommand(@NotNull GeneralCommandLine cli, int executionTimeout) throws VcsException {
    return CommandUtil.runCommand(cli, executionTimeout, getPrivateData());
  }

  protected void failIfNotEmptyStdErr(@NotNull GeneralCommandLine cli, @NotNull ExecResult res) throws VcsException {
    if (!StringUtil.isEmpty(res.getStderr())) {
      CommandUtil.commandFailed(cli.getCommandLineString(), res);
    }
  }

  protected void failIfNonZeroExitCode(@NotNull GeneralCommandLine cli, @NotNull ExecResult res) throws VcsException {
    if (res.getExitCode() != 0) {
      CommandUtil.commandFailed(cli.getCommandLineString(), res);
    }
  }

  public Set<String> getPrivateData() {
    return Collections.singleton(mySettings.getPassword());
  }
}