Search code examples
c#visual-studiomsbuildcsproj

how to Modify a CS file before Build target runs when Publishing


I have a number of applications that I build and deploy where I like to have versioning information available in the code. This versioning information consists of stuff like the GitHash and a Date of deployment and the Branch deployed from and whether the git repo was "dirty" or not when publishing. This information is useful for debugging issues with deployments and helps me to know what version was deployed and when it was deployed.

Here's a sample of the auto generated version.cs file:

using System;

namespace Common
{
    public static class Version
    {
        public static readonly DateTime Date = DateTime.Parse("2024-09-30T16:29:16.368Z").ToUniversalTime();
        public static readonly string Guid = "c7b312cf-3cde-4141-97cd-97648216373a";
        public static readonly string GitHash = "78d90bd133450f2c412d772de3b493650afae754";
        public static readonly string Branch = "master";
        public static readonly bool GitIsDirty = false;
    }
}

Normally I'd auto create a version.json file (when publishing) with all of this information but it means that at runtime I have to read in this file for use, what I'd rather do is auto create a version.cs file (when publishing) and have that file compiled into the application.

So where I'm at right now is, I've edited my csproj file to have a special build target. That build target gets executed when a Build is happening AND a Publish is happening. The issue is that the new CS file that got generated (see file above) is not picked up by the compiler. Rather... the file that existed BEFORE the publish got compiled into the application.

<Target Name="GenerateVersionFile">

    <!-- run script to generate version json file -->
    <Exec Command="npm run generateVersionFile" />

</Target>

<Target Name="CheckGenerateVersionFile" BeforeTargets="Build">

    <!-- ONLY IF PUBLISHING: Generate new version file -->
    <CallTarget Targets="GenerateVersionFile" Condition="'$(PublishProtocol)' != ''" />

</Target>

Note: I only want to generate a new version file when both Building AND Publishing. I don't want to create a new version file every time I Build.

So my question is: is there a way for me to modify a CS file when performing a Publish?


Solution

  • If you want to ensure that a file generated before the publish process is compiled during publishing in MSBuild, you can check the following steps:

    1. Add the generated file to the Compile item group.
    <Target Name="GenerateVersionFile">
    
        <!-- run script to generate version json file -->
        <Exec Command=""npm run generateVersionFile" />
        <ItemGroup>
            <Compile Include="your real path\version.cs" />
        </ItemGroup>
    </Target>
    
    1. Generate new version file before the publish via BeforeTargets.
    <Target Name="CheckGenerateVersionFile" BeforeTargets="publish">
        <CallTarget Targets="GenerateVersionFile"  />
    </Target>