How to deploy T4 include files with NuGet packages

2020-07-17 15:32发布

问题:

I have some T4 include files which I want to reuse in several projects. So I have created a NuGet package and placed the files in the Tools folder of the package. Now they are installed in the packages\PackageName.x.x.x\Tools folder and I can add an include directive in my T4 files in the project.

But the disadvantage of this is, that there is a version number in the path which will change when I create new versions of the package. That would require me to update all include directives in the project.

Does anybody know a hint for a good approach for this?

回答1:

If these T4 templates are supposed to generate code inside libraries then you could put them into the content directory.

They will be added to the library having the reference to the nuget package. So rename tools to content (or add a new folder content) and you should be ok. You can also add subfolders to that folder. Or put .cs files in there.

See: http://docs.nuget.org/docs/reference/nuspec-reference



回答2:

The T4 include directive doesn't recognise msbuild properties, but the assembly one does. So you can use a base template class and reference an assembly from within your NuGet package.

With NuGet 2.5 (probably not available at the time of the original post) you can create an msbuild .props file. Assign $(MSBuildThisFileDirectory) combined the relative path to the assembly to a new msbuild property. The new property can then used in the T4 template of projects which are using the NuGet package.



标签: .net t4 nuget