mono / t4

T4 text templating engine
Other
395 stars 101 forks source link

How to manually add dependencies to a T4 template on a .proj file? #195

Open Pxtl opened 3 weeks ago

Pxtl commented 3 weeks ago

I'm trying to get T4 templates compiling on build, but they have xml-based dependencies. I see that the TemplateBuildState.TransformTemplate objects do keep a list of dependencies that get checked as part of the Last Modified Date calculation business, but I can't find any documented API to notify the build system that my template has an external dependency. Is there any way to do that?

Thanks.

mhutch commented 3 days ago

There isn't currently a way but we could definitely add one. Probably AdditionalDependencies item metadata with a semicolon-separated list of absolute or project-relative paths?

Pxtl commented 3 days ago

That would be good. I dug into the code of T4.Build but on this one, it had code to treat the #@Include directives as inputs to the target so it could be re-run if they changed when skipUpToDate was enabled -- I don't know if your project does same. If that was the case, a way to opt into this for non-include directives using a custom directive would be the gold-plated version of this.

Something like

<#@ IncludeAdditionalDependency Processor="T4.BuildTools.CustomDirectives"  Path="../../somefile.xml" AsStreamVariable="myXmlStream" #>

<#
var myXDoc = XDocument.Load(myXmlStream);
foreach(var xEl in myXDoc.Root.Elements())
{
#>
   do stuff with <#= xEl.Value #>
<#
{
#>

Then the user doesn't have to duplicate their reference to the file in both the .csproj and the .tt. But I think you still might have to do something with adding it as an UpToDateCheckInput or whatever in some circumstances, I'm not sure... I've been trying to learn about msbuild's xml language but it's strange and feels more difficult than it needs to be.

Of course, then you get into questions about wildcards or referencing a whole tree of directories or whatever instead of a single file. For me a single-file is all I need for my use-case though.

Either way, any way to flag a file as a non-T4 dependency for the T4 template for up-to-date checking would be great, I'm just gold-plating.