Skip to content

Instantly share code, notes, and snippets.

@seankearney
Last active May 1, 2018 13:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save seankearney/5957503 to your computer and use it in GitHub Desktop.
Save seankearney/5957503 to your computer and use it in GitHub Desktop.
This TDS Code Generation template is an example of how you can generate a multiple files that contains a single class in each.
<#@ template language="C#" hostSpecific="true" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="HedgehogDevelopment.SitecoreProject.VSIP.CodeGeneration.Models" #>
<#@ parameter name="Model" type="HedgehogDevelopment.SitecoreProject.VSIP.CodeGeneration.Models.SitecoreItem" #>
<#@ parameter name="DefaultNamespace" type="System.String" #>
<#
/************************************************************
This TDS Code Generation template is an example of how
you can generate a multiple files that contains
a single class in each.
WARNING:
**This is unsupported by Hedgehog Development.**
Using this method of code generation will likely lead
to orphaned code files as the files aren't deleted
if a Sitecore template is removed from the project.
Remember that the generated file can be overwritten
at any time by TDS so do not make changes to the
generated file. Use partial classes in a different
file to expand the generated class.
************************************************************/
#>
<#
// Only act on templates
if(!(Model is SitecoreTemplate))
{
return string.Empty;
}
SitecoreTemplate Template = (SitecoreTemplate)Model;
#>
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by Team Development for Sitecore.
// http://TeamDevelopmentForSitecore.com
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
namespace <#=Template.Namespace#>
{
/// <summary>
/// <#= Template.Name #>Item
/// <para>Path: <#= Template.Path #></para>
/// <para>ID: <#= Template.ID.ToString() #></para>
/// </summary>
public partial class <#= Template.Name #>
{
<#foreach(SitecoreField field in Template.Fields){#>
/// <summary>
/// The <#=field.Name#> field.
/// <para>Field Type: <#=field.Type#></para>
/// <para>Field ID: <#=field.ID.ToString()#></para>
/// <para>Custom Data: <#=field.Data#></para>
/// </summary>
public string <#= field.Name #> { get; set; }
<#}#>
}
}
<#
/*****************************************************/
// In addition to the general TDS code generation
// manually save the output of this transformation
// to a separate file
SaveOutput();
/*****************************************************/
#>
<#+
/// <summary>
/// The Model passed to this t4 template doesn't provide us with enough information
/// about the project that is configured in the TDS Code gen properties screen.
/// Therefore, the files will be stored relative to this .tt file.
/// Multiple outputs of T4 doesn't include them in the project and manual
/// adding/deleting from project would be requried
/// </summary>
/// <param name="outputFileName"></param>
void SaveOutput()
{
// we will store the files according to the namespace
// i.e. each namespace segment will represent a path segment
// My.Namespace.For.The.Class --> My\Namespace\For\The\Class\[Model.Name].cs
string fileNamespace = Model.Namespace;
string outputFileName = string.Concat(fileNamespace.Replace('.', '\\'), "\\", Model.Name, ".cs");
/// Feel free to adjust to fit your project structure
string templateDirectory = System.IO.Path.GetDirectoryName(Host.TemplateFile);
string outputFilePath = System.IO.Path.Combine(templateDirectory, "MultipleOutputs", outputFileName);
// Ensure the path exists
System.IO.FileInfo f = new System.IO.FileInfo(outputFilePath);
if (!f.Directory.Exists)
{
f.Directory.Create();
}
// Create the file and write out the contents
System.IO.File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString());
// This line will prevent the output of the transformation from being written to the single file.
this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
}
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment