Check-in t4 class into source control?
Asked Answered
S

2

6

I want to use t4 to generate some text at runtime. I created a 'Runtime Text Template'. I noticed that there is a .cs file that is generated from the .tt file. I'm trying to figure out if that is normally checked into source control. Normally I don't check in generated file. This file has a #line directive with a full path to the file on my local machine. Checking that in just seems funny, since it obviously won't be the same on my teammate's machine.

Seaver answered 10/4, 2014 at 13:5 Comment(5)
I didn't understand your question to the very end, but if you are afraid of accidentally checking in the file, just exclude that file from your source control after it's generated. like this for TFS: #10698907Haight
Not worried about accidentally checking it in. I'm wondering if it is common when using t4 to check in the generated .cs file or if the .cs file should be regenreated on my teammate's computerSeaver
I personally use t4 for custom Entity Framework model code generation and I did check it in. Everyone uses it, though classes generated have no links to local environment. t4 is only a usefull tool in your hands to generate great deal of code that represents certain template (Entity Framework entity objects), you regenerate the code if you change the template (based on certain condition some entity objects will now be implementing ICustomInterface)Haight
you can have a link to environment variable instead of hardcoded value in each environment. than every single developer will configure that variable.Haight
or you can add pre-build action to your project to execute certain batch that retrieves that value from the system and creates a simple .cs file with constant string in it. the file should have a reference in project but excluded from tfs. reveal the meaning of your value and it will be easier for us to deliver the solution.Haight
D
6

Your concern about the filename in the .cs file is very valid but in this case it's part of a #line compiler directive and it simply tells the compiler what filename to print in it's output when processing this file.

Edit: Nice find from the OP - For VS 2012 you can add the following directive in your template to remove LinePragmas. Further SO discussion here.

<#@ template language="C#" linePragmas="false" #>

As a general rule I only like to have files checked in if they...

  1. Are needed by another developer or build agent to build the code
  2. Get deployed
  3. Serve as documentation or some similar purpose
Deathbed answered 10/4, 2014 at 13:11 Comment(4)
Yes, but isn't it weird that the generated file contains a full path to a file located on my local machine? That just smells funny to me.Seaver
Agreed. That line in the .cs file though is only telling the compiler what filename to put in the output. It's not really used for the template.Deathbed
Ah, there is an attribute called "linePragmas" that I can set to false. This will remove the #line compiler directive all together. From the docs "This attribute can also help if you’re finding the absolute filenames in pragmas are causing distracting merges under source code control."Seaver
Nice! I'll add it to my answer so others can find it helpful.Deathbed
D
4

Yes you will want to check it in to source code because it will most likely cause the build to break for other developers if you don't. Although they could just run the T4 template themselves it shouldn't be up to them to do so. Another related reason why is if you have some sort of build tool, like cruise control, it may not run your T4 template and wont be able to build the project anymore.

Dune answered 10/4, 2014 at 13:9 Comment(1)
We've just discovered on our project that this is true, but we hate it. Checking in generated files is morally bankrupt, and known to cause trouble later in life, one day you'll understand but by then it will be too late. Too late. (What VS needs is a "compile time template" concept, so every msbuild gets it up to date.) Oh well!Session

© 2022 - 2024 — McMap. All rights reserved.