Announcing Leprechaun

Leprechaun

I am very pleased to announce, albeit a bit late, the launch of Leprechaun! Leprechaun actually made its debut in April at SUGCON EU, but I didn’t write anything up on its release. I am really passionate about this project and I’d love to share it with you now!

What is Leprechaun?

Leprechaun is a universal code generator for Sitecore templates that reads in serialized yaml from Rainbow for use in Unicorn-based projects. The tool then generates models using Rosyln code generation for use in the solution.

What’s Special about Leprechaun?

Synthesis has native code generation support (for now), so you’re probably asking yourself “why would we build another tool to do what we already have?”

Universal Code Generator

Firstly, Leprechaun is not just for Synthesis. It can natively generate models for Habitat, Synthesis, and Glass. I have also personally used it on a project that used a combination of Fortis and Synthesis at the same time, and one instance of Leprechaun could generate models for both ORMs. The possibilities don’t end here. You can create custom C# Script (.csx) files to make Leprechaun generate models any way you’d like. The tool is very flexible.

Who Needs Context?

Synthesis requires a live, working, instance of Sitecore in order to generate templates. I’ve encountered scenarios occasionally where my instance may have been broken during development and my models are out of date. The solution? Comment out code or generate empty elements just to get the code to compile so that you can regenerate the model.

Leprechaun doesn’t need a connection to Sitecore at all! It reads directly from the serialized yaml files. If you need to make a tweak to a template, you change the yaml file directly. Since there’s no need for Sitecore connections, this opens up new possibilities for code generation! The tool is run via an executable which can be worked in as a pre-build event in MSBuild or as a command line / PowerShell task in your continuous integration or build process. You can even turn it into a gulp task for node apps.

Goodbye, Model File Merge Conflicts

Have you ever worked with a code generator that puts all of the classes in a giant single file? Have you had a merge conflict in that file? Fun to fix isn’t it? Say goodbye to those sorts of conflicts! Since model generation can now happen on the servers or as a pre-build step, you no longer need to commit these model files. Committing auto-generated files that frequently update is a bad practice and can get messy.

Easy to Read, Easy to Modify

Leprechaun uses C# Script (.csx) files as templates for generating code. These files are essentially big StringBuilders that are provided metadata in order to generate models. Take a peek at the Synthesis.csx file for example. To me, it looks like what would happen if T4 Templates were based in C#. Inside these script files, you can execute any C# code as you normally would. I’d recommend against going too crazy though, as you want to keep it readable.

Watch it, Buddy!

Leprechaun also has watch functionality! When you put it into watch mode, it will keep an eye on changes to template yaml files and automatically regenerate models when there are changes. For example, if you’re actively working on a feature and make changes to the template. Unicorn and Rainbow see that you changed the template and update the yaml file accordingly. Leprechaun sees that the yaml file has changed and regenerates your model. By the time you tab back into Visual Studio, your model is up to date and ready to work with the latest code!

How can I catch this Leprechaun?

The best way to download Leprechaun is via the Github releases: https://github.com/blipson89/Leprechaun/releases. Installation and initial configuration documentation can be found at the readme file at the root of the project (https://github.com/blipson89/Leprechaun) which can help you get started.

Need Help?

If you have any questions, feel free to join in on the conversation in Sitecore slack in the channel #leprechaun. You can also reach out to me directly in Sitecore slack, on Twitter, email, or carrier pigeon. Feel free to open up Github issues for feature requests or bug reports, and don’t be afraid to put in pull requests as well.