package jetbrains.mps.migration.component.plugin;
/*Generated by MPS */
import com.intellij.openapi.project.Project;
import jetbrains.mps.project.MPSProject;
import jetbrains.mps.ide.migration.wizard.MigrationWizard;
import jetbrains.mps.ide.migration.wizard.MigrationSession;
import jetbrains.mps.ide.migration.wizard.MigrationErrorDescriptor;
import jetbrains.mps.ide.migration.MigrationManager;
import jetbrains.mps.migration.global.MigrationOptions;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import jetbrains.mps.migration.global.ProjectMigration;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.ArrayList;
import jetbrains.mps.lang.migration.runtime.base.MigrationScript;
import org.jetbrains.mps.openapi.module.SModule;
import jetbrains.mps.ide.migration.ScriptApplied;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.baseLanguage.closures.runtime.Wrappers;
import jetbrains.mps.smodel.ModelAccess;
import jetbrains.mps.smodel.MPSModuleRepository;
import jetbrains.mps.internal.collections.runtime.IWhereFilter;
import jetbrains.mps.migration.global.CleanupProjectMigration;
import jetbrains.mps.lang.migration.runtime.base.BaseScriptReference;
import jetbrains.mps.internal.collections.runtime.ITranslator2;
import jetbrains.mps.internal.collections.runtime.ISelector;
import jetbrains.mps.lang.migration.runtime.base.MigrationScriptBase;
import jetbrains.mps.lang.migration.runtime.base.MigrationScriptReference;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.model.SNode;
public class TestMigrationHelper {
private Project myProject;
private MPSProject myMpsProject;
public TestMigrationHelper(Project project, MPSProject mpsProject) {
myProject = project;
myMpsProject = mpsProject;
}
public void main() {
new MigrationWizard(myProject, new TestMigrationHelper.MyMigrationSession()).showAndGet();
}
private class MyMigrationSession implements MigrationSession {
private MigrationErrorDescriptor myD;
private TestMigrationHelper.MyMigrationSession.MyMigrationManager myManager = new TestMigrationHelper.MyMigrationSession.MyMigrationManager();
public MyMigrationSession() {
}
public jetbrains.mps.project.Project getProject() {
return myMpsProject;
}
public MigrationManager getMigrationManager() {
return this.myManager;
}
public MigrationOptions getOptions() {
return new MigrationOptions();
}
@Nullable
public MigrationErrorDescriptor getErrorDescriptor() {
// todo remove odd MigrationErrorDescriptor
return MyMigrationSession.this.myD;
}
public void setErrorDescriptor(MigrationErrorDescriptor errors) {
MyMigrationSession.this.myD = errors;
}
private class MyMigrationManager implements MigrationManager {
private List<ProjectMigration> myProjectMig = ListSequence.fromListAndArray(new ArrayList<ProjectMigration>(), new TestMigrationHelper.MyMigrationSession.MyCleanupProjectMigration("project migration 1: cleanup"), new TestMigrationHelper.MyMigrationSession.MyCleanupProjectMigration("project migration 2: cleanup"), new TestMigrationHelper.MyMigrationSession.MyProjectMigration("project migration 3: normal"), new TestMigrationHelper.MyMigrationSession.MyProjectMigration("project migration 4: normal"));
private List<MigrationScript> myModuleMig = ListSequence.fromListAndArray(new ArrayList<MigrationScript>(), new TestMigrationHelper.MyMigrationSession.MyModuleMigration("module migration 1", 1), new TestMigrationHelper.MyMigrationSession.MyModuleMigration("module migration 2", 2));
public MyMigrationManager() {
}
public boolean isMigrationRequired() {
return true;
}
public boolean isMigrationRequired(Iterable<SModule> modules) {
return true;
}
public boolean importVersionsUpdateRequired(Iterable<SModule> modules) {
return false;
}
public void doUpdateImportVersions(SModule module) {
}
@Override
public void executeScript(ScriptApplied s) {
s.getScriptReference().resolve(true).execute(s.getModule());
ListSequence.fromList(passedM).addElement(s);
}
public Iterable<ProjectMigration> getProjectMigrationsToApply() {
return myProjectMig;
}
public List<ScriptApplied> getModuleMigrationsToApply(Iterable<SModule> modules) {
return (List<ScriptApplied>) ((List) Sequence.fromIterable(getModuleMigrationsApplied()).toListSequence());
}
public List<ScriptApplied> getMissingMigrations() {
final Wrappers._T<SModule> module = new Wrappers._T<SModule>();
ModelAccess.instance().runReadAction(new Runnable() {
public void run() {
module.value = MPSModuleRepository.getInstance().getModules().iterator().next();
}
});
return ListSequence.fromListAndArray(new ArrayList<ScriptApplied>(), new ScriptApplied(module.value, ListSequence.fromList(myModuleMig).first().getReference()));
}
public int projectStepsCount(final boolean isCleanup) {
return Sequence.fromIterable(getProjectMigrationsToApply()).where(new IWhereFilter<ProjectMigration>() {
public boolean accept(ProjectMigration it) {
return isCleanup == (it instanceof CleanupProjectMigration);
}
}).count();
}
private List<ProjectMigration> passedP = ListSequence.fromList(new ArrayList<ProjectMigration>());
public ProjectMigration nextProjectStep(MigrationOptions options, final boolean cleanup) {
final ProjectMigration next = Sequence.fromIterable(getProjectMigrationsToApply()).where(new IWhereFilter<ProjectMigration>() {
public boolean accept(ProjectMigration it) {
return cleanup == (it instanceof CleanupProjectMigration);
}
}).findFirst(new IWhereFilter<ProjectMigration>() {
public boolean accept(ProjectMigration it) {
return !(ListSequence.fromList(passedP).contains(it));
}
});
if (next == null) {
return null;
}
ListSequence.fromList(passedP).addElement(next);
return next;
}
public int moduleStepsCount() {
return Sequence.fromIterable(getModuleMigrationsApplied()).count();
}
private List<ScriptApplied> passedM = ListSequence.fromList(new ArrayList<ScriptApplied>());
public ScriptApplied nextModuleStep(@Nullable BaseScriptReference ref) {
Iterable<ScriptApplied> applied = getModuleMigrationsApplied();
final ScriptApplied sa = Sequence.fromIterable(applied).findFirst(new IWhereFilter<ScriptApplied>() {
public boolean accept(final ScriptApplied sa) {
return ListSequence.fromList(passedM).all(new IWhereFilter<ScriptApplied>() {
public boolean accept(ScriptApplied it) {
return neq_q74r2d_a0a0a0a0a0a0a0a0a0a0b0r9h(it.getScriptReference(), sa.getScriptReference()) || it.getModule() != sa.getModule();
}
});
}
});
return sa;
}
private Iterable<ScriptApplied> getModuleMigrationsApplied() {
final List<SModule> modules = ListSequence.fromList(((List<SModule>) myMpsProject.getProjectModules())).take(5).toListSequence();
return ListSequence.fromList(this.myModuleMig).translate(new ITranslator2<MigrationScript, ScriptApplied>() {
public Iterable<ScriptApplied> translate(final MigrationScript script) {
return ListSequence.fromList(modules).select(new ISelector<SModule, ScriptApplied>() {
public ScriptApplied select(SModule module) {
return new ScriptApplied(module, script.getReference());
}
});
}
});
}
}
private class MyModuleMigration extends MigrationScriptBase {
private String myCaption;
private int myFakeId;
public MyModuleMigration(String caption, int fakeId) {
myCaption = caption;
myFakeId = fakeId;
}
@Override
public String getCaption() {
return myCaption;
}
public MigrationScriptReference getReference() {
// todo this is suspicious
// todo name is used as id here
final TestMigrationHelper.MyMigrationSession.MyModuleMigration _this = this;
return new MigrationScriptReference(MetaAdapterFactory.getLanguage(0, myFakeId, "fake lang " + myFakeId), 0) {
@Override
public MigrationScript resolve(boolean silent) {
return _this;
}
};
}
@Nullable
public SNode execute(SModule module) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
return null;
}
}
private class MyProjectMigration implements ProjectMigration {
private String myId;
public MyProjectMigration(String id) {
myId = id;
}
public String getDescription() {
return myId;
}
public void applyToCreatedProject(jetbrains.mps.project.Project p0) {
}
public boolean shouldBeExecuted(jetbrains.mps.project.Project p0) {
return true;
}
public boolean isRerunnable() {
return true;
}
public void execute(jetbrains.mps.project.Project p0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
private class MyCleanupProjectMigration extends TestMigrationHelper.MyMigrationSession.MyProjectMigration implements CleanupProjectMigration {
public MyCleanupProjectMigration(String id) {
super(id);
}
@Override
public void forceExecutionNextTime(jetbrains.mps.project.Project project) {
}
}
}
private static boolean neq_q74r2d_a0a0a0a0a0a0a0a0a0a0b0r9h(Object a, Object b) {
return !(((a != null ? a.equals(b) : a == b)));
}
}