Write T4 generated code to separate output files
Asked Answered
C

1

3

I am creating a .tt file that transforms text into model classes, to practice.

A .cs file is generated that with all models, but I would like each model to be saved in its own .cs file in a different folder.

What would be the best approach to achieve this?

Contradistinguish answered 22/3, 2019 at 9:29 Comment(2)
I am not sure what you mean by loose?Pollination
Separate files, instead of packed in 1 fileContradistinguish
K
6

Here is simple example how you can output multiple files from single T4 template.

Using SaveOutput-method output files (Content1.txt,Content2.txt..) are created to same folder than .tt-file, with SaveOutputToSubFolder output files goes to separate folders (1\Content1.txt, 2\Content2.txt..)

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ output extension=".txt" #>
<#
for (Int32 i = 0; i < 10; ++i) {
#>
File Content <#= i #>
<#

  SaveOutput("Content" + i.ToString() + ".txt");
  //Uncomment following to write to separate folder 1,2,3
  //SaveOutputToSubFolder(i.ToString(),"Content" + i.ToString() + ".txt");
}
#>
<#+
private void SaveOutput(string outputFileName) {
  string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
  string outputFilePath = Path.Combine(templateDirectory, outputFileName);
  File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString()); 
  this.GenerationEnvironment.Clear();
}
private void SaveOutputToSubFolder(string folderName, string outputFileName) {
  string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
  string newDirectoryName = Path.Combine(templateDirectory,folderName);
  if(!Directory.Exists(newDirectoryName))
    Directory.CreateDirectory(newDirectoryName);
  string outputFilePath = Path.Combine(newDirectoryName, outputFileName);
  File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString()); 
  this.GenerationEnvironment.Clear();
}
#>
Knockout answered 23/3, 2019 at 7:0 Comment(5)
Thanks! I fixed it by using StringBuilder and use StreamWriter to write to specific path. I will try your solution too!Contradistinguish
Any reason you use this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length) as opposed to just this.GenerationEnvironment.Clear()? Otherwise this code is awesome and we use it with minor tweaks (for example having it output UTF-8) Thank you!Halden
@Halden Good to hear that my snippet helps. Unfortunately I don't remember any particular reason for not using .Clear(). I made quick test and it seem to work on my machine. If it works also in your machine with .Clear(), I can amend my answer since .Clear() is more elegant..Knockout
@RistoM It does thank you so much for this it greatly simplified our T4 transforms as we were able to ditch TemplateFileManager (T4.Helper) which allows us to automate using TextTransform.exe and not rely on Visual Studio.Halden
@Halden I edited answer, thanks for your contribution!Knockout

© 2022 - 2024 — McMap. All rights reserved.