Creating a solution with preconfigured javasource_stubs Model Root

Hello everyone, 

I'm trying to create a plugin solution that creates a template project for the language that I am currently working on like the one in the screenshot below:

The structure of the project after creating it is shown in the logical view like this:

What I am trying to do is to add java_source_stubs as the Model Root that references the "./generated" sub-folder of the solution GeneratedJavaSources like the one in the screenshot below:

But the current status of the solution is:

I am implementing the "MPSProjectTemplate" interface to create the template solution. In the implementation of the "getTemplateFiller()" method I am creating the "GeneratedJavaSources" solution like

// create Solution for the generated sources
Solution genSourceSolution = NewModuleUtil.createSolution("GeneratedJavaSources", myNewSolutionSettings.getSolutionLocation(), project);

Is there a way to pre-configure the solution that is being created in the new project to first create the "./generated" sub-folder and then reference it as the javasource_stubs as the Model Root.

 

4 comments
Official comment

As Artem wrote, you need to use genSourceSolution.getModuleDescriptor().getModelRootDescriptors().

Here what you need to do:

First delete default model root if you do not need it:
genSourceSolution.getModuleDescriptor().getModelRootDescriptors().clear();

Create folder for generated java sources (in fact it is not necessary if it created somewhere else after):
string solutionLocation = genSourceSolution.getDescriptorFile().getParent().getPath(); // solution folder
string generatedFolder = "generated";
File file = new File(solutionLocation + File.separator + generatedFolder); // can be used any other way to create folder
if (!file.exists()) {
    file.mkdir();
}

Then create java source stubs model root with folder that you need:
Memento m = new MementoImpl();
m.put("contentPath", solutionLocation);
Memento child = m.createChild(FileBasedModelRoot.SOURCE_ROOTS);
child.put("location", generatedFolder); // relative path of folder with generated sources

Finally add model root to solution:
genSourceSolution.getModuleDescriptor().getModelRootDescriptors().add(new ModelRootDescriptor("java_source_stubs", m));

That's it.

For java classes it is even easier and can be done in just two rows:
ModelRootDescriptor javaStubsModelRoot = ModelRootDescriptor.getJavaStubsModelRoot(genSourceSolution.getOutputPath().getPath());
genSourceSolution.getModuleDescriptor().getModelRootDescriptors().add(javaStubsModelRoot);

Though it's not the most comfortable approach, you can modify Solution obtained from NewModuleUtil to host model root. To create .generated subfolder you should use regular VCS mechanism, and to attach it as a model root, check Solution.getModuleDescriptor().getModelRootDescriptors(). There you'd find default one (corresponds to /models location). Either replace it or add a new one (ModelRootDescriptor with "java_source_stubs" as type). Perhaps, you'll need to tell MPS to reload changes in the descriptor as well (I'm not that familiar with MPSProjectTemplate) with Solution.setModuleDescriptor(theOneWithChanges).

0

Hello Victor, Thank you very much for the reply. I tried doing what you said but I have a problem with the dependencies in this part: 

 

What models should I import for those ?

 

 

0

MementoImpl - MPS.Core/jetbrains.mps.persistence@java_stub

FileBasedModelRoot - MPS.Core/jetbrains.mps.extapi.persistence@java_stub

ModelRootDescriptor - MPS.Core/jetbrains.mps.project.structure.model@java_stub

 

You can easily add them by using action Add Model Import by Root (Ctrl+R): select unresolved root (for ex. MementoImpl), press shortcut (Ctrl+R) and choose corresponding root from dropdown list - model that contains needed root will be added as dependency to your model.

0

Please sign in to leave a comment.