2

I want to use the script-name in the generator to create the corresponding java file. For example if my script file would be "WordCount.script" I want to create a "WordCount.java" file. I found out the previous versions exposed this via "resource.className" but it did not work for 2.3.1.

override void doGenerate(...

   fsa.generateFile(magic_here + ".java", compile...)
moin moin
  • 2,263
  • 5
  • 32
  • 51

3 Answers3

4

You can access the normalized URI for your Resource using ECoreUtil2. For example;

import static extension org.eclipse.xtext.EcoreUtil2.*

....
override void doGenerate(Resource input...

    fsa.generateFile(input.normalizedURI.lastSegment + ".java", compile...)

Or, if you want an absolute path, you can leave off the lastSegment. You may want to strip the filename off the end (".mydsl" for example) before generation, such as in:

    fsa.generateFile(input.normalizedURI.replace(".mydsl", ".java), compile...

Whatever suits your use case!

Colonel Panic
  • 929
  • 8
  • 15
0

The resource has a property URI that encapsulates the name of the file. You may want to access the #lastSegment of it to compute the name of the Java file.

Sebastian Zarnekow
  • 6,609
  • 20
  • 23
0

Here is an example:

 fsa.generateFile(
    "src"+"/"+"com"+"/"+"stackoverflow"+"/"+"magic"+"/"+ //package
    "more_magic"+".java", //class name
    compile...)

The package will be in the src folder and will be 'com.stackoverflow.magic' You will only see it as a package once you import the files into a project.

Charles Henry
  • 353
  • 3
  • 16
  • I think the question was about how to get the information which you assumed to be just constant. Also: Why would anyone want to concatenate _constant_ strings by hand in this way? – A.H. May 18 '13 at 07:44