package jetbrains.mps.lang.textGen.migration;

/*Generated by MPS */

import jetbrains.mps.lang.migration.runtime.base.MigrationScriptBase;
import org.jetbrains.mps.openapi.model.SNode;
import org.jetbrains.mps.openapi.module.SModule;
import org.jetbrains.mps.openapi.model.SModel;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SModuleOperations;
import jetbrains.mps.smodel.SModelInternal;
import org.jetbrains.mps.openapi.module.SModuleReference;
import org.jetbrains.mps.openapi.persistence.PersistenceFacade;
import org.jetbrains.mps.openapi.language.SLanguage;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.lang.migration.runtime.base.MigrationScriptReference;

public class UseDevkit extends MigrationScriptBase {
  private final String description = "Employ GP and devkit for textgen aspect models";
  public String getCaption() {
    return description;
  }
  @Override
  public boolean isRerunnable() {
    return true;
  }
  public SNode execute(final SModule m) {
    doExecute(m);
    return null;
  }
  public void doExecute(final SModule m) {
    SModel textgenAspectModel = SModuleOperations.getAspect(m, "textGen");
    if (textgenAspectModel instanceof SModelInternal == false) {
      return;
    }
    SModelInternal mi = (SModelInternal) textgenAspectModel;
    final SModuleReference tgAspectDevkit = PersistenceFacade.getInstance().createModuleReference("fa73d85a-ac7f-447b-846c-fcdc41caa600(jetbrains.mps.devkit.aspect.textgen)");
    if (mi.importedDevkits().contains(tgAspectDevkit)) {
      return;
    }
    if (mi.importedLanguageIds().stream().anyMatch((SLanguage l) -> !(l.getQualifiedName().startsWith("jetbrains.mps.")))) {
      // Transition code, in case aspect uses custom extensions, do not turn GP on for it yet.
      return;
    }
    mi.deleteLanguageId(MetaAdapterFactory.getLanguage(0xb83431fe5c8f40bcL, 0x8a3665e25f4dd253L, "jetbrains.mps.lang.textGen"));
    mi.deleteDevKit(PersistenceFacade.getInstance().createModuleReference("fbc25dd2-5da4-483a-8b19-70928e1b62d7(jetbrains.mps.devkit.general-purpose)"));
    mi.deleteDevKit(PersistenceFacade.getInstance().createModuleReference("2677cb18-f558-4e33-bc38-a5139cee06dc(jetbrains.mps.devkit.language-design)"));
    mi.deleteDevKit(PersistenceFacade.getInstance().createModuleReference("e073aac8-8c71-4c23-be71-86bf7a6df0a2(jetbrains.mps.devkit.bootstrap-languages)"));
    mi.addDevKit(tgAspectDevkit);
  }
  public MigrationScriptReference getReference() {
    return new MigrationScriptReference(MetaAdapterFactory.getLanguage(0xb83431fe5c8f40bcL, 0x8a3665e25f4dd253L, "jetbrains.mps.lang.textGen"), 0);
  }

}
