package eu.sorescu.util;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.io.FileUtils;
import org.apache.tools.ant.Project;
import org.codehaus.groovy.ant.UberCompileTask;
import org.apache.tools.ant.types.Path;
import eu.sorescu.reflect.JARLoader;
public class GroovyProjectCompiler {
ArrayList<File> jars = new ArrayList<>();
ArrayList<File> roots = new ArrayList<>();
ArrayList<File> sources = new ArrayList<>();
File target = null;
private GroovyProjectCompiler() {
}
private static String deref(String path) {
for (;;) {
String[] segments = path.split("\\%");
if (segments.length == 1)
return path;
String value = System.getenv(segments[1]);
if (value == null)
value = "";
path = path.replace("%" + segments[1] + "%", value);
}
}
private void configure(Collection<String> configLines) throws IOException {
for (String config : configLines)
configure(config);
}
private void configure(String config) throws IOException {
if ((config == null) || (config.trim().length() == 0) || (config.charAt(0) == '#'))
return;
if (config.charAt(0) != '-')
throw new IllegalArgumentException("Argument " + config + " not understood");
File file = new File(deref(config.substring(2)));
if (config.charAt(1) == 'J')
jars.add(file);
else if (config.charAt(1) == 'T') {
if (target != null)
throw new IllegalArgumentException("Target set already to '" + target + "', cannot set again to '" + file + "'.");
target = file;
} else if (config.charAt(1) == 'S')
sources.add(file);
else if (config.charAt(1) == 'R')
roots.add(file);
else if (config.charAt(1) == 'C') {
System.out.println("Loading file: " + file);
configure(Files.readAllLines(file.toPath(), StandardCharsets.UTF_8));
} else
throw new IllegalArgumentException("Argument " + config + " not understood");
}
public static void main(String[] args) throws ClassNotFoundException, IOException {
GroovyProjectCompiler compiler = new GroovyProjectCompiler();
if ((args == null) || (args.length == 0)) {
System.out.println("GroovyCompiler -T<target_folder> [-J<jar_folder> ...] [-S<source_folder> ...] [-C<config_path>...] [-R<jar_tree_root>...]");
return;
} else {
for (String arg : args)
compiler.configure(arg);
compiler.compile();
}
}
public void compile() throws ClassNotFoundException, IOException {
if (target == null)
throw new IllegalArgumentException("Only one target file must be defined.");
target.mkdirs();
target.mkdir();
File tempFolder = new File(File.createTempFile("sorescu_groovy_compiler_", "").getParent(), "sorescu_groovy_compiler_" + Math.random());
tempFolder.mkdir();
for (File jar : jars) {
System.out.println("Loading JAR: " + jar);
JARLoader.loadJAR(jar);
}
for (File source : sources) {
FileUtils.copyDirectory(source, tempFolder);
FileUtils.copyDirectory(source, target);
}
for (File root : roots) {
System.out.println("Loading recursive JAR folder: " + root);
JARLoader.loadJARFolder(root, true);
}
for (File jar : jars) {
System.out.println("Copying JAR: " + jar);
FileUtils.copyFile(jar, new File(target, jar.getName()));
}
final Project project = new Project();
final Path classPath = new Path(project, target.getAbsolutePath());
for (File jar : jars) {
System.out.println("Adding to classpath: " + jar);
classPath.add(new Path(project, jar.toString()));
}
UberCompileTask uberCompiler = new UberCompileTask();
project.setBaseDir(tempFolder);
uberCompiler.setSrcdir(new Path(project, tempFolder.getAbsolutePath()));
uberCompiler.setDestdir(target);
uberCompiler.setProject(project);
uberCompiler.setClasspath(classPath);
uberCompiler.execute();
}
}