-1

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.

Christian Dietrich
  • 11,778
  • 4
  • 24
  • 32
mzattera
  • 13
  • 5

1 Answers1

0

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

Christian Dietrich
  • 11,778
  • 4
  • 24
  • 32
  • Hi, thanks for your reply. I was hoping there was some configuration parameter I missed :) Also, I do not like the idea of the solution to depend on hard-coded folder names (like "src"). – mzattera Oct 23 '19 at 07:14
  • you can use java api (JavaCode / IJavaProject / IPackageFragmentRoot) to find out what sourcefolders are. or you try to use StorageToUriMapper org.eclipse.xtext.builder.BuilderParticipant.getCurrentSourceFolder(IBuildContext, Delta) (this is eclipse ui api so you need some means to handle this in standalone and ui mode differently) – Christian Dietrich Oct 23 '19 at 20:32