Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a folder using .tt template?

Tags:

c#

.net

t4

I'm trying to build a template that will create a series of files in different folders but I haven't found any samples.

like image 734
MikeW Avatar asked Oct 08 '22 02:10

MikeW


1 Answers

You can use RenderToFile from t4Toolbox to do this.

Snippet from documentation example as of 2016.10.12:

  • Create a Visual Studio solution with two C# Class Library projects ClassLibrary1.csproj and ClassLibrary2.csproj.

  • Add a new code generation file called CodeGenerator.tt to the first class library project.

  • Modify contents of the new file to look like so

<#@ template language="C#" hostspecific="True" debug="True" #>
<#@ output extension="txt" #>
<#@ include file="T4Toolbox.tt" #>
<#
    SampleTemplate template = new SampleTemplate();
    template.Output.File = @"SubFolder\SampleOutput.txt";
    template.Output.Project = @"..\ClassLibrary2\ClassLibrary2.csproj";
    template.Render();
#>
<#+
    public class SampleTemplate : Template
    {
        public override string TransformText()
        {
            this.WriteLine("Hello, World!");
            return this.GenerationEnvironment.ToString();
        }
    }
#>

Original Documentation

Wayback Machine

like image 122
krystan honour Avatar answered Oct 12 '22 10:10

krystan honour