package jetbrains.mps.tool.common; /*Generated by MPS */ import java.util.Set; import java.io.File; import java.util.LinkedHashSet; import java.util.Map; import java.util.List; import java.util.LinkedHashMap; import org.apache.log4j.Level; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; import org.jdom.Element; import java.io.FileNotFoundException; import java.io.IOException; import org.jdom.JDOMException; public class Script { private static final String ELEMENT_TODO = "todo"; private static final String ELEMENT_MODEL = "model"; private static final String ELEMENT_MODULE = "module"; private static final String ELEMENT_EXCLUDEDFROMDIFF = "excludedFromDiff"; private static final String ELEMENT_PROJECT = "project"; private static final String ELEMENT_PARAMETER = "parameter"; private static final String ELEMENT_PROPERTY = "property"; private static final String PATH = "path"; private static final String VALUE = "value"; private static final String ELEMENT_CHUNK = "chunk"; private static final String ATTRIBUTE_BOOTSTRAP = "bootstrap"; private static final String ELEMENT_LIBRARYJAR = "libraryJar"; private final Set<File> myModels = new LinkedHashSet<File>(); private final Set<File> myModules = new LinkedHashSet<File>(); private final Set<File> myExcludedFromDiff = new LinkedHashSet<File>(); private final Map<File, List<String>> myMPSProjects = new LinkedHashMap<File, List<String>>(); private boolean myFailOnError = true; private final Map<String, File> myLibraries = new LinkedHashMap<String, File>(); private final Set<String> myCompiledLibraries = new LinkedHashSet<String>(); private final Map<String, String> myMacro = new LinkedHashMap<String, String>(); private Level myLogLevel = Level.INFO; private final Map<String, String> myProperties = new LinkedHashMap<String, String>(); private final List<String> myParameters = new ArrayList<String>(); private boolean myLoadBootstrapLibraries = true; private final Map<List<String>, Boolean> myChunks = new LinkedHashMap<List<String>, Boolean>(); private final List<String> myLibraryJars = new ArrayList<String>(); public Script() { } public void addModuleFile(File file) { assert file.exists() && !(file.isDirectory()) : "bad file: " + file.toString(); myModules.add(file); } public void addModelFile(File file) { assert file.exists() && !(file.isDirectory()) : "bad file: " + file.toString(); myModels.add(file); } public void excludeFileFromDiff(File file) { assert file.exists() && !(file.isDirectory()); myExcludedFromDiff.add(file); } public void addProjectFile(File projectFile) { assert projectFile.exists() && projectFile.isFile(); if (!(myMPSProjects.containsKey(projectFile))) { myMPSProjects.put(projectFile, new ArrayList<String>()); } } public void addProjectFile(File projectFile, String... property) { assert projectFile.exists() && projectFile.isFile(); List<String> projectProperties = myMPSProjects.get(projectFile); if (projectProperties == null) { projectProperties = new ArrayList<String>(); myMPSProjects.put(projectFile, projectProperties); } projectProperties.addAll(Arrays.asList(property)); } public void addChunk(List<String> modules, boolean isBootstrap) { myChunks.put(modules, isBootstrap); } public void addLibraryJar(String libraryJar) { myLibraryJars.add(libraryJar); } public Set<File> getModels() { return Collections.unmodifiableSet(myModels); } public void updateModels(Set<File> models) { myModels.addAll(models); } public Set<File> getExcludedFromDiffFiles() { return Collections.unmodifiableSet(myExcludedFromDiff); } public void updateExcludedFromDiffFiles(Set<File> excluded) { myExcludedFromDiff.addAll(excluded); } public Set<File> getModules() { return Collections.unmodifiableSet(myModules); } public void updateModules(Set<File> modules) { myModules.addAll(modules); } public Map<File, List<String>> getMPSProjectFiles() { return Collections.unmodifiableMap(myMPSProjects); } public void updateMPSProjectFiles(Map<File, List<String>> mpsProjects) { myMPSProjects.putAll(mpsProjects); } public boolean getFailOnError() { return myFailOnError; } public void updateFailOnError(boolean showError) { myFailOnError = showError; } public Map<String, String> getProperties() { return Collections.unmodifiableMap(myProperties); } public void updateProperties(Map<String, String> properties) { myProperties.putAll(properties); } public void addLibrary(String name, File dir, boolean compile) { myLibraries.put(name, dir); if (compile) { myCompiledLibraries.add(name); } } public Map<String, File> getLibraries() { return Collections.unmodifiableMap(myLibraries); } public void updateLibraries(Map<String, File> libraries) { myLibraries.putAll(libraries); } public Set<String> getCompiledLibraries() { return Collections.unmodifiableSet(myCompiledLibraries); } public void updateCompiledLibraries(Set<String> libraries) { myCompiledLibraries.addAll(libraries); } public void addMacro(String name, String value) { myMacro.put(name, value); } public Map<String, String> getMacro() { return Collections.unmodifiableMap(myMacro); } public void updateMacro(Map<String, String> macro) { myMacro.putAll(macro); } public void updateLogLevel(Level level) { myLogLevel = level; } public Level getLogLevel() { return myLogLevel; } public void addParameter(String parameter) { myParameters.add(parameter); } public List<String> getParameters() { return myParameters; } public void updateParameters(List<String> parameters) { myParameters.addAll(parameters); } public boolean isLoadBootstrapLibraries() { return myLoadBootstrapLibraries; } public void setLoadBootstrapLibraries(boolean load) { myLoadBootstrapLibraries = load; } public Map<List<String>, Boolean> getChunks() { return Collections.unmodifiableMap(myChunks); } public void updateChunks(Map<List<String>, Boolean> chunks) { myChunks.putAll(chunks); } public List<String> getLibraryJars() { return Collections.unmodifiableList(myLibraryJars); } public void updateLibraryJars(List<String> libraryJars) { myLibraryJars.addAll(libraryJars); } public void cloneTo(Object dest) { // TODO get rid of generic cloneTo Class<? extends Script> srcClass = this.getClass(); Class<? extends Object> destClass = dest.getClass(); Method[] srcMethods = srcClass.getMethods(); for (Method srcMethod : srcMethods) { String srcMethodName = srcMethod.getName(); if (srcMethodName.startsWith("get")) { String dstMethodName = srcMethodName.replace("get", "update"); try { Method destMethod = destClass.getMethod(dstMethodName, srcMethod.getReturnType()); destMethod.invoke(dest, srcMethod.invoke(this)); } catch (NoSuchMethodException e) { // doing nothing } catch (InvocationTargetException e) { throw new IllegalArgumentException("cannot clone", e); } catch (IllegalAccessException e) { throw new IllegalArgumentException("cannot clone", e); } } } } private Element prepareData() { Element data = new Element(ELEMENT_TODO); for (File f : myModels) { data.addContent(new Element(ELEMENT_MODEL).setAttribute(PATH, f.getAbsolutePath())); } for (File f : myModules) { data.addContent(new Element(ELEMENT_MODULE).setAttribute(PATH, f.getAbsolutePath())); } for (File f : myExcludedFromDiff) { data.addContent(new Element(ELEMENT_EXCLUDEDFROMDIFF).setAttribute(PATH, f.getAbsolutePath())); } for (File f : myMPSProjects.keySet()) { Element elem = new Element(ELEMENT_PROJECT).setAttribute(PATH, f.getAbsolutePath()); for (String s : myMPSProjects.get(f)) { elem.addContent(new Element(ELEMENT_PROPERTY).setAttribute(VALUE, s)); } data.addContent(elem); } for (String p : myParameters) { data.addContent(new Element(ELEMENT_PARAMETER).setAttribute(VALUE, p)); } for (Map.Entry<List<String>, Boolean> chunk : myChunks.entrySet()) { Element element = new Element(ELEMENT_CHUNK).setAttribute(ATTRIBUTE_BOOTSTRAP, chunk.getValue().toString()); for (String module : chunk.getKey()) { element.addContent(new Element(ELEMENT_MODULE).setAttribute(PATH, module)); } data.addContent(element); } for (String jar : myLibraryJars) { data.addContent(new Element(ELEMENT_LIBRARYJAR).setAttribute(PATH, jar)); } return data; } public void parseData(Element elem) { for (Object o : elem.getChildren()) { Element e = (Element) o; String elementName = e.getName(); if (ELEMENT_MODEL.equals(elementName)) { addModelFile(new File(e.getAttributeValue(PATH))); } else if (ELEMENT_MODULE.equals(elementName)) { addModuleFile(new File(e.getAttributeValue(PATH))); } else if (ELEMENT_EXCLUDEDFROMDIFF.equals(elementName)) { excludeFileFromDiff(new File(e.getAttributeValue(PATH))); } else if (ELEMENT_PROJECT.equals(elementName)) { List<String> properties = new ArrayList<String>(); for (Object prop : e.getChildren(ELEMENT_PROPERTY)) { properties.add(((Element) prop).getAttributeValue(VALUE)); } addProjectFile(new File(e.getAttributeValue(PATH)), properties.toArray(new String[properties.size()])); } else if (ELEMENT_PARAMETER.equals(elementName)) { addParameter(e.getAttributeValue(VALUE)); } else if (ELEMENT_CHUNK.equals(elementName)) { List<String> chunkModules = new ArrayList<String>(); List modules = e.getChildren(); for (Object oo : modules) { Element moduleElement = (Element) oo; if (ELEMENT_MODULE.equals(moduleElement.getName())) { String path = moduleElement.getAttributeValue(PATH); if ((path != null && path.length() > 0)) { chunkModules.add(path); } } } addChunk(chunkModules, Boolean.parseBoolean(e.getAttributeValue(ATTRIBUTE_BOOTSTRAP))); } else if (ELEMENT_LIBRARYJAR.equals(elementName)) { String path = e.getAttributeValue(PATH); if ((path != null && path.length() > 0)) { addLibraryJar(path); } } } } public File dumpToTmpFile() throws FileNotFoundException { File tmpFile = Script.createTmpFile(); ScriptData data = new ScriptData(); data.setFailOnError(myFailOnError); data.setLogLevel(myLogLevel); data.setLoadBootstrapLibraries(myLoadBootstrapLibraries); data.setLibraries(myLibraries); data.setMacros(myMacro); data.setProperties(myProperties); data.setData(ELEMENT_TODO, prepareData()); try { data.save(tmpFile); } catch (IOException e) { e.printStackTrace(); } return tmpFile; } public void putProperty(String name, String value) { myProperties.put(name, value); } public String getProperty(String name) { return myProperties.get(name); } public void addPerfomanceReport(String s) { String reports = myProperties.get(ScriptProperties.GENERATE_PERFORMANCE_REPORT); if (reports == null) { reports = ""; } if (!(reports.isEmpty())) { reports += ","; } reports += s; myProperties.put(ScriptProperties.GENERATE_PERFORMANCE_REPORT, reports); } public static File createTmpFile() { File tmp = new File(System.getProperty("java.io.tmpdir")); int i = 0; while (true) { if (!(new File(tmp, "mpstemp" + i).exists())) { break; } i++; } File result = new File(tmp, "mpstemp" + i); try { result.createNewFile(); } catch (IOException e) { e.printStackTrace(); } return result; } public static Script fromDumpInFile(File file) { ScriptData data = new ScriptData(); try { data.load(file); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (!(file.delete())) { throw new RuntimeException("File " + file + " was not deleted."); } } Script whatToDo = new Script(); whatToDo.myFailOnError = data.getFailOnError(); whatToDo.myLogLevel = data.getLogLevel(); whatToDo.myLoadBootstrapLibraries = data.getLoadBootstrapLibraries(); whatToDo.myProperties.putAll(data.getProperties()); whatToDo.myMacro.putAll(data.getMacros()); whatToDo.myLibraries.putAll(data.getLibraries()); whatToDo.parseData(data.getData(ELEMENT_TODO)); return whatToDo; } }