Part - 2 : Integration of Leprechaun Code Generation to a project

 In my previous blog, I have explained about the Leprechaun Code Generation Setup and installation for Sitecore CLI you can refer before reading this blog for better understanding of the context.

Leprechaun Config file configuration 

I have configured the Leprechaun.config file for a project and can be find in my Git for your reference. Below I'm explaining about important configuration changes need to done for Leprechaun file and this blog more focus for Sitecore CLI using glass mapper for code generation.
  • This configuration node contains the module.json file incase if there is any path change or file name changes can be update here.
  • This configuration contains the base project where your code generation class files.
  • Here I'm using Code Generator script as GlassMapper.csx and we can use any thing which we have downloaded and showed in my first blog. If we want to use multiple code generator script you can use comma delimited. The model.cs output file 
  • Template Predicate rootnamespace contains model name space where you can configure as per your project and this is your model name space.
  • In case, if we need "_" infront of the class name then use the below setting to true otherwise you can set it to false.

Integrate Leprechaun to solution

This section explains the changes to perform to solution for integrate the Leprechaun code generation.
  • The highlight snippet for Leprechaun need to add in the Sitecore CLI module.json file as shown below where extends contains the base ORM class for the model to generate and name two values (i.e., Layer and modules) here layer refers feature and module name refers to Navigation. Based on these value the code generation will happen based on the above configuration and generate accordingly.
  • This configuration is specific to this file and model, obviously each projects has multiple models and in each module.json file should have similar entry to have model generated for for all the projects.
  • In the leprechaun config file, contains Templates based on this name code generator filter the template fields to generate code and the same name should match in module.json file too and this is very mandatory and important step.
  • If you have multiple type of templates you can have multiple entries but try to make it more consistent.
  • Now run below command to generate the model in your respective project solution folders with the modulename.cs in this case it is navigation.cs file.
dotnet Leprechaun -c .\Leprechaun.config 

Alter the model generation for model properties

There is an option to alter the model generation for few fields like droplink, multilist, droptree, etc., where in general it will have guid but there is an option to have model for the property and in the model the properties will generate based on the glassmapper.csx file.

For example:

Actual Property Generated public virtual Guid LinkBehavior { get; set; }
Expected Property Generated public virtual IUtilityBehavior LinkBehavior { get; set; }

Now Instead of Guid we wanted to have IUtilityBehavior then below steps need to be followed to customize this change.

There is an option to alter the model generation for few fields like droplink, multilist, droptree, etc., where in general it will have guid but there is an option to have model for the property by following below steps.

  • Add the field entry as below where it should have field id and type of the field.
  • Now navigate to the file "GlassMapper.csx" in your solution as shown below.
  • Go to the following method "GetFieldType" in the GlassMapper.csx and add the entry as shown below.
  • Now rerun the Leprechaun code generator command and the model will update with the new changes. In the above example I have shown some different examples based on the usecase and modify it as per your requirement.'
  • These models will generate based on the module.json file and its associated serializes yaml files.

Conclusion

Leprechaun, with its customizable templates and build-time generation, is a developer’s secret weapon. Whether you’re weaving Helix-friendly modules or sprinkling code like fairy dust, Leprechaun ensures your projects shine with efficiency and enchantment. 

If you have any more information or need further assistance, don’t hesitate to ask. 

Happy Code Generation 😇😇😇

Comments