Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [Solved] Specify path of file to generate(How to generate file in custom path using xtext and xtend)
[Solved] Specify path of file to generate [message #1827174] Sat, 09 May 2020 10:00 Go to next message
Luca Reccia is currently offline Luca RecciaFriend
Messages: 14
Registered: May 2020
Junior Member
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 14:32]

Report message to a moderator

Re: Specify path of file to generate [message #1827175 is a reply to message #1827174] Sat, 09 May 2020 10:29 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 14667
Registered: July 2009
Senior Member
Hi. I did not get your question.
You specify the file name + path relative to the output path
Inside generator

EG

a/b/c.txt

and configure the rootdir / output path in the main / filesystem access

What of these do you want to change


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Sat, 09 May 2020 10:50]

Report message to a moderator

Re: Specify path of file to generate [message #1827180 is a reply to message #1827175] Sat, 09 May 2020 14:37 Go to previous message
Luca Reccia is currently offline Luca RecciaFriend
Messages: 14
Registered: May 2020
Junior Member
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!
Previous Topic:Creating Custom Task Tags
Next Topic:Xtext grammar to Java Code
Goto Forum:
  


Current Time: Fri Apr 26 17:48:22 GMT 2024

Powered by FUDForum. Page generated in 0.03072 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top