Automatizzare il cambio versione
12-01-2024
Creare una versione in formato [anno.mese.giorno.progressivo]
CSHARP
12-01-2024
Creare una versione in formato [anno.mese.giorno.progressivo]
using System;
using System.Text;
using System.Reflection;
namespace ControlloVersione
{
class Program
{
static void Main(string[] args)
{
string assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
Console.WriteLine($"assemblyVersion: {assemblyVersion}");
Console.WriteLine("Premi un tasto per continuare...");
Console.ReadLine();
}
}
}
Per modificare il file da Visual Studio:
Da inserire prima del tag "</Project>" nel file "nome_progetto.csproj"
<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>
Si puĆ² lasciare <Deterministic>true</Deterministic> sempre in .csproj nel <PropertyGroup> In AssemblyInfo.cs lasciare gli assembly decommentati
[assembly: AssemblyVersion("2024.1.12.17")]
[assembly: AssemblyFileVersion("1.0.0.0")]