1

I'm having issues just creating the most basic of Assembly Information using T4 - can anyone give me a hand?

I simply want the code below to generate a version number, manually so I can see that it works but the compiled .cs file doesn't have the information it should.

Here's the code:

SharedAssemblyInfo.tt

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
<#
    int major    = 5;
    int minor    = 0;
    int build    = 0;
    int revisions = 0;

    // TODO: Write code here to automatically generate a version

    string version = String.Format("{0}.{1}.{2}.{3}",
                                   major,
                                   minor,
                                   build,
                                   revisions);
#>
// 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("<#= version #>")]
[assembly: AssemblyFileVersion("<#= version #>")]

Here's the expected result:

SharedAssemblyInfo.cs

using System.Reflection;

[assembly: AssemblyVersion("5.0.0.0")]
[assembly: AssemblyFileVersion("5.0.0.0")]

And here's the actual result - which does not have any of the assembly code above.

This is similar to a few questions I've reviewed like these: this, this and this but even following their specific use cases, I've not been able to produce a .cs file that has the AssemblyVersion and AssemblyFileVersion attributes I'm expecting

prestonsmith
  • 660
  • 1
  • 9
  • 29
  • Possible duplicate of [How to have an auto incrementing version number (Visual Studio)?](https://stackoverflow.com/questions/826777/how-to-have-an-auto-incrementing-version-number-visual-studio) – Kenneth K. Nov 06 '18 at 20:37
  • I've already reviewed that and a few other posts @Kenneth K. should I include the in the question ? – prestonsmith Nov 06 '18 at 20:52
  • 1
    The way this is usually done is using a utility that modifies an already-existing AssemblyInfo file. MSBuild has such a step (`AssemblyInfoTask`). My instincts tell me using T4 for this won't work as expected. If you use Git, `GitVersion` supports automatically updating the AssemblyInfo as well. –  Nov 06 '18 at 21:06
  • Man @Amy, I'd never even heard of Got version but that seems perfect! Can you put that as an answer so I can accept it ? I'll modify the question to deal more abstractedly with handling "versioning" – prestonsmith Nov 07 '18 at 01:25

1 Answers1

1

From the output, it looks like you have the .tt file configured as a runtime file generator rather than a design time generator.

Check the Custom Tool property for the file. It should be TextTemplateFileGenerator, not TextTemplatingFilePreprocessor:

enter image description here

duncanp
  • 1,339
  • 8
  • 8