I tried to follow this tutorial: http://t4-editor.tangible-engineering.com/blog/how-to-generate-multiple-output-files-from-a-single-t4-template.html
with visual studio 2015 (.Net 4.5)
Sample project with error: http://www.filedropper.com/t4fail
I created the Template1.tt with the following source:
<#@ include file="TemplateFileManagerV2.1.ttinclude" #>
<#@ Assembly Name="System.Core" #>
<#@ Assembly Name="System.Windows.Forms" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#
var manager = TemplateFileManager.Create(this);
#>
I added TemplateFileManagerV2.1.ttinclude
from template gallery to my project.
Then I got an error:
'Microsoft.VisualStudio.TextTemplating.IDebugTextTemplatingEngine' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.VisualStudio.TextTemplating.Interfaces.11.0, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
So I added references to
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating.11.0\v4.0_11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.TextTemplating.11.0.dll
and
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating.Interfaces.11.0\v4.0_11.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.TextTemplating.Interfaces.11.0.dll
to my project, but nothing changed.
The error was in the following method inside .ttinclude
public string GetTemplateContent(string templateName, TextTemplatingSession session)
{
string fullName = this.Host.ResolvePath(templateName);
string templateContent = File.ReadAllText(fullName);
var sessionHost = this.Host as ITextTemplatingSessionHost;
sessionHost.Session = session;
Engine engine = new Engine();
return engine.ProcessTemplate(templateContent, this.Host);
}
I replaced it with
public string GetTemplateContent(string templateName, TextTemplatingSession session)
{
string fullName = this.Host.ResolvePath(templateName);
string templateContent = File.ReadAllText(fullName);
var sessionHost = this.Host as ITextTemplatingSessionHost;
sessionHost.Session = session;
//Engine engine = new Engine();
return "";//engine.ProcessTemplate(templateContent, this.Host);
}
to check if the problem is indeed in dll and got:
'Microsoft.VisualStudio.TextTemplatingA30AC8B57EFC4307E43667FCD72F5E4857F498C5224AE0D43FFC74B3A98D4FA090794EF196648D62B1BC664AFBA5EDE831067D7D1768A759EBBE83426975F7AA.GeneratedTextTransformation' does not contain a definition for 'Host' and no extension method 'Host' accepting a first argument of type 'Microsoft.VisualStudio.TextTemplatingA30AC8B57EFC4307E43667FCD72F5E4857F498C5224AE0D43FFC74B3A98D4FA090794EF196648D62B1BC664AFBA5EDE831067D7D1768A759EBBE83426975F7AA.GeneratedTextTransformation' could be found (are you missing a using directive or an assembly reference?)
It seems, that it's not.
Add a text template file to your project and give it a name that has the extension . tt. To do this, in Solution Explorer, on the shortcut menu of your project, choose Add > New Item. In the Add New Item dialog box select Text Template from the middle pane.
T4 can be used to automate the generation of text files of any type and purpose. Scott Hanselman gave such an example by using a template for generating LINQ to SQL code.
I remember I found an easier way to do it back in 2010, but now, after looking across the web for that method, I couldn't find it again. So, after some digging, I managed to find it in an old source code repository. Here's how I did it back then, without making use of any external file or dependency:
<#@ 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) {
#>
Content <#= i #>
<#
// End of file.
SaveOutput("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.Remove(0, this.GenerationEnvironment.Length);
}
#>
Please note that I don't know who the original author of this method is. If you happen to know who he or she is, please leave a comment.
<#@ template hostSpecific="true"#>
on top of .tt
file solves everything.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With