package jetbrains.mps.project.persistence; /*Generated by MPS */ import org.apache.log4j.Logger; import org.apache.log4j.LogManager; import java.io.File; import jetbrains.mps.util.MacroHelper; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jdom.Element; import jetbrains.mps.project.structure.project.ProjectDescriptor; import jetbrains.mps.project.structure.project.ModulePath; import jetbrains.mps.internal.collections.runtime.Sequence; import jetbrains.mps.internal.collections.runtime.ISelector; import jetbrains.mps.util.xml.XmlUtil; import jetbrains.mps.vfs.path.Path; import java.util.List; import jetbrains.mps.internal.collections.runtime.ListSequence; import java.util.ArrayList; import jetbrains.mps.vfs.IFile; import org.jdom.Document; import jetbrains.mps.util.JDOMUtil; import jetbrains.mps.internal.collections.runtime.IWhereFilter; import jetbrains.mps.vfs.impl.IoFileSystem; public class ProjectDescriptorPersistence { private static final Logger LOG = LogManager.getLogger(ProjectDescriptorPersistence.class); private static final String MPS_DOT_FOLDER = ".mps"; private static final String MODULES_XML_LOCATION = "modules.xml"; private static final String MODULE_PATH_TAG = "modulePath"; private static final String FOLDER_TAG = "folder"; private static final String PROJECT_MODULES_TAG = "projectModules"; private static final String PATH_TAG = "path"; private final File myBaseDir; private final MacroHelper myMacroHelper; public ProjectDescriptorPersistence(@NotNull File baseDir, @NotNull MacroHelper macroHelper) { myBaseDir = baseDir.getAbsoluteFile(); myMacroHelper = macroHelper; } @Nullable public Element save(@NotNull ProjectDescriptor descriptor) { Element project = new Element("project"); Element projectModules = new Element(PROJECT_MODULES_TAG); for (ModulePath path : Sequence.fromIterable(((Iterable<ModulePath>) descriptor.getModulePaths())).sort(new ISelector<ModulePath, String>() { public String select(ModulePath p) { return shrinkPath(p); } }, true)) { // TODO: move from MacrosFactory to PathMacroUtil XmlUtil.tagWithAttributes(projectModules, MODULE_PATH_TAG, PATH_TAG, shrinkPath(path), FOLDER_TAG, path.getVirtualFolder()); } project.addContent(projectModules); return project; } private String shrinkPath(@NotNull ModulePath p) { String shrinkedPath = myMacroHelper.shrinkPath(p.getPath()); // fixme such filepath convertation is not supported by Path (IDEA stores windows paths as C:/smth !) return shrinkedPath.replace(Path.WIN_SEPARATOR, Path.UNIX_SEPARATOR); } @NotNull public ProjectDescriptor load(@Nullable Element root) { final String name = myBaseDir.getName(); ProjectDescriptor descriptor = new ProjectDescriptor(name); if (root == null) { return descriptor; } ProjectDescriptor result_jnk9az_a3a71 = descriptor; List<Element> moduleList = ListSequence.fromList(new ArrayList<Element>()); // AP : these are deprecated, aren't they? ListSequence.fromList(moduleList).addSequence(Sequence.fromIterable(XmlUtil.children(XmlUtil.first(root, "projectSolutions"), "solutionPath"))); ListSequence.fromList(moduleList).addSequence(Sequence.fromIterable(XmlUtil.children(XmlUtil.first(root, "projectLanguages"), "languagePath"))); ListSequence.fromList(moduleList).addSequence(Sequence.fromIterable(XmlUtil.children(XmlUtil.first(root, "projectDevkits"), "devkitPath"))); ListSequence.fromList(moduleList).addSequence(Sequence.fromIterable(XmlUtil.children(XmlUtil.first(root, PROJECT_MODULES_TAG), MODULE_PATH_TAG))); for (Element moduleElement : ListSequence.fromList(moduleList)) { String path = myMacroHelper.expandPath(moduleElement.getAttributeValue(PATH_TAG)); String virtualFolder = moduleElement.getAttributeValue(FOLDER_TAG); ModulePath modulePath = new ModulePath(path, virtualFolder); result_jnk9az_a3a71.addModulePath(modulePath); } return descriptor; } @Nullable public Element loadProjectElement() { try { IFile projectFile = findProjectFile(myBaseDir.getPath()); if (!(projectFile.exists())) { return null; } Document document = JDOMUtil.loadDocument(projectFile); Iterable<Element> components = document.getRootElement().getChildren("component"); return Sequence.fromIterable(components).findFirst(new IWhereFilter<Element>() { public boolean accept(Element it) { return it.getAttributeValue("name").equals("MPSProject"); } }); } catch (Exception e) { throw new RuntimeException(e); } } @NotNull private static IFile findProjectFile(String path) { IFile projectFile = IoFileSystem.INSTANCE.getFile(path); if (!(projectFile.exists())) { throw new IllegalArgumentException("Path " + path + " does not exist"); } if (projectFile.isDirectory()) { projectFile = projectFile.getDescendant(MPS_DOT_FOLDER).getDescendant(MODULES_XML_LOCATION); } if (!(projectFile.toPath().endsWith(MODULES_XML_LOCATION))) { LOG.warn("Supposed to be the 'modules.xml' file: '" + projectFile + "'"); } return projectFile; } }