Giter VIP home page Giter VIP logo

juan-dvd / rainbowcodegeneration Goto Github PK

View Code? Open in Web Editor NEW

This project forked from heikof/rainbowcodegeneration

0.0 0.0 0.0 1.38 MB

A simple set of utility classes and a sample T4 template that allow easy code generation for Sitecore templates from Rainbow / Unicorn serialized items. Currently, only the YAML serialization format is supported.

License: MIT License

C# 89.04% Pascal 10.96%

rainbowcodegeneration's Introduction

RainbowCodeGeneration

NuGet

A simple set of utility classes and a sample T4 template that allow easy code generation for Sitecore templates from Rainbow / Unicorn serialized items. Currently, only the YAML-based serialisation format is supported.

Using the Rainbow / Unicorn code generation

You are probably already using Unicorn with the YAML serialisation format (you really should - it's awesome!). I am also assuming that you serialise your templates in a dedicated folder inside your solution, ideally in a modular fashion like in Sitecore Habitat. Using the Rainbow / Unicorn code generation is simple:

  • Install the nuget package
  • Configure the T4 template
  • Re-Run the code generation when Sitecore templates change

Installing the nuget package

Install the RainbowCodeGeneration package. This installs a set of simple utility classes and an example T4 template. The template will generate an empty SitecoreTemplates struct in your project.

Empty Sitecore Template

Configuring the T4 template

Adjust a few settings on the T4 template SitecoreTemplates.tt to make it run in your configuration.

  • Ensure the assembly references at the top of the file resolve correctly (e.g. the path to your Sitecore.Kernel.dll). I have added an example based on NuGet packages however it is likely that you need to update the version numbers. The code generation is compatible with Unicorn 3 and 4 (Rainbow 1 and 2).
  • The physicalFileStore setting needs to point to where you store the Unicorn / Rainbow items for your project. The out of the box setting uses the relative paths that Habitat uses.
  • The treeName setting is the name of the sub-tree in Unicorn. This is the name of the folder for your Unicorn predicate to generate code for.
  • The treePath setting is the matching path in Sitecore. The "News" feature in Habitat would use "/sitecore/templates/Feature/News"

Adjust Settings

  • For Sitecore 9 you also need to install the NuGet package for Microsoft.Extensions.DependencyInjection in version 1.0.0 and reference the DLLs. The configuration then looks as follows (for Sitecore 9 using Unicorn 4):
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<# // NOTE - Reference your NuGet packages for Rainbow and RainbowCodeGeneration here #>
<#@ assembly name="$(SolutionDir)packages\Rainbow.Core.2.0.0\lib\net452\Rainbow.dll" #>
<#@ assembly name="$(SolutionDir)packages\Rainbow.Storage.Yaml.2.0.0\lib\net452\Rainbow.Storage.Yaml.dll" #>
<#@ assembly name="$(SolutionDir)packages\RainbowCodeGeneration.0.2.0\lib\net452\RainbowCodeGeneration.dll" #>
<# // NOTE - Reference your Sitecore.Kernel.dll and Sitecore.Logging.dll here #>
<#@ assembly name="$(SolutionDir)packages\Sitecore.Kernel.NoReferences.9.0.171002\lib\net462\Sitecore.Kernel.dll" #>
<#@ assembly name="$(SolutionDir)packages\Sitecore.Logging.NoReferences.9.0.171002\lib\net462\Sitecore.Logging.dll" #>
<#@ assembly name="$(SolutionDir)packages\Microsoft.Extensions.DependencyInjection.Abstractions.1.0.0\lib\netstandard1.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll" #>
<#@ assembly name="$(SolutionDir)packages\Microsoft.Extensions.DependencyInjection.1.0.0\lib\netstandard1.1\Microsoft.Extensions.DependencyInjection.dll" #>
<# 
// CONFIGURATION
var physicalFileStore = @"D:\inetpub\wwwroot\sc900\Data\Unicorn"; // the path to your serialisation items
var treeName = "Feature.Sample\\Templates"; // the name of the configuration you want to code-generate 
var treePath = "/sitecore/templates/Sample"; // the matching path in Sitecore for the configuration

The code generation for your Sitecore templates will run when you save the T4 template.

You can tweak the T4 template to your liking. The entire IItemData object from Rainbow for the template and its fields is available within the template.

Re-run code generation

When your Unicorn / Rainbow items update, you will have to re-run the code generation. In Visual Studio, right-click on the T4 template and select "Run Custom Tool" to re-run the code generation.

Adjust Settings

Generate code for any item (not just templates)

Starting in version 0.3 it is possible to generate code based on any item. For an example T4 template, refer to SitecoreKnownItems.tt in the test project of the repository.

Known issues

Re-serialisation fails if Visual Studio solution is open

Re-serialising an entire configuration fails if Visual Studio is open and a code generation is covering the configuration. Visual Studio appears to retain a lock on the folder and Unicorn cannot remove it. Close the solution and re-open it after you re-serialised the configuration.

rainbowcodegeneration's People

Contributors

heikof avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.