开发者

NuGet - T4 File properties are different after installed

I'm building a NuGet package that delivers some T4 templates into the CodeTemplates directory. When I install the NuGet package, the T4 templates al开发者_C百科l have the Custom Tool property set to "TextTemplatingFileGenerator". This isn't right.

I know I can disable this by altering my registry so that new T4 templates aren't added this way, but since this is a NuGet package, that is not an option.

I've looked into PowerShell, but I'm having trouble understanding what I would do to achieve my goal.

I've looked at the .csproj file xml and found this:

<None Include="CodeTemplates\AddController\Controller.tt">
    <Generator>TextTemplatingFileGenerator</Generator>
    <LastGenOutput>Controller.cs</LastGenOutput>
</None>

If I remove "TextTemplatingFileGenerator" from this node, then the file will work as I desire.

Where should I go from here?


I'm not sure there's a nice way to do this. In a blog post, David Ebbo wrote:

One last thing I’ll mention about this model is that the .tt file is normally not part of your project. Instead, it lives somewhere else, and only its output becomes part of your project. Well, technically, the .tt file can be in your project for easy editing, but you then have to remove the ‘TextTemplatingFileGenerator’ custom tool, because you really don’t want it to execute on its own (it would surely fail with the custom host).

This makes it sound like this is Visual Studio behavior when a .tt file is added to the project.

That said, Scott Hanselman's AddMvc3ToWebForms makes some changes to a GUID in the csproj file to add MVC functionality (Add Controller / Add View, etc.), so it's possible you could do something similar to his code and remove the Generator section for files in your package and reload the project?


I had the same problem and I solved it using "install.ps1" which executes everytime the nuget package is installed or updated.

Your install.ps1 should look like:

param($installPath, $toolsPath, $package, $project)

$addControllerFolder = $project.ProjectItems.Item("CodeTemplates").ProjectItems.Item("AddController")

$addControllerFolder.ProjectItems.Item("Controller.tt").Properties.Item("CustomTool").Value = ""
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜