Search code examples
xtext

How to replicate source code folder structure for generated code


I created a simple DSL in Xtext. All my source files (".src") are in a set of folders under "src". I want the generated files (".obj") to be created inside "src-gen", but preserving the folder structure of the source files.

The below (default) code outputs all files in the same folder ("src-gen").

override void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {

  // Root node of parsing tree

  val Program prg = resource.allContents.filter(Program).next()

  // Compiled code

  var String code = prg.compile().toString()

  // change file extension

  val outName = ....

  // Generate output

  fsa.generateFile(outName, code)

  ...

If file A.src is in folder src/one and B.src is in folder src/two I would like that A.obj to be created in src-gen/one and B.obj in src-gen/two.


Solution

  • You can use the resources getURI() to obtain the uri of the resource. then you can use that uri to find the segment with src and then take the following segments to get the path