package jetbrains.mps.ide.script.plugin.migrationtool;
/*Generated by MPS */
import jetbrains.mps.project.MPSProject;
import org.jetbrains.mps.openapi.model.SNodeReference;
import org.jetbrains.mps.openapi.util.ProgressMonitor;
import org.jetbrains.mps.openapi.model.SNode;
import java.util.Collections;
import jetbrains.mps.ide.script.plugin.AbstractMigrationScriptHelper;
import jetbrains.mps.progress.EmptyProgressMonitor;
import jetbrains.mps.ide.findusages.model.SearchResults;
import jetbrains.mps.ide.findusages.model.SearchQuery;
import java.util.List;
import jetbrains.mps.ide.findusages.model.SearchResult;
public class MigrationScriptExecutor {
private MPSProject project;
private SNodeReference script;
private String title;
public MigrationScriptExecutor(SNodeReference script, String title, MPSProject project) {
this.project = project;
this.script = script;
this.title = title;
}
public void execImmediately(ProgressMonitor promon) {
// up to the point ot MigrationScriptController.process, we are fine with read action
// However, at the moment, we do not care to speed up UI stuff as it would require thorough refactoring, while simplicity is my goal now.
// E.g. shall split prepare() to run in read action, to collect applicable SNodeReference, and separate apply() to run as command in UI thread
getMPSProject().getModelAccess().checkWriteAccess();
SNode scriptNode = (SNode) script.resolve(getMPSProject().getRepository());
if (scriptNode == null) {
return;
}
final MigrationScriptFinder finder = new MigrationScriptFinder(Collections.singletonList(AbstractMigrationScriptHelper.toExecutable(scriptNode, getMPSProject())));
final MigrationScriptsController controller = new MigrationScriptsController(finder);
if (promon == null) {
promon = new EmptyProgressMonitor();
}
promon.start(String.format("Executing \"%s\"", title), 200);
SearchResults results = finder.find(new SearchQuery(AbstractMigrationScriptHelper.createMigrationScope(getMPSProject())), promon.subTask(100));
controller.process(promon.subTask(100), (List<SearchResult<SNode>>) results.getSearchResults());
promon.done();
}
private MPSProject getMPSProject() {
return project;
}
}