本文是小编为大家收集整理的关于如何使用Tangible Editor从一个T4模板创建多个输出文件?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
与Visual Studio 2015(.net 4.5)
带错误的示例项目: http://wwwww.filedropper.com/t4fail
我创建了带有以下源的template1.tt:
<#@ 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); #>
我添加了TemplateFileManagerV2.1.ttinclude从模板画廊添加到我的项目.
然后我有一个错误:
'microsoft.visualstudio.texttemplating.idebugtextTemplatingEngine'is 在未引用的组件中定义.您必须添加一个 参考组装 'microsoft.visualstudio.texttemplating.interfaces.11.0, 版本= 11.0.0.0,文化=中性,publicKeytoken = b03f5f7f11d50a3a'.
所以我将引用添加到
c:\ windows \ microsoft.net \ assembly \ gac_msil \ microsoft.visualstudio.texttemplating.11.0 \ v4.0_11.0.0.0.b0_b03 f5f7f1l1d50af11d50a3a
和
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
我的项目,但没有任何改变.
错误是在以下方法中.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); }
我用
代替了它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); }
检查问题是否确实在DLL中并得到:
'Microsoft.VisualStudio.TextTemplatingA30AC8B57EFC4307E43667FCD72F5E4857F498C5224AE0D43FFC74B3A98D4FA090794EF196648D62B1BC664AFBA5EDE831067D7D1768A759EBBE83426975F7AA.GeneratedTextTransformation' 不包含"主机"的定义,也不包含扩展方法 "主机"接受类型的第一个论点 'Microsoft.VisualStudio.TextTemplatingA30AC8B57EFC4307E43667FCD72F5E4857F498C5224AE0D43FFC74B3A98D4FA090794EF196648D62B1BC664AFBA5EDE831067D7D1768A759EBBE83426975F7AA.GeneratedTextTransformation' 可以找到(您是否缺少使用指令或装配 参考?)
似乎不是.
推荐答案
<#@ template hostSpecific="true"#>
在.tt文件的顶部求解所有内容.
其他推荐答案
我记得我找到了一种更简单的方法来在2010年进行,但是现在,在网上寻找该方法后,我再也找不到了.因此,经过一番挖掘,我设法在旧的源代码存储库中找到了它.这是当时我做到的,而无需使用任何外部文件或依赖性:
<#@ 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); } #>
请注意,我不知道该方法的原始作者是谁.如果您碰巧知道他或她是谁,请发表评论.
其他推荐答案
这是有形工程的文档:
它要求您包括并使用模板文件管理器.步骤简短如下:
- 从他们的免费代码库获取文件经理(在主模板文件中包括文件管理器:
<#@ include file="TemplateFileManagerV2.1.ttinclude" #> - 实例化经理:
<# var manager = TemplateFileManager.Create(this); #> - 使用经理启动一个新文件:
<# manager.StartNewFile("Outputfile2.txt"); #>
请注意,这将在开始下一个之前结束先前启动的文件. - 为正常生成模板代码(将在新文件中生成,直到启动另一个文件或在下面处理文件)
- 最终确定所有文件(将在先前启动的文件结束):
<# manager.Process(); #>
另外,此方法将自动将新文件添加到项目中.
更新 - 图片包括
第1部分:生成多个输出文件
创建模板
包括模板画廊中的可重复使用的模板管理器
测试输出到多个文件
第2部分:不同项目中的多个输出文件
问题描述
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.
推荐答案
<#@ template hostSpecific="true"#>
on top of .tt file solves everything.
其他推荐答案
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.
其他推荐答案
This is the documentation from Tangible Engineering:
It requires you to include and use the Template File Manager. The steps are briefly as follows:
- Obtain the file manager from their free code gallery (https://t4-editor.tangible-engineering.com/Download_T4Editor_Plus_ModelingTools.html)
- Include the file manager in the main template file:
<#@ include file="TemplateFileManagerV2.1.ttinclude" #> - Instantiate a manager:
<# var manager = TemplateFileManager.Create(this); #> - Use the manager to start a new file:
<# manager.StartNewFile("Outputfile2.txt"); #>
Note that this will end a previously started file before starting the next. - Generate template code as normal (will be generated in new file until another file is started or files are processed below)
- Finalize all files (will end previously started file):
<# manager.Process(); #>
Also, this method will automatically add the new files to the project.
Update -- pics included
Part 1: Generate multiple output files
Create your template
Include reusable Template Manager from Template Gallery
Test the output to multiple files
Part 2: Multiple output files in different projects