4

I'm building a NuGet package that delivers some T4 templates into the CodeTemplates directory. When I install the NuGet package, the T4 templates all 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?

Don Rolling
  • 2,301
  • 4
  • 30
  • 27

2 Answers2

4

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?

Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275
  • It sounds like you have some knowledge about the subject, but this doesn't really address my problem. I am definitely looking to keep the T4 templates as part of the project. That is the only way that I know of to override the default view and controller templates in MVC. Plus I have some other templates besides these that I want to live inside the project. – Don Rolling May 16 '11 at 14:03
  • I don't think there's a nice way to do what you want, short of automating VS to change the types after they're added. The AddMvc3ToWebForms package manupilates the project file to change a GUID - maybe you can do something similar to change the custom tool against each file you add? – Danny Tuppeny May 16 '11 at 14:20
  • Yeah, that's probably what I'll do. I was hoping that someone could help with that. – Don Rolling May 16 '11 at 15:24
2

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 = ""
Subgurim
  • 179
  • 3
  • 14
  • 1
    I haven't tried this because I haven't dealt with this issue for years now, but I'm glad somebody followed up with something that might work. This was a frustrating problem. – Don Rolling Jan 17 '17 at 22:17