0

I have a C# WPF project in Visual Studio 2019 that I'm trying to set the AssemblyVersion and AssemblyFileVersion of. The idea being that I am trying to set them to "0.Months since project started.Days since this month started.Minutes since midnight" to help with version control

I have a Text Template file with the code below

using System.Reflection;

[assembly: AssemblyVersion("<#= this.Release #>.<#= this.MonthsSinceProjectStarted #>.<#= this.DaysSinceMonthStarted #>.<#= this.MinutesSinceMidnight  #>")]
[assembly: AssemblyFileVersion("<#= this.Release #>.<#= this.MonthsSinceProjectStarted #>.<#= this.DaysSinceMonthStarted #>.<#= this.MinutesSinceMidnight  #>")]

<#+
int Release = 0;
 
static DateTime ProjectStartedDate = new DateTime(year: 2022, month: 5, day: 20);
 
int MonthsSinceProjectStarted = (int)((Int32.Parse(DateTime.Now.ToString("yyyy")) * 12) + Int32.Parse(DateTime.Now.ToString("MM"))) - ((ProjectStartedDate.Year * 12) ProjectStartedDate.Month);
int DaysSinceMonthStarted = (int)Int32.Parse(DateTime.Now.ToString("dd"));
int MinutesSinceMidnight = (int)DateTime.UtcNow.TimeOfDay.TotalMinutes;
#>

My .csproj file also looks like this

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <RootNamespace>LPG_Launcher</RootNamespace>
    <UseWPF>true</UseWPF>
    <Company>LowPoly Games</Company>
    <Authors>LowPoly Games</Authors>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
    <PlatformTarget>x64</PlatformTarget>
    <DebugType>none</DebugType>
    <DebugSymbols>false</DebugSymbols>
    <TransformOnBuild>true</TransformOnBuild>
    <OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <Optimize>true</Optimize>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
    <DebugType>full</DebugType>
    <DebugSymbols>true</DebugSymbols>
    <PlatformTarget>x64</PlatformTarget>
    <TransformOnBuild>true</TransformOnBuild>
    <OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DebugType>full</DebugType>
    <DebugSymbols>true</DebugSymbols>
    <TransformOnBuild>true</TransformOnBuild>
    <OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <DebugType>none</DebugType>
    <DebugSymbols>false</DebugSymbols>
    <PlatformTarget>x64</PlatformTarget>
    <TransformOnBuild>true</TransformOnBuild>
    <OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <Optimize>true</Optimize>
  </PropertyGroup>

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="if $(ConfigurationName) == Release (&#xD;&#xA;   del /S *.pdb&#xD;&#xA;)" />
  </Target>

  <PropertyGroup>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <TransformOnBuild>true</TransformOnBuild>
    <OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
  </PropertyGroup>
  
  <ItemGroup>
    <None Include="VersionAutoIncrementer.tt">
      <Generator>TextTemplatingFileGenerator</Generator>
      <LastGenOutput>VersionAutoIncrementer.txt</LastGenOutput>
    </None>
  </ItemGroup>

  <ItemGroup>
    <Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
  </ItemGroup>

  <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v16.0\TextTemplating\Microsoft.TextTemplating.targets" />

</Project>

When I clean and build my solution, it builds a .cs file with the below code in it


using System.Reflection;

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


But checking the properties of the "LPG Launcher.exe" thats built is showing the version numbers to be "0.0.0.0". Is this a suitable method of setting these values and if so, what am I doing incorrectly?

Istalri Skolir
  • 96
  • 1
  • 11
  • 1
    Is the file name correct? (AssemblyInfo.cs, usually in the Properties directory). Is the generate file included in the project? Is the generated file included in the build? Are you creating the file before the build (or accidentally after it)? – JHBonarius May 20 '22 at 11:29
  • Not actually sure, its possible its not included in the build but fairly certain its being built before the build looking at the output window. I was following the tutorial on the below link https://makolyte.com/auto-increment-build-numbers-in-visual-studio/ – Istalri Skolir May 20 '22 at 11:31
  • 2
    .NET 5 is already out of support. There's no `AssemblyInfo.cs` in .NET Core, it's generated by the project assembly properties. You can still add your own file with extra attributes but it's probably easier to just set those attributes – Panagiotis Kanavos May 20 '22 at 11:32
  • 2
    You can use format strings in the project properties. For example, `$([System.DateTime]::UtcNow.ToString(\`yyyyMMdd-HHmm\`))` will generate a suffix (build) version based on the current time – Panagiotis Kanavos May 20 '22 at 11:35
  • This is still a relatively young project so might be able to update it to a newer framework without too much hassle, if its something thats easier in the newer framework then I'll look at updating to it. Though @JHBonarius might be onto something with it potentially not including the built .cs file. I see it in the solution explorer but that doesn't guarantee its being used in the build – Istalri Skolir May 20 '22 at 11:36
  • Check [this topic](https://stackoverflow.com/questions/42138418) – JHBonarius May 20 '22 at 11:37
  • 2
    It's not a matter of easier. .NET 5 was a 1-year version. The long term support version is .NET 6. This was announced several years ago, when the versioning policy of .NET (Core) was announced. .NET 5 reached End-Of-Life two weeks ago. .NET 6 will be supported until November 2024 – Panagiotis Kanavos May 20 '22 at 11:37
  • After checking, it turns out that @JHBonarius was right, it wasn't including the file in the build and is now working perfectly. Though I did also test Panagiotis Kanavos suggestion which did also work as a alternative. Thanks for the help everyone – Istalri Skolir May 20 '22 at 11:40
  • 1
    You should really reconsider the versioning scheme. Minor versions matter too, so using the month as the minor version can lead to problems. If you do want to do this, `DateTime.Today.ToStrign("YYYYMM")` is better than trying to decipher what 5 means 2 months after deployment. Or was that 3 months? At least with `YYYYMM` you know the correct month immediatelly – Panagiotis Kanavos May 20 '22 at 11:43
  • Thanks for the advice, I'll reconsider my versioning scheme. I do have a days and minutes count but maybe the months was a poor choice in hindsight – Istalri Skolir May 20 '22 at 11:44
  • 1
    I used `Version` and `VersionSuffix` with `yyyyMMdd-HHmm` because I display this as a CLI tool version. I had to deploy fixes in the same day at least once. `-HHmm` is a problem for NuGet and maybe assemblies because it's considered a pre-release prefix – Panagiotis Kanavos May 20 '22 at 11:46
  • Part of a programmer's job is trying to find out why tools aren't doing what you want them to do, haha – JHBonarius May 20 '22 at 13:06
  • Aye, very true! – Istalri Skolir May 20 '22 at 13:35

1 Answers1

0

After checking from information given in the comments, I've determined that the .cs file being built by the Text Template wasn't being included in the build process. Thanks for the help everyone

Istalri Skolir
  • 96
  • 1
  • 11