How to add a dependency to a arbitrary file to a T4 template?

十年热恋 提交于 2019-12-18 15:53:22

问题


I have a T4 template that generates classes from an xml file.

How can I add a dependency between the xml file and the template file so that when the xml file is modified the template is rerun automatically without choosing "Run custom tool" from the context menu?


回答1:


I don't believe T4 supports automatic template transformation based on an external dependency. I agree with Marc - if you only have one external file, you could create a custom "custom tool" for your XML file or simply use ttxgen. However, I don't think this approach scales up to a scenario where t4 template depends on more than one file. You may need to create a Visual Studio package to handle that.




回答2:


How long does the tool take to execute? One lazy option might be to simply edit the csproj such that it always runs the tool during build (presumably via <Exec ... /> or a custom targets file) - of course, this depends on it being quick to execute.

Another way would be to write a shim that works as the "Custom Tool" in VS, and simply calls the existing exe (or whatever) with the right args. Not trivial, but doable (see here) - I believe this then supposedly plays fairly nicely with change detection. It is actually on my list of things to do for a current project, so I'll find out soon enough...




回答3:


You can use AutoTT Visual Studio Extension.

This extension allows to configure triggers that will run a T4 template.

One of the possible triggers is a file change. In the sample configuration file in AutoTT page, the regular expression for the triggers matches all files in the specified folders (Controllers, Content), but you can change it so that it runs with a specific file only.

Chirpy is another option for doing this. And also T4 Regenerator, which does it in a different way.




回答4:


Have you tried using <#@ xsd?



来源:https://stackoverflow.com/questions/280748/how-to-add-a-dependency-to-a-arbitrary-file-to-a-t4-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!