IntentArchitect / Support

A repository dedicated to handling issues and support queries
3 stars 0 forks source link

Disable creation of file in specific scenario #24

Closed leonAtRain closed 1 year ago

leonAtRain commented 1 year ago

Ask a question

I'm adding the Drool engine to my project and in the module that provides the functionality, I need a common class -but that common class needs to rely on a specific Domain, which is ConstraintRef - I don't want to create the domain if it's not there, I would rather not add the common Drool compiler.

I want to do something like this:

  public override ITemplate CreateTemplateInstance(IOutputTarget outputTarget)       {           if (_metadataManager.Domain(outputTarget.Application).GetClassModels().Any(c => c.Name.Equals("ConstraintRef")))           {               return new DroolRuleCompilerTemplate(outputTarget);           }           // don't create the file:           return IJavaTemplate;       }

dandrejvv commented 1 year ago

Hi @leonAtRain. To control whether a Template generates code or not can be achieved in one of two ways:

Make use of a custom Registration that controls whether Templates even get created or not.

In your Module Builder Designer, select the Template in question and change the Type to Custom.

image

The Template's Registration class will now have a different method DoRegistration (instead of CreateTemplateInstance) which allows you to control whether Templates get created and registered at all depending on a certain criteria (i.e. "is there a class with the name ConstraintRef ?")

[IntentManaged(Mode.Fully, Body = Mode.Ignore)]
public void DoRegistration(ITemplateInstanceRegistry registry, IApplication applicationManager)
{
    if (_metadataManager.Domain(applicationManager).GetClassModels().Any(c => c.Name.Equals("ConstraintRef")))
    {
        registry.RegisterTemplate(TemplateId, project => new DroolRuleCompilerTemplate(project, null));
    }
}

Thus, if a Template is not registered, it won't be created and so there won't be an output file generated by the Software Factory.

Have the Template override the CanRunTemplate() method so that even on registration, it doesn't generate any code.

Alternatively, in your Template Partial class, override the CanRunTemplate() method like so (assuming you have your Template Type set to Single File in your case):

public override bool CanRunTemplate()
{
    return Model.Any(c => c.Name.Equals("ConstraintRef"));
}

This will determine whether this Template instance can generate code or not based on the CanRunTemplate result.

leonAtRain commented 1 year ago

I have decided to use the override method, which seemed a lot simpler, but because I have the Single file option, I implemented it like this:

    public override bool CanRunTemplate()
        {
            return ExecutionContext.MetadataManager.Domain(ExecutionContext.GetApplicationConfig().Id)
                 .GetClassModels().Any(c => c.Name.Equals("ConstraintRef"));
        }

Then I see this in the SF :

image

dandrejvv commented 1 year ago

Yes the Software Factory will highlight a Template like that as Skipped whereas the first method won't even feature the Template in the Software Factory output log if it didn't meet a certain criteria.