Prefer GitVersion values for assembly and publish versioning

Enhance versioning by prioritizing GitVersion output for assembly, file, and informational versions. Add fallback MSBuild target to run GitVersion CLI in Visual Studio builds. Update publish rename logic to use FullSemVer and improve version normalization for artifact naming.
This commit is contained in:
MaddoScientisto 2026-02-15 00:03:16 +01:00
commit a00ab074c4

View file

@ -49,6 +49,17 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<!-- If GitVersion ran and produced values, prefer them for assembly/file/informational version
so the built assembly exposes FullSemVer for runtime and the publish rename uses the same value. -->
<PropertyGroup>
<!-- AssemblyVersion uses AssemblySemVer when available (three-part) -->
<AssemblyVersion Condition="'$(GitVersion_AssemblySemVer)' != ''">$(GitVersion_AssemblySemVer)</AssemblyVersion>
<!-- FileVersion uses GitVersion FileVersion when available -->
<FileVersion Condition="'$(GitVersion_FileVersion)' != ''">$(GitVersion_FileVersion)</FileVersion>
<!-- Prefer FullSemVer for informational version -->
<InformationalVersion Condition="'$(GitVersion_FullSemVer)' != ''">$(GitVersion_FullSemVer)</InformationalVersion>
</PropertyGroup>
<ItemGroup>
<Compile Update="Properties\Settings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput>
@ -63,19 +74,32 @@
</ItemGroup>
<PropertyGroup />
<!-- When building inside Visual Studio (which runs .NET Framework MSBuild), the GitVersion.MsBuild
task (net8.0) may fail. Run the GitVersion CLI as a fallback to produce obj/gitversion.json so
subsequent targets can consume the same values. Developers should install the global tool:
dotnet tool install __global GitVersion.Tool __version 6.5.1
-->
<Target Name="RunGitVersionCliForVisualStudio" BeforeTargets="GenerateAssemblyInfo" Condition="'$(BuildingInsideVisualStudio)' == 'true'">
<Message Text="Building inside Visual Studio: attempting to run GitVersion CLI to generate obj\gitversion.json (if `dotnet-gitversion` is installed)." Importance="High" />
<!-- Try the typical global tool command. ContinueOnError=true so VS build doesn't fail if CLI is not installed. -->
<Exec Command="dotnet-gitversion -output file -outputfile &quot;$(ProjectDir)obj\gitversion.json&quot;" ContinueOnError="true" />
<!-- Also try 'dotnet tool run' in case a local tool manifest is used. -->
<Exec Command="dotnet tool run GitVersion.Tool -- -output file -outputfile &quot;$(ProjectDir)obj\gitversion.json&quot;" ContinueOnError="true" />
</Target>
<!-- After publish, rename the produced executable to ImageCatalog.<year>.<major>.<minor>.<build>.<revision>.<ext>
The destination extension is taken from the actual produced file (exe or dll).
-->
<Target Name="RenamePublishedExeWithYearAndVersion" AfterTargets="Publish" Condition="'$(PublishDir)' != ''">
<PropertyGroup>
<_PublishYear>$([System.DateTime]::Now.ToString("yyyy"))</_PublishYear>
<!-- Prefer FileVersion (four-part) then AssemblyVersion -->
<_Ver>$(FileVersion)</_Ver>
<!-- Prefer GitVersion FullSemVer, then FileVersion, then AssemblyVersion -->
<_Ver Condition="'$(GitVersion_FullSemVer)' != ''">$(GitVersion_FullSemVer)</_Ver>
<_Ver Condition="'$(GitVersion_FullSemVer)' == ''">$(FileVersion)</_Ver>
<_Ver Condition="'$(_Ver)' == ''">$(AssemblyVersion)</_Ver>
<!-- Normalize to avoid plus signs and spaces -->
<!-- Normalize to avoid spaces but keep plus signs for pre-release build metadata; replace spaces with underscore -->
<_VerSanitized>$([System.String]::Copy('$(_Ver)'))</_VerSanitized>
<_VerSanitized>$([System.String]::Copy('$(_VerSanitized)').Replace('+', '.'))</_VerSanitized>
<_VerSanitized>$([System.String]::Copy('$(_VerSanitized)').Replace(' ', '_'))</_VerSanitized>
<_NewExeNameBase>ImageCatalog.$(_PublishYear).$(_VerSanitized)</_NewExeNameBase>