I was looking for a way to automatically generate Glass Models for my Sitecore Templates that had been serialized using Unicorn’s Rainbow YAML serialization formatter and came across this post by Sitecore MVP Robin Hermanussen – http://hermanussen.eu/sitecore/wordpress/2015/04/generating-sitecore-code-without-tds
After reading the documents on GitHub it sounded perfect so I forked the repo and took a look at what the project was doing – https://github.com/hermanussen/sitecore.codegenerator
My first step were :-
- Updated to .Net4.6.1
- Used Visual Studio 2017 with T4 templates for 2017 – https://github.com/hagronnestad/T4Toolbox/releases/tag/vs2017-b1
- Used Sitecore Nuget Feed for Sitecore dlls.
- Targeting 8.2.170728 Sitecore 8.2 Update 5
Then I made some updates to the models that were being outputted using the T4 helpers from the tds-codegen project – https://github.com/HedgehogDevelopment/tds-codegen
Changed to GlassGenerator :-
- Switched to Rainbow Provider
- Supported more Sitecore field types
- Changed file name to *.cs instead of *.gen.cs
Changes to GlassMappedClassTemplate :-
- Namespace uses the default namespace and the template location.
- Static Class for Template and Field Values
- Used static classes for SitecoreType TemplateId
- Used static classes for SitecoreField Name
- Added inheritance of IGlassBase interface
Here is the original output :-
And this is what Sitecore.CodeGenerator is outputting now with my changes:-
Have a play with the code – https://github.com/Wesley-Lomax/sitecore.codegenerator and checkout Robins repo – https://github.com/hermanussen/sitecore.codegenerator I’ve found it a great timesaver for boiler plate models for your renderings.