如何禁用 #line 指令写入 T4 生成输出文件
我在生成 T4 代码时遇到了一个小问题。
我已将 T4 模板分解为单独的文件并将它们放置在不同的目录中,我这样做了,因此我的部分代码生成可以在多个项目中重复使用,例如模型生成、存储库生成和服务生成都包含核心 EntityGeneration .tt 文件。
不幸的是,当 TextTemplate 解析我的嵌套包含时,它会在生成的 .cs 文件中构建一个很长的 #line 预处理器指令,将所有相对路径组合到最低级别的包含文件。
不幸的是,由于该路径是用相对路径构建的,因此它最终会变得不必要的长,实际上长到超过了最大路径长度(Windows 7)。
如果您感兴趣,这是生成的代码中的错误行:
#line 3 "C:\VS2010\AlbatrossTravelGroup\ASC\AlbatrossTravelGroup.ASC.BusinessRules\Services\Contracts\..\..\..\..\AlbatrossTravelGroup.BusinessRules\Services\Contracts\..\..\..\AlbatrossTravelGroup.Models\Repositories\Contracts\..\..\../AlbatrossTravelGroup.Common/CodeGeneration.tt"
我的问题是,如何禁用这些指令写入生成的代码文件?如果做不到这一点,如何在不更改文件结构的情况下避免此问题?
I have encountered a small problem with my T4 code generation.
I have broken my T4 templates up into separate files and placed them in various directories, I have done this so parts of my code generation may be re-used in multiple projects, e.g. model generation, repository generation and service generation all include a core EntityGeneration.tt file.
Unfortunately, when TextTemplating resolves my nested includes, it builds up a long #line pre-processor directive in its generated .cs file, combining all of the relative paths to the lowest level included file.
Unfortunately, as this path is built up with relative paths, it ends up needlessly long, so long in fact that it exceeds the maximum path length (Windows 7).
Here is the line at fault from the generated code in case you are interested:
#line 3 "C:\VS2010\AlbatrossTravelGroup\ASC\AlbatrossTravelGroup.ASC.BusinessRules\Services\Contracts\..\..\..\..\AlbatrossTravelGroup.BusinessRules\Services\Contracts\..\..\..\AlbatrossTravelGroup.Models\Repositories\Contracts\..\..\../AlbatrossTravelGroup.Common/CodeGeneration.tt"
My question is this, how can I disable these directives being written to the generated code file? Failing that, how can I avoid this problem without changing my file structure?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Visual Studio 2012 添加了
linePragmas="false"
模板指令:http://msdn.microsoft.com/en-us/library/gg586945(v=vs.110).aspx
仍然不确定如何执行此操作VS2010,我在工作中一直在使用它。
Visual Studio 2012 adds the
linePragmas="false"
template directive:http://msdn.microsoft.com/en-us/library/gg586945(v=vs.110).aspx
Still not sure how to do this in VS2010 which I'm stuck with at work.
我不确定这会解决你的问题。我对 T4 没有太多经验。也就是说,我正在使用它在我的项目中构建许多文件,但我没有遇到您遇到的问题。
就我而言,我有一堆单独的 .tt 文件,它们在我的项目中呈现单独的文件。
我这样做的方式如下:我有一个 SaveOutput 方法(我相信是从 stackoverflow 上的某个地方偷来的):
所以在我的主 .tt 文件中,我有 <#@ include... #>对于我的各个文件的各种 .tt 文件。例如:<#@ include file="DataObjectInterface.tt" #>
然后,在我的主 .tt 类中,我只需进行如下调用:
不确定这种分解模板的方法是否能满足您的需求,但它对我有用,而且我还没有遇到任何问题。
我应该澄清一下,我的主 .tt 文件实际上并没有从自身生成任何内容。它本身不是模板。它只是加载每个模板并从其他模板生成文件。
I'm not sure this will fix your problem. I don't have a great deal of experience with T4. That said, I'm using it build a number of files in my project and I have not run into the problem you are running into.
In my case, I have a bunch of individual .tt files that render individual files in my project.
The way I'm doing it is as follows: I have a SaveOutput method (stolen from somewhere on stackoverflow, I believe):
So in my main .tt file, I have <#@ includes... #> for my various .tt files for the individual files. For example: <#@ include file="DataObjectInterface.tt" #>
Then, in my main .tt class, I simply make a call like this:
Not sure if this method of breaking your templates up will work for your needs, but it worked for mine and I haven't run into any problems with it yet.
I should clarify that my main .tt file doesn't actually generate anything from itself. It is not itself a template. It simply loads each of the templates and generates the files from the the other templates.