A source generator that generates C# code based on Sitecore Content Serialization (SCS) .yml
files.
⚠️ This package is highly experimental! It has not been tested very well.
⚠️ Visual Studio does not support generated code very well in < .NET 5 projects, and Sitecore needs a 4.x full framework version. This results in errors when editing (though it DOES work when building).
- Ensure that you have .NET 5 installed. It does not have to be used in your project, but you do need it for the source generator to work.
- Install the NuGet package in your project. Run the following command in the NuGet package manager console
Install-Package Fairy
- Ensure the SCS
.yml
files that you want to generate code for are added asAdditionalFiles
in your.csproj
file. E.g.:
<ItemGroup>
<AdditionalFiles Include="../serialization/**/*.yml" Visible="false" />
</ItemGroup>
- You can now use the generated classes in your code. Reference the following namespace to get to them.
using Templates;
If you have specific needs for the generated code, you can easily create a Scriban template to replace the default one. All you have to do is:
- Create a file named
FairyTemplate.sbntxt
and place it in the root of your project. - Ensure that the file is included in the
AdditionalFiles
for your project (the same way that you include your json files). - Copy the contents of the default template, paste them in the file and save. Or find a template you like under the templates that are used for testing
- Change the template in any way you want, and you should observe the changes when you build your project.
Because of the Visual Studio issue that is mentioned in the warning above, it may be helpful to view the generated source files. If you add the following to your project file, you can see the generated files in the obj\GeneratedFiles
folder.
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GeneratedFiles</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
It was based on ideas and code from:
- Leprechaun - code generation tool for Sitecore
- Helixbase - used mainly for testing
- RainbowDataAnalyzer (my own project)
- JsonByExampleGenerator (my own project)