running textgen by an intention

Hi, 

I have been trying to generate Textgen files for multiple file of same concept. So, I was thinking about using an intention to find all the instances of the concept and run Textgen. Is there any way of calling the Textgen action by code?

thanks in advance

3 comments

Here is a pretty dirty hack I did. Used it to textgen files to memory and then write them to file myself. Maybe you can use it in some way. But I hope someone else points you to a better solution, because this is really not pretty : 

 

final DefaultMakeMessageHandler msgHandler = new DefaultMakeMessageHandler(mpsProject); 
msgHandler.clear();
MakeSession session = new MakeSession(mpsProject, msgHandler, true);
if (INSTANCE.get().openNewSession(session)) {
  IScript scr = new ScriptBuilder().withFacetNames(&Generate, &TextGen, &Make).withFinalTarget(&TextGen:textGenToMemory).toScript();
  final Future<IResult> future = INSTANCE.get().make(session, new ModelsToResources(this.currentModule.getModels()).resources(false), scr);
  ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
    public void run() {
      try {
        final IResult result = future.get();
        final List<TextPreviewFile> previewFiles = new ModelAccessHelper(mpsProject.getModelAccess()).runReadAction(new Computable<List<TextPreviewFile>>() {
          public List<TextPreviewFile> compute() {
            ArrayList<TextPreviewFile> rv = new ArrayList<TextPreviewFile>();
            foreach tgr in result.output().ofType<TextGenOutcomeResource> {
              string modelName = NameUtil.compactNamespace(tgr.getModel().getModelName());
              for (TextUnit tu : tgr.getTextGenResult().getUnits()) {
                rv.add(new TextPreviewFile(tu, modelName));
              }
            }
            return rv;
          }
        });
         
        ApplicationManager.getApplication().invokeLater(new Runnable() {
          public void run() {
            try {
              long folderTime = TimeUnit.NANOSECONDS.toMicros(System.nanoTime());
              String folderString = "//////////outputFolder/Generation/" + folderTime;
              new File(folderString).mkdir();
              for (TextPreviewFile pFile : previewFiles) {
                FileOutputStream out = new FileOutputStream(folderString + "/" + pFile.getName());
                out.write(pFile.contentsToByteArray());
                out.close();
              }
              GenerationHelper.self.addGenTime(TimeUnit.NANOSECONDS.toMicros(System.nanoTime()));
            } catch (FileNotFoundException e) {
              warn "", e;
               
            } catch (IOException e) {
              <no statements>
            }
          }
        });
         
        // to update tree to reveal transient models. is it still necessary?
        ProjectPane.getInstance(mpsProject).rebuild();
         
      } catch (Exception e) {
        msgHandler.handle(new Message(MessageKind.ERROR, "TextPreviewModel", e.toString()).setException(e));
      }
    }
  });
   
 
     
  });
   
} else {
  warn " false";
}
0

Dear Jochen,

Thanks a lot for your response, how do I get the instance to MPS projects you mentioned in the first line? Thanks in advance!

0

That depends. From where do you want to invoke this code?

 

 

0

Please sign in to leave a comment.