org.robovm.compiler.util.Executor Maven / Gradle / Ivy
/*
* Copyright (C) 2013 Trillian Mobile AB
*
* 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; either version 2
* of the License, or (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.robovm.compiler.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.ExecuteStreamHandler;
import org.apache.commons.exec.PumpStreamHandler;
import org.apache.commons.exec.environment.EnvironmentUtils;
import org.apache.commons.exec.util.StringUtils;
import org.robovm.compiler.log.DebugOutputStream;
import org.robovm.compiler.log.ErrorOutputStream;
import org.robovm.compiler.log.Logger;
import org.robovm.compiler.target.Launcher;
import org.robovm.compiler.util.io.NeverCloseOutputStream;
/**
* Builder style wrapper around commons-exec
which also adds support for asynchronous
* execution.
*/
public class Executor implements Launcher {
private final String cmd;
private final Logger logger;
private List args = new ArrayList();
private Map env = new HashMap();
private boolean inheritEnv = true;
private File wd;
private OutputStream out;
private OutputStream err;
private InputStream in;
private boolean closeOutputStreams = false;
private ExecuteStreamHandler streamHandler = null;
/**
* Creates a new instance which will execute the specified command.
*
* @param logger {@link Logger} used by this {@link Executor}.
* @param cmd the command to be executed. Either the full path to an executable or the name of
* an executable which will be searched for in the search paths specified by the
* PATH
environment variable.
*/
public Executor(Logger logger, String cmd) {
this.logger = logger;
this.cmd = cmd;
}
/**
* Creates a new instance which will execute the specified command.
*
* @param logger {@link Logger} used by this {@link Executor}.
* @param cmd the command to be executed.
*/
public Executor(Logger logger, File cmd) {
this.logger = logger;
this.cmd = cmd.getAbsolutePath();
}
/**
* Adds arguments from the specified {@link Collection}. {@link File} arguments will be
* converted to absolute paths using {@link File#getAbsolutePath()}. All other types of args
* will be converted to {@link String}s using {@link Object#toString()}.
*
* @param args the arguments to add.
* @return this {@link Executor}.
*/
public Executor args(Collection