package org.fluentjava.iwant.api.antrunner;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.fluentjava.iwant.entry.Iwant;
import org.fluentjava.iwant.entry.Iwant.ExitCalledException;

public class AntRunner {

	public static void runAnt(List<File> antJars, File cachedScript,
			String... antArgs) throws Exception {
		final String className = "org.apache.tools.ant.Main";
		List<String> allArgs = new ArrayList<>();
		allArgs.add("-f");
		allArgs.add(cachedScript.getAbsolutePath());
		allArgs.add("-logger");
		allArgs.add(MinimalAntLogger.class.getCanonicalName());
		allArgs.addAll(Arrays.asList(antArgs));
		try {
			Iwant.runJavaMain(true, false, className, antJars,
					allArgs.toArray(new String[0]));
		} catch (InvocationTargetException e) {
			if (e.getCause() instanceof ExitCalledException) {
				ExitCalledException ece = (ExitCalledException) e.getCause();
				if (ece.status() != 0) {
					throw ece;
				} else {
					// ant just feels like trying to kill our JVM,
					// it was still a success
				}
			} else {
				throw asRuntimeException(e.getCause());
			}
		}
	}

	private static RuntimeException asRuntimeException(Throwable e) {
		if (e instanceof RuntimeException) {
			return (RuntimeException) e;
		} else {
			return new RuntimeException(e);
		}
	}

}

@@ -718,16 +718,241 @@
 Args));%0A
+%0A%09%09List%3CFile%3E allJars = new ArrayList%3C%3E();%0A%09%09File loggerLocation = new File(MinimalAntLogger.class%0A%09%09%09%09.getProtectionDomain().getCodeSource().getLocation().toURI());%0A%09%09allJars.addAll(antJars);%0A%09%09allJars.add(loggerLocation);%0A%0A
 %09%09try %7B%0A
@@ -997,18 +997,18 @@
 sName, a
-nt
+ll
 Jars,%0A%09%09

fix effort: give logger location to ant when running