sitecore-pathfinder
Version:
89 lines (72 loc) • 2.83 kB
Markdown
generate-code
===========
Generates code from items and files in the project.
Remarks
-------
Pathfinder can generate code based on your project. The most obvious thing is to generate a C# class for each template in the project.
To generate code, execute the task `generate-code`. This wil iterate through the elements in the project and check if a code generator
is available for that item. If so, the code generator is executed.
Normally you want to run the `generate-code` task before building an assembly, so the C# source files are up-to-date.
Razor
-----
Pathfinder supports code generation using Razor text templating.
By default Pathfinder will look for files with the extension ".tt.cshtml" in the [Project] directory and below. For each
file found, it will invoke the text templating engine and pass the project model object as a parameter. The output file will
be located in the same directory as the ".tt.cshtml" file, but the extension ".tt.cshtml" will be removed. To create a
"MyProject.cs" output file, name the Razor template file "MyProject.cs.tt.cshtml".
Example of a template file:
```
Sitecore.Pathfinder.Projects.IProject
Sitecore.Pathfinder.Extensions
(var template in Model.Templates)
{
var className = template.ItemName.GetSafeCodeIdentifier() + "Item";
<text>
<pagebreak file="@(template.ShortName).cs" />
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
// </auto-generated>
//------------------------------------------------------------------------------
#pragma warning disable 1591
using Sitecore;
using Sitecore.Data.Items;
namespace Sitecore
{
#region Designer generated code
[System.Diagnostics.DebuggerStepThrough()]
[System.CodeDom.Compiler.GeneratedCode("Sitecore.Pathfinder", "1.0.0.0")]
public partial class
{
public @(className)(Item innerItem)
{
InnerItem = innerItem;
}
[NotNull]
[System.CodeDom.Compiler.GeneratedCodeAttribute("Sitecore.Pathfinder", "1.0.0.0")]
public Item InnerItem { get; private set; }
</text>
(var section in template.Sections)
{
foreach (var field in section.Fields)
{
<text>
[NotNull]
[System.CodeDom.Compiler.GeneratedCode("Sitecore.Pathfinder", "1.0.0.0")]
public string .FieldName.GetSafeCodeIdentifier() { get { return InnerItem["@field.FieldName"]; } }
</text>
}
}
<text>
}
#endregion
}
#pragma warning restore 1591
</text>
}
```
This template generates a file per template in the project (notice the <pagebreak file=""> tag).