0

I'm trying to use the approach described here to employ T4 Templates to automatically increment my build numbers in a .NET Core 3.1 project. Unfortunately it's not working. My generated assembly just has 1.0.0 as the version. I'm wondering if anyone can tell me what I'm doing wrong:

First I created my "Version.tt" file and wrote (copied) the T4 Template as described.

<#@ template language="C#" #>
// 
// This code was generated by a tool. Any changes made manually will be lost
// the next time this code is regenerated.
//

using System.Reflection;

[assembly: AssemblyVersion("2.0.0")]
[assembly: AssemblyFileVersion("2.0.0.<#= this.BuildNumber #>")]
[assembly: AssemblyInformationalVersion("2.0.0.<#= this.BuildNumber #>")]
<#+
    int BuildNumber    = (int)(DateTime.Now - new DateTime(2020,1,1)).TotalDays;
#>

Then I placed this file in the root folder and added it to my project as a link

enter image description here

But when I built and looked at the assembly properties from Windows Explorer, it just says it's "1.0.0". In VS the project, I took a look at the properties of the TT file and I see this:

enter image description here

I tried searching through the directories and I don't see any sort of output file for this. Is there some step I'm missing? Do I need to change one of these properties? I didn't see anything in "Build Action" that seemed to apply.

Joe
  • 5,394
  • 3
  • 23
  • 54

1 Answers1

1

I ran through your example. Apologies--in your case you want the Build Action to be None.

enter image description here

What worked for me is this template (showing some example import statements, should your template needs become more robust...):

<#@ template debug="true" hostspecific="true" language="C#" #>
<#@ output extension=".cs" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Text.RegularExpressions" #>
// Generated by a T4 template!
using System.Reflection;

[assembly: AssemblyVersion("2.0.0")]
[assembly: AssemblyFileVersion("2.0.0.<#= this.BuildNumber #>")]
[assembly: AssemblyInformationalVersion("2.0.0.<#= this.BuildNumber #>")]
<#+
    int BuildNumber    = (int)(DateTime.Now - new DateTime(2020,1,1)).TotalDays;
#>

Because you're trying to generate your own AssemblyInfo.cs file, I suggest placing this T4 template in the Properties folder of your project and deleting the existing AssemblyInfo.cs file that's there. As soon as you save the T4 template, it will get build and generate AssemblyInfo.cs immediately (if no errors occurred). I tested the above and it works.

Jazimov
  • 12,626
  • 9
  • 52
  • 59
  • Thanks for the answer. Unfortunately that does not appear to have any effect. The generated assembly still has "1.0.0" in its versions in the and I do not see any sort of generated file from this anywhere in the project or its subforlders. I was looking for some sort of file with "Version" somewhere in the name (I was expecting "Version.cs") but I don't see anything. – Joe May 16 '20 at 13:43
  • 1
    Sometimes it takes me a second round to understand what's being asked and what actually will work for a particular situation. I hope the above helps and thanks for the appreciation you expressed for my efforts. :) – Jazimov May 16 '20 at 16:30
  • I just came on here a few days later (got pulled off to another project, sorry for the delay) to thank you because it worked. But then I realized something: It only works if I add the file ***directly*** to the project. But it does not work if I add the file as a ***link***. I was hoping to be able to share this file among all my projects. But when I add it as a link, it does not get built at all. Is there a workaround? I'm OK if I must add a file directly but it would be nice to be able to share it – Joe May 20 '20 at 17:14
  • 1
    Figured out how to add as a link but it requires manually editing the csproj. Basically, add it directly first (not as a link) and save off (copy/paste) the `Compile` XML element that gets added to the csproj file. Now delete it from the project and add it back again, this time as a link. Finally, manually add in that `Compile` element back into the .csproj and you'll get the best of both worlds – Joe May 20 '20 at 17:59
  • Also required adding a `False` line to the .csproj as well to prevent duplicate attributes – Joe May 20 '20 at 18:01
  • All sounds good--congratulations and enjoy the power of your T4 template(s)! – Jazimov May 20 '20 at 22:14