[Solved] Specify path of file to generate [message #1827174] |
Sat, 09 May 2020 06:00  |
Eclipse User |
|
|
|
This is my main code, I would like xtext generate the output file with a specific name instead of its random name.
At the moment it uses the outPath as folder in which it places the generated file.
I would like to create the file with name specified in the outPath.
I didn't manage to find anything in the documentation.
class Main2 {
def static main(String[] args) {
if (args.empty) {
System::err.println('Usage: <path_to_input_file> <path_to_output_file>')
return
}
val injector = new MGrammarStandaloneSetup().createInjectorAndDoEMFRegistration
val main = injector.getInstance(Main2)
main.runGenerator(args.get(0), args.get(1))
}
@Inject Provider<ResourceSet> resourceSetProvider
@Inject IResourceValidator validator
@Inject GeneratorDelegate generator
@Inject JavaIoFileSystemAccess fileAccess
def protected runGenerator(String inPath, String outPath) {
// Load the resource
val set = resourceSetProvider.get
val resource = set.getResource(URI.createFileURI(inPath), true)
// Validate the resource
val issues = validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl)
if (!issues.empty) {
issues.forEach[System.err.println(it)]
return
}
// Configure and start the generator
fileAccess.outputPath = outPath
val context = new GeneratorContext => [
cancelIndicator = CancelIndicator.NullImpl
]
generator.generate(resource, fileAccess, context)
System.out.println('Code generation finished.')
}
}
thanks
[Updated on: Sat, 09 May 2020 10:32] by Moderator
|
|
|
|
Re: Specify path of file to generate [message #1827180 is a reply to message #1827175] |
Sat, 09 May 2020 10:37  |
Eclipse User |
|
|
|
I'm sorry. There was a file renaming function create by some programmer before me. I thought it was some functionality from xtext that caused the random naming of files but luckily it was not.
Just as quick reference. In order to reuse the same file name as the input file and only changing the file extension i'm using this little ugly solution:
val uri = resource.getURI().toString()
val parts = uri.split("/")
val filename = parts.get(parts.length-1).replace(".mg", ".xpg")
fsa.generateFile(filename,positional.generatePositionalSource);
If there's any better solution i'll be pleased to know!
|
|
|
Powered by
FUDForum. Page generated in 0.03067 seconds