view test/org/tmatesoft/hg/test/ExecHelper.java @ 587:a52f4cc56f9c

Minimize vectors re-allocating when merging patches
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 26 Apr 2013 20:04:17 +0200
parents 36853bb80a35
children
line wrap: on
line source
/*
 * Copyright (c) 2011-2013 TMate Software Ltd
 *  
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * For information on how to redistribute this software under
 * the terms of a license other than GNU General Public License
 * contact TMate Software at support@hg4j.com
 */
package org.tmatesoft.hg.test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

import org.tmatesoft.hg.internal.ProcessExecHelper;

/**
 * Utility to run a command line application and to parse its output
 * @author Artem Tikhomirov
 * @author TMate Software Ltd.
 */
public class ExecHelper extends ProcessExecHelper {

	private final OutputParser parser;

	public ExecHelper(OutputParser outParser, File workingDir) {
		parser = outParser;
		super.cwd(workingDir);
	}
	
	@Override
	protected List<String> prepareCommand(List<String> cmd) {
		String commandName = cmd.get(0);
		if (System.getProperty("os.name").startsWith("Windows")) {
			StringTokenizer st = new StringTokenizer(System.getenv("PATH"), ";");
			while (st.hasMoreTokens()) {
				File pe = new File(st.nextToken());
				if (new File(pe, commandName + ".exe").exists()) {
					return cmd;
				}
				// PATHEXT controls precedence of .exe, .bat and .cmd files, usually .exe wins
				if (new File(pe, commandName + ".bat").exists() || new File(pe, commandName + ".cmd").exists()) {
					ArrayList<String> command = new ArrayList<String>();
					command.add("cmd.exe");
					command.add("/C");
					command.addAll(cmd);
					return command;
				}
			}
		}
		return super.prepareCommand(cmd);
	}
	
	public void run(List<String> cmd) throws IOException, InterruptedException {
		CharSequence res = super.exec(cmd);
		parser.parse(res);
	}

	public void run(String... cmd) throws IOException, InterruptedException {
		run(Arrays.asList(cmd));
	}

	public int getExitValue() {
		return super.exitValue();
	}
}