package jetbrains.mps.tool.builder.make;
/*Generated by MPS */
import jetbrains.mps.core.tool.environment.classloading.UrlClassLoader;
import jetbrains.mps.tool.common.Script;
import jetbrains.mps.tool.builder.MpsWorker;
import org.apache.log4j.Logger;
import jetbrains.mps.tool.environment.EnvironmentConfig;
import java.io.File;
import jetbrains.mps.internal.collections.runtime.IMapping;
import jetbrains.mps.internal.collections.runtime.MapSequence;
import jetbrains.mps.tool.environment.Environment;
import jetbrains.mps.project.Project;
import java.util.List;
import java.util.Set;
import org.jetbrains.mps.openapi.module.SModule;
import java.util.LinkedHashSet;
import java.util.Collections;
import jetbrains.mps.tool.environment.MpsEnvironment;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.tool.common.ScriptProperties;
import jetbrains.mps.internal.collections.runtime.SetSequence;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.util.PathManager;
import jetbrains.mps.internal.collections.runtime.ISelector;
import java.net.URL;
import java.net.MalformedURLException;
import jetbrains.mps.internal.collections.runtime.IWhereFilter;
import jetbrains.mps.library.LibraryInitializer;
public class GeneratorWorker extends BaseGeneratorWorker {
private final UrlClassLoader myClassLoader;
public GeneratorWorker(Script whatToDo, MpsWorker.AntLogger logger) {
super(whatToDo, logger);
myClassLoader = createClassloader();
}
@Override
public void work() {
Logger.getRootLogger().setLevel(myWhatToDo.getLogLevel());
EnvironmentConfig config = EnvironmentConfig.defaultConfig();
for (String jar : myWhatToDo.getLibraryJars()) {
File jarFile = new File(jar);
if (!(jarFile.exists())) {
warning("Library " + jar + " does not exist.");
}
config = config.addLib(jar);
}
for (IMapping<String, String> macro : MapSequence.fromMap(myWhatToDo.getMacro())) {
config = config.addMacro(macro.key(), new File(macro.value()));
}
Environment environment = new GeneratorWorker.MyEnvironment(config);
environment.init();
setupEnvironment();
setGenerationProperties();
boolean doneSomething = false;
Project project = createDummyProject();
for (IMapping<List<String>, Boolean> chunk : MapSequence.fromMap(myWhatToDo.getChunks())) {
final List<String> modulePaths = chunk.key();
final Set<SModule> modules = new LinkedHashSet<SModule>();
project.getModelAccess().runWriteAction(new Runnable() {
public void run() {
for (String modulePath : modulePaths) {
processModuleFile(new File(modulePath), modules);
}
}
});
Boolean bootstrap = chunk.value();
if (bootstrap) {
warning("Found bootstrap chunk " + chunk.key() + ". Generation may be impossible.");
}
MpsWorker.ObjectsToProcess go = new MpsWorker.ObjectsToProcess(Collections.EMPTY_SET, modules, Collections.EMPTY_SET);
if (go.hasAnythingToGenerate()) {
generate(project, go);
doneSomething = true;
}
}
if (!(doneSomething)) {
error("Could not find anything to generate.");
}
dispose();
showStatistic();
}
public static void main(String[] args) {
MpsWorker mpsWorker = new GeneratorWorker(Script.fromDumpInFile(new File(args[0])), new MpsWorker.SystemOutLogger());
mpsWorker.workFromMain();
}
protected ClassLoader getClassLoader() {
return myClassLoader;
}
protected class MyEnvironment extends MpsEnvironment {
public MyEnvironment(@NotNull EnvironmentConfig config) {
super(config);
}
@Override
protected ClassLoader rootClassLoader() {
return myClassLoader;
}
}
private UrlClassLoader createClassloader() {
String pluginsPath = myWhatToDo.getProperty(ScriptProperties.PLUGIN_PATHS);
Set<File> pluginsClasspath = SetSequence.fromSet(new LinkedHashSet<File>());
if (pluginsPath != null) {
for (String plugin : pluginsPath.split(File.pathSeparator)) {
File lib = new File(plugin + File.separator + "lib");
if (lib.exists() && lib.isDirectory()) {
SetSequence.fromSet(pluginsClasspath).addSequence(Sequence.fromIterable(Sequence.fromArray(lib.listFiles(PathManager.JAR_FILE_FILTER))));
}
}
}
if ((pluginsPath == null || pluginsPath.length() == 0)) {
return null;
}
return new UrlClassLoader(SetSequence.fromSet(pluginsClasspath).select(new ISelector<File, URL>() {
public URL select(File it) {
try {
return it.toURI().toURL();
} catch (MalformedURLException e) {
return null;
}
}
}).where(new IWhereFilter<URL>() {
public boolean accept(URL it) {
return it != null;
}
}).toGenericArray(URL.class), LibraryInitializer.class.getClassLoader());
}
}