package jetbrains.mps.migration.component.util; /*Generated by MPS */ import jetbrains.mps.project.AbstractModule; import org.jetbrains.mps.openapi.module.SModule; import jetbrains.mps.baseLanguage.tuples.runtime.Tuples; import jetbrains.mps.lang.migration.runtime.base.MigrationScriptReference; import org.jetbrains.mps.openapi.model.SNode; import jetbrains.mps.vfs.IFile; import org.jetbrains.mps.openapi.model.SModel; import jetbrains.mps.extapi.persistence.FileDataSource; import org.jetbrains.mps.openapi.persistence.ModelFactory; import jetbrains.mps.persistence.PersistenceRegistry; import jetbrains.mps.project.MPSExtentions; import java.util.Map; import jetbrains.mps.internal.collections.runtime.MapSequence; import java.util.HashMap; import java.io.IOException; import jetbrains.mps.internal.collections.runtime.Sequence; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SModelOperations; import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations; import org.jetbrains.mps.openapi.persistence.ModelSaveException; import java.util.Collections; import java.util.List; import jetbrains.mps.internal.collections.runtime.ListSequence; import java.util.ArrayList; import jetbrains.mps.baseLanguage.tuples.runtime.MultiTuple; import jetbrains.mps.internal.collections.runtime.IWhereFilter; import jetbrains.mps.smodel.Generator; import jetbrains.mps.util.FileUtil; import jetbrains.mps.vfs.FileSystem; public class MigrationDataUtil { public static void saveData(AbstractModule module, SModule dataModule, Iterable<Tuples._2<MigrationScriptReference, SNode>> data) { IFile file = getDataFile(module); SModel model; FileDataSource dataSource = new FileDataSource(file); ModelFactory factory = PersistenceRegistry.getInstance().getModelFactory(MPSExtentions.MODEL); Map<String, String> options = MapSequence.<String, String>fromMapAndKeysArray(new HashMap<String, String>(), ModelFactory.OPTION_MODELNAME, ModelFactory.OPTION_MODULEREF).withValues("migrationData", dataModule.getModuleReference().toString()); try { model = factory.create(dataSource, options); } catch (IOException e) { throw new RuntimeException(); } for (Tuples._2<MigrationScriptReference, SNode> p : Sequence.fromIterable(data)) { SNode stepData = SModelOperations.createNewRootNode(model, MetaAdapterFactory.getConcept(0x9882f4ad195546feL, 0x826994189e5dbbf2L, 0x66eed171c5f82709L, "jetbrains.mps.lang.migration.util.structure.StepData")); SPropertyOperations.set(stepData, MetaAdapterFactory.getProperty(0x9882f4ad195546feL, 0x826994189e5dbbf2L, 0x66eed171c5f82709L, 0x1b62b551c10bc5f4L, "script"), p._0().serialize()); SLinkOperations.setTarget(stepData, MetaAdapterFactory.getContainmentLink(0x9882f4ad195546feL, 0x826994189e5dbbf2L, 0x66eed171c5f82709L, 0x66eed171c5f90226L, "data"), p._1()); } try { factory.save(model, dataSource); } catch (IOException e) { throw new RuntimeException(e); } catch (ModelSaveException e) { throw new RuntimeException(e); } } public static Iterable<Tuples._2<MigrationScriptReference, SNode>> loadData(AbstractModule module) { IFile file = getDataFile(module); if (!(file.exists())) { return Sequence.fromIterable(Collections.<Tuples._2<MigrationScriptReference, SNode>>emptyList()); } List<Tuples._2<MigrationScriptReference, SNode>> result = ListSequence.fromList(new ArrayList<Tuples._2<MigrationScriptReference, SNode>>()); FileDataSource dataSource = new FileDataSource(file); ModelFactory factory = PersistenceRegistry.getInstance().getModelFactory(MPSExtentions.MODEL); SModel model; try { model = factory.load(dataSource, Collections.<String,String>emptyMap()); } catch (IOException e) { throw new RuntimeException(e); } for (SNode root : ListSequence.fromList(SModelOperations.roots(model, MetaAdapterFactory.getConcept(0x9882f4ad195546feL, 0x826994189e5dbbf2L, 0x66eed171c5f82709L, "jetbrains.mps.lang.migration.util.structure.StepData")))) { ListSequence.fromList(result).addElement(MultiTuple.<MigrationScriptReference,SNode>from(MigrationScriptReference.deserialize(SPropertyOperations.getString(root, MetaAdapterFactory.getProperty(0x9882f4ad195546feL, 0x826994189e5dbbf2L, 0x66eed171c5f82709L, 0x1b62b551c10bc5f4L, "script"))), SLinkOperations.getTarget(root, MetaAdapterFactory.getContainmentLink(0x9882f4ad195546feL, 0x826994189e5dbbf2L, 0x66eed171c5f82709L, 0x66eed171c5f90226L, "data")))); } return result; } public static void addData(SModule module, SModule dataModule, MigrationScriptReference script, SNode data) { List<Tuples._2<MigrationScriptReference, SNode>> loadedData = Sequence.fromIterable(loadData((AbstractModule) module)).toListSequence(); ListSequence.fromList(loadedData).addElement(MultiTuple.<MigrationScriptReference,SNode>from(script, data)); saveData((AbstractModule) module, dataModule, loadedData); } public static SNode readData(SModule module, final MigrationScriptReference script) { List<Tuples._2<MigrationScriptReference, SNode>> loadedData = Sequence.fromIterable(loadData((AbstractModule) module)).toListSequence(); if (loadedData == null) { return null; } Tuples._2<MigrationScriptReference, SNode> result = ListSequence.fromList(loadedData).where(new IWhereFilter<Tuples._2<MigrationScriptReference, SNode>>() { public boolean accept(Tuples._2<MigrationScriptReference, SNode> it) { return eq_hzite5_a0a0a0a0a0a0c0d(it._0(), script); } }).first(); return (result == null ? null : result._1()); } public static IFile getDataFile(AbstractModule module) { String path; if (module instanceof Generator) { path = FileUtil.getNameWithoutExtension(((Generator) module).getSourceLanguage().getDescriptorFile().getPath()) + "generator.migration"; } else { path = FileUtil.getNameWithoutExtension(module.getDescriptorFile().getPath()) + ".migration"; } return FileSystem.getInstance().getFileByPath(path); } private static boolean eq_hzite5_a0a0a0a0a0a0c0d(Object a, Object b) { return (a != null ? a.equals(b) : a == b); } }