Get Visual Studio to run a T4 Template on every build

后端 未结 22 2868
余生分开走
余生分开走 2020-11-22 08:29

How do I get a T4 template to generate its output on every build? As it is now, it only regenerates it when I make a change to the template.

I have found other ques

22条回答
  •  广开言路
    2020-11-22 09:07

    Thanks to GitHub.com/Mono/T4, at the moment you can do it for both .NET Core and Visual Studio builds by adding this to your .csproj file:

      
        
        
      
    
      
        
          
        
        
        
          
        
      
    

    If you transform your templates to different programming languages you should add something like and in order to get these files compiled even if you don't have generated files yet.

    Remove and Include trick only needed for first time generation, or you can make the XML-shorter like this:

      
        
        
      
    
      
        
      
    

    and just run build twice (for the first time). If you already have generated files committed to the repository there will be no problems on rebuilds with both examples.

    In the Visual Studio you might want to see something like this:

    instead of this:

    So add something like this to your project file:

      
        
          UInt16Class.tt
        
        
          UInt32Class.tt
        
        
          UInt64Class.tt
        
        
          UInt8Class.tt
        
      
    

    Complete example here: GitHub.com/Konard/T4GenericsExample (includes generation of multiple files from single template).

提交回复
热议问题