Abort template output from within template code

Posts   
 
    
worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 08-May-2007 02:39:50   

Hello,

I have a set of templates we use at work to generate our web apps UI. I'd like to put a check in some of the templates to determine whether they actually should be outputted.

This check will determine if it is appropriate to output the template for a the current entity. For some templates regardless of the entity I still want the file to be generated, however for other templates I don't want any output (if it's a M2M join table for example).

Is there someway in the code of the template I can instruct the generator to abort generating the current template for the current entity and move on to the next?

worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 08-May-2007 03:39:19   

I came up with this:


if (CurrentEntity.PrimaryKeyFields.Count == CurrentEntity.Fields.Count){
        string outputPath = Path.Combine(Path.Combine(_executingGenerator.ProjectDefinition.DestinationFolder, _parameters["destinationFolder"].Value), _parameters["filenameFormat"].Value.Replace("[elementName]", ObjectName));
        
        __outputWriter.Close();
        File.Delete(outputPath);
        
        return;
    }

It'd be nice(r) if i could return false or if there was some object available which I could user to provide the generator with instructions such as do not output the file.

Rant: I can't believe Path.Combine doesn't accept a params string[] arg. What are MS thinking? Oh no, no one would ever want to combine more than two parts of a path. confused

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39774
Joined: 17-Aug-2003
# Posted on: 08-May-2007 15:42:23   

worldspawn wrote:

I came up with this:


if (CurrentEntity.PrimaryKeyFields.Count == CurrentEntity.Fields.Count){
        string outputPath = Path.Combine(Path.Combine(_executingGenerator.ProjectDefinition.DestinationFolder, _parameters["destinationFolder"].Value), _parameters["filenameFormat"].Value.Replace("[elementName]", ObjectName));
        
        __outputWriter.Close();
        File.Delete(outputPath);
        
        return;
    }

It'd be nice(r) if i could return false or if there was some object available which I could user to provide the generator with instructions such as do not output the file.

At the moment it's not possible to specify whether a generate task should actually be terminated or continue. What people often do is derive a class from the task performer used (e.g. the lpt template engine) and in there override the Perform method, though it's not that fine-grained: it cancels the whole task for example. Another way is to modify the sourcecode of the taskperformer a bit so it consumes the data for the check.

Frans Bouma | Lead developer LLBLGen Pro
worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 10-May-2007 03:21:50   

Is this a feature I can look forward to in V2.1?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39774
Joined: 17-Aug-2003
# Posted on: 10-May-2007 10:36:29   

worldspawn wrote:

Is this a feature I can look forward to in V2.1?

It's a little hard to do. Not the exclusion of a file output, but how should the user specify which criteria should be true before task T is executed... At the moment, the generator doesn't have this functionality and it's a bit hard to add this as well, as it comes down to the fact that there should be a possibility that a little program is ran before a task is executed which returns true or false, and if true, the task will execute, otherwise it will be false.

But it goes further than that: what if you're generating files in a loop using a single task and you want to exclude certain entities per task ? or exclude entities based on a rule or set of rules?

It's a nice idea, and I've thought a lot about solving the problem in one way or the other, but I couldn't find a proper solution which was simple.

That's also why people simply modify the task performer a bit to for example load an .xml file to weed out files they don't want for a given task.

Which particular things would you like to be able to specify in the tasks / preset for example, related to control flow ?

Frans Bouma | Lead developer LLBLGen Pro
worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 14-May-2007 03:00:17   

Actually I was more hoping for just some simple way to abort the executing template from within template code. I don't think there's a requirement to have a 3rd boxed bit of code that evaluates whether a template should be executed for each specific entity. The template itself should know if it should be running or not (at least in my scenario it does).

So in that little sample code I posted I close the outputwriter and delete the file. I'm just proposing a more elegant (and supported) way of doing that, ie. calling some method on the executing generator (or other appropriate class) that stops the current template execution and throws away any output it's created.

Something like: __executingGenerator.SkipCurrent();

So always execute the task, and then let the template decide whether to keep the output or not. I'm trying to handle the case where it's not appropriate for the template to run because one or more characteristics of the entity make it invalid for that template. I think you're thinking about handling a more sophisticated scenario where at the project/or task definition level you can evaluate whether or not the template should run based on some more project level evaluation (such as does the entity name being "B").

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39774
Joined: 17-Aug-2003
# Posted on: 15-May-2007 11:26:49   

We're indeed talking about different things.

There are 2 levels of excluding a file: - simply don't execute the task or - during the execution of a task, skip an element which was about to be processed by the task performer.

You added a 3rd: - during the execution of a task, during the processing of an element, abort the current processing of this element altogether, though keep on processing other elements if necessary by the same task performer.

the first 2 aren't implemented, the 3rd one you added could be implemented indeed. In TDL this isn't really possible though in .lpt it can be done indeed. I'll see what I can do for the coming upgrade to have some kind of mechanism to support this. Thanks for reporting this simple_smile

Frans Bouma | Lead developer LLBLGen Pro
worldspawn avatar
worldspawn
User
Posts: 321
Joined: 26-Aug-2006
# Posted on: 16-May-2007 04:01:56   

Cool, thanks Frans.