package jetbrains.mps.build.ant.converter;
/*Generated by MPS */
import org.apache.tools.ant.taskdefs.Copy;
import java.util.Map;
import java.util.HashMap;
import java.io.File;
import org.apache.tools.ant.util.FirstMatchMapper;
import org.apache.tools.ant.util.GlobPatternMapper;
import org.apache.tools.ant.util.IdentityMapper;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.BuildException;
import jetbrains.mps.build.ant.MPSClasspathUtil;
import java.util.List;
import java.net.URL;
import java.util.ArrayList;
import java.net.MalformedURLException;
import java.net.URLClassLoader;
import java.lang.reflect.Method;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.io.StringWriter;
import java.io.PrintWriter;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.types.FilterSetCollection;
import java.util.Vector;
import org.apache.tools.ant.Project;
public class ConvertToBinaryTask extends Copy {
private Map<String, String> toConvert = new HashMap<String, String>();
private boolean myStripImplementation = false;
private File mpsHome;
public ConvertToBinaryTask() {
FirstMatchMapper mapper = new FirstMatchMapper();
GlobPatternMapper first = new GlobPatternMapper();
first.setFrom("*.mps");
first.setTo("*.mpb");
mapper.add(first);
mapper.add(new IdentityMapper());
add(mapper);
fileUtils = new ConvertToBinaryTask.FileUtilsEx(fileUtils);
}
public void setMpsHome(File mpsHome) {
this.mpsHome = mpsHome;
}
public File getMpsHome() {
return mpsHome;
}
public void setStripImplementation(boolean value) {
this.myStripImplementation = value;
}
public boolean getStripImplementation() {
return myStripImplementation;
}
@Override
public void addFileset(FileSet set) {
set.setErrorOnMissingDir(false);
super.addFileset(set);
}
@Override
public void execute() throws BuildException {
super.execute();
// create output dir in any case, dest dir used in src packaging and fails if models dir not exists
if (destDir != null) {
destDir.mkdirs();
}
if (!(toConvert.isEmpty())) {
Iterable<File> classPaths = MPSClasspathUtil.buildClasspath(getProject(), mpsHome, false);
List<URL> classPathUrls = new ArrayList<URL>();
for (File path : classPaths) {
try {
classPathUrls.add(new URL("file:///" + path));
} catch (MalformedURLException e) {
throw new BuildException(e);
}
}
URLClassLoader classLoader = new URLClassLoader(classPathUrls.toArray(new URL[classPathUrls.size()]), this.getClass().getClassLoader());
try {
Thread.currentThread().setContextClassLoader(classLoader);
Class<?> converterClass = classLoader.loadClass("jetbrains.mps.tool.builder.converter.ConvertToBinaryWorker");
Object converter = converterClass.newInstance();
Method method = converterClass.getMethod("convert", Map.class, Boolean.class);
method.invoke(converter, toConvert, myStripImplementation);
} catch (Throwable t) {
if (t instanceof RuntimeException && t.getCause() instanceof IOException) {
t = t.getCause();
} else if (t instanceof InvocationTargetException) {
t = ((InvocationTargetException) t).getTargetException();
}
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
String message = sw.toString();
throw new BuildException(String.format("Cannot convert .mps into .mpb: %s\nModels:%s\nClasspath:%s", message, toConvert.keySet(), classPathUrls), t);
}
}
}
public class FileUtilsEx extends FileUtils {
private final FileUtils delegate;
public FileUtilsEx(FileUtils delegate) {
this.delegate = delegate;
}
@Override
public String getDefaultEncoding() {
return delegate.getDefaultEncoding();
}
@Override
public void copyFile(File sourceFile, File destFile, FilterSetCollection filters, Vector filterChains, boolean overwrite, boolean preserveLastModified, boolean append, String inputEncoding, String outputEncoding, Project project, boolean force) throws IOException {
if (sourceFile.getPath().endsWith(".mps")) {
toConvert.put(sourceFile.getPath(), destFile.getPath());
} else {
delegate.copyFile(sourceFile, destFile, filters, filterChains, overwrite, preserveLastModified, append, inputEncoding, outputEncoding, project, force);
}
}
}
}