3

After dig and search. I find that AssemblyVersion only accept System.Int16 That mean it can't exceed 65,535 value.

the output *.DLL assembly version style I need (YYYY.M.D.HHMMSS) (2022.02.20.060920)

Actually the second part first zero will omit 060920 It will be 60920, What if hour is 15 = 3 PM, It should be 150920 = 6 digits. It will not work. I guess there's a way DateTime.Now.Ticks but it gave a System.Int64 and will never works for Version.

So please is there any 5 digit hour/minute/second style that maybe can used here? Or maybe special 5 digit Tick that have meaning.

The main problem that when I use Visual Studio auto-increment Revision 1.0.0.* feature. It gives arbitrary increment value. And the package output are not ordered.

DLLs

What should I do now? For now I need to use Time for revision field... Can you help on that please?

Edit, If 5 digit style not avaiable for above. I found article increment revision by one. But its very very old and use JavaScript! https://www.codeproject.com/Tips/161923/Incrementing-AssemblyVersion-revision-number-on-ea

There's also question not answered which still new I would like to increment the build revision version for every build in visual studio

I got following code which get Version 2022.02.20.Revision (Revision are useless during its restricted to large value midnight * 2 from MSDN.

Edit: During the only extension in VS marketplace that do that is not updated to Visual Studio 2022 or maybe they will not supported it, I try to give solution below:

(Feel free to modify Answer if you have better approach)

nickgreek
  • 181
  • 1
  • 7
  • How is the package output not ordered? The file names are in descending order, and the time column is (or appears to be) in descending order. – Lance U. Matthews Feb 20 '22 at 04:24
  • Yes its ordered, But its meaningless revision number. is there any 5 digit hour/minute/second style that maybe can used please. Or how to make it increment daily? For example Day 20 Revision start from 1,2,3,4, etc. Day 21 start from 1,2,3.. – nickgreek Feb 20 '22 at 04:30
  • 2
    There are 86,400 seconds in a day, so you could use those 5 digits to store the time as seconds-from-midnight. I don't think the resulting number would be any more opaque than however else you might encode a 6-digit number into 5 digits. Also, are you really expecting to create multiple builds in the same minute? Perhaps you could store it as `seconds / 10` or `seconds / 12`. Closely related: [How to use release date as assembly version for a .net project?](https://stackoverflow.com/q/35325127/150605), [Creating a revision number from date/time](https://stackoverflow.com/q/1527596/150605) – Lance U. Matthews Feb 20 '22 at 04:33

1 Answers1

8

I was wish they update extension in VS marketplace.

But anyway here's solution for generate a Version with (Year, Month, Day, Incremental Daily)

This code must inserted before close of </project> tag in *.csproj file

<!-- Change AssemblyInfo.cs AssemblyVersion with date/time and increment revision daily by one in Visual Studio 2022 -->
<Target Name="AssemblyVersion" BeforeTargets="CoreCompile" DependsOnTargets="PrepareForBuild">
    <PropertyGroup>
        <!-- Define Constants -->
        <AssemblyInfo>Properties\AssemblyInfo.cs</AssemblyInfo>
        <AssemblyInfoContent>$([System.IO.File]::ReadAllText($(AssemblyInfo)))</AssemblyInfoContent>
        <VersionRegex>(\[\s*assembly\s*:\s*AssemblyVersion\(\s*"(\d+)\.(\d+)\.(\d+)(\.)(\d+)("\)\s*\]))</VersionRegex>
        <BuildAndRevisionRegex>(\d+\.\d+")</BuildAndRevisionRegex>

        <!-- Parse Build and Revision from AssemblyInfo-->
        <AssemblyVersion>$([System.Text.RegularExpressions.Regex]::Match('$(AssemblyInfoContent)', '$(VersionRegex)'))</AssemblyVersion>
        <BuildAndRevision>$([System.Text.RegularExpressions.Regex]::Match('$(AssemblyVersion)', '$(BuildAndRevisionRegex)'))</BuildAndRevision>
        <BuildAndRevision>$(BuildAndRevision.Remove($(BuildAndRevision.LastIndexOf('"')), 1))</BuildAndRevision>
        
        <!-- Generate Build and Revision from AssemblyVersion -->
        <Build>$(BuildAndRevision.SubString(0, $(BuildAndRevision.LastIndexOf('.'))))</Build>
        <Revision>$(BuildAndRevision.SubString($([MSBuild]::Add($(BuildAndRevision.LastIndexOf('.')), 1))))</Revision>
        
        <!-- Increment Revision by one if Build equal Current Day otherwise start from one as new Day Build-->
        <Revision Condition ="$([System.DateTime]::Now.Day) == $(Build)">$([MSBuild]::Add($(Revision), 1))</Revision>
        <Revision Condition ="$([System.DateTime]::Now.Day) != $(Build)">1</Revision>

        <!-- New AssemblyVersion Block -->
        <AssemblyVersion>[assembly: AssemblyVersion("$([System.DateTime]::Now.ToString("yyyy.M.d.$(Revision)"))")]</AssemblyVersion>
    </PropertyGroup>

    <!-- Write New AssemblyVersion Block to AssemblyInfo.cs file -->
    <WriteLinesToFile File="$(AssemblyInfo)" Lines="$([System.Text.RegularExpressions.Regex]::Replace($(AssemblyInfoContent), $(VersionRegex), $(AssemblyVersion)))" Overwrite="true" />
</Target>

A generated result will be like that:

  1. Each day a library/project will start from (Year, Month, Day, Daily Day Incremental)

Incremental daily

  1. Next Day increment from one again: Next Day increment from one again
nickgreek
  • 181
  • 1
  • 7