Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Code generator conditional behaviour(Looking to create optional behaviour in a code generator that can be controlled via a gradle project property)
Code generator conditional behaviour [message #1797002] Tue, 23 October 2018 19:04 Go to next message
Hubert Holierhoek is currently offline Hubert HolierhoekFriend
Messages: 3
Registered: October 2018
Junior Member
I've got a code generator that creates JPA entity classes, plus a gradle plugin that developers can include if they are defining a JPA domain model.

I'd like them to be able to control one (small) aspect of the code generation from the repo's build.gradle, ideally with a project property.

I don't know a lot about either gradle plugins, or xtext integration - for the most part I inherited this code - but perusing various bits of code I can't find a way to get information passed down to the generator code, or even a way to get the gradle project information from within the generator.

I had the generator output a full stack trace so I could see a full call-chain. I've perused the source for a bunch of classes in that trace and can't find a way to do this.

Has anyone found a way to do this?

Re: Code generator conditional behaviour [message #1797292 is a reply to message #1797002] Mon, 29 October 2018 15:24 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

There are many ways to solve that. At the end, the generator is "just" a Java program and you need to pass the property from Gradle through the main program to the template.
It highly depends on how the generator was implemented. Can't you ask the person from whom you inherited? Without knowing your project we can't give you concrete hints. However, it is possible to get remote support from us if needed.
Re: Code generator conditional behaviour [message #1797295 is a reply to message #1797292] Mon, 29 October 2018 15:46 Go to previous messageGo to next message
Hubert Holierhoek is currently offline Hubert HolierhoekFriend
Messages: 3
Registered: October 2018
Junior Member
I'll start by saying I no longer need this; I opted to add a new construct to the language to allow the developers to control the code generation. Not sure why I didn't think of that before, to be honest.

But maybe it'd be good to get an answer here in case I need it later, or maybe it'll help others.

The person I inherited it from, it turns out, tried to figure this out in the past for some other reason, but was never able to. I'll concede that neither of us is that adept at Gradle plugins either.

Grabbing the project property in the gradle plugin is easy. The difficulty as I see it is that ultimately the creation and execution of my generator is not in code that our gradle plugin seems to be able to control. So I can't figure out how to get information down to it. For starters the generator methods (runGenerator, doGenerate) that we implement don't have much in the line of arguments to get information through - a resource, file-system-access, and a context.

Are there specifics about my project that would help in understanding?
Re: Code generator conditional behaviour [message #1797298 is a reply to message #1797295] Mon, 29 October 2018 15:52 Go to previous messageGo to next message
Karsten Thoms is currently offline Karsten ThomsFriend
Messages: 762
Registered: July 2009
Location: Dortmund, Germany
Senior Member

Your generator is invoked with a main method. You can either pass the args to that and pass them through to the template, e.g. by an injectable context object, or - simple, ugly, but effective - pass it as a system arg. Within doGenerator you can of course access these system props. In my projects I prefer to explicitly create a proper CLI for the generator's entry point, parse the arguments and make them accessible by the templates by either the generator's context or by an injectable config singleton configured by the generator's argument in a custom Guice module. It will be easier for you to go the system args way.
Re: Code generator conditional behaviour [message #1797300 is a reply to message #1797298] Mon, 29 October 2018 15:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
i dont see that option.
either you build something completely on your own.
or you reuse the xtext-gradle-plugin.
then you need to pass the information via sidechannel.
e.g. system properties.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Code generator conditional behaviour [message #1797302 is a reply to message #1797300] Mon, 29 October 2018 16:40 Go to previous message
Hubert Holierhoek is currently offline Hubert HolierhoekFriend
Messages: 3
Registered: October 2018
Junior Member
Yes, we're using the xtext gradle plugin.

So I guess system properties are the answer. Seems a bit clunky, but I guess it'll do in a pinch.

Thanks.
Previous Topic:Generator does not use the order of the
Next Topic:Error creating Xtext artefacts
Goto Forum:
  


Current Time: Thu Apr 18 01:22:28 GMT 2024

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

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

Back to the top