Integrate GitVersion and add version provider abstraction

- Added GitVersion for semantic versioning and build metadata
- Introduced IVersionProvider and VersionProvider for UI-friendly version display
- MainForm now uses IVersionProvider for version label
- Registered VersionProvider in DI container
- Improved logging: filtered out AutoMapper license logs
- General code cleanup in Program.cs
This commit is contained in:
MaddoScientisto 2026-02-14 21:14:06 +01:00
commit 509d5357a8
8 changed files with 154 additions and 44 deletions

23
.gitversion.yml Normal file
View file

@ -0,0 +1,23 @@
mode: ContinuousDelivery
branches:
main:
tag: ''
increment: Patch
prevent-increment-of-merged-branch-version: true
track-merge-target: false
develop:
tag: alpha
increment: Minor
prevent-increment-of-merged-branch-version: false
feature:
tag: beta
increment: Patch
hotfix:
tag: hotfix
increment: Patch
release:
tag: rc
increment: Patch
ignore:
sha: []
commit-message-incrementing: Disabled

View file

@ -4,4 +4,4 @@ branches: {}
ignore: ignore:
sha: [] sha: []
merge-message-formats: {} merge-message-formats: {}
next-version: 3.0 next-version: "3.2"

View file

@ -0,0 +1,9 @@
namespace MaddoShared;
public interface IVersionProvider
{
/// <summary>
/// Returns a human-friendly version string for display (prefer AssemblyInformationalVersion).
/// </summary>
string GetVersionString();
}

View file

@ -0,0 +1,60 @@
using System.Diagnostics;
using System.Reflection;
using System.Collections.Generic;
namespace MaddoShared;
public sealed class VersionProvider : IVersionProvider
{
public string GetVersionString()
{
// Prefer the entry assembly; fall back to executing assembly
var asm = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
// 1) AssemblyInformationalVersion
var infoAttr = asm.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
if (!string.IsNullOrWhiteSpace(infoAttr?.InformationalVersion))
{
return infoAttr.InformationalVersion!;
}
// 2) File product version
try
{
var location = asm.Location;
if (!string.IsNullOrWhiteSpace(location))
{
var fvi = FileVersionInfo.GetVersionInfo(location);
if (!string.IsNullOrWhiteSpace(fvi.ProductVersion))
return fvi.ProductVersion!;
}
}
catch
{
// ignore and fall back to assembly version
}
// 3) AssemblyName.Version formatted (avoid -1 and trailing .0 parts)
var ver = asm.GetName().Version;
if (ver is not null)
{
// Build a list of parts but ignore negative values
var parts = new List<int> { ver.Major, ver.Minor };
if (ver.Build >= 0) parts.Add(ver.Build);
if (ver.Revision >= 0) parts.Add(ver.Revision);
// Trim trailing zeros but keep at least major.minor
for (int i = parts.Count - 1; i > 1; i--)
{
if (parts[i] == 0)
parts.RemoveAt(i);
else
break;
}
return string.Join('.', parts);
}
return "0.0.0";
}
}

View file

@ -0,0 +1,12 @@
This project uses GitVersion (via `GitVersion.MsBuild`) to compute semantic version numbers during build.
How it works:
- The `GitVersion.MsBuild` package runs during `dotnet build` and sets MSBuild properties such as `Version`, `AssemblyVersion`, `FileVersion`, and `InformationalVersion`.
- The repository is configured via `.gitversion.yml` at the repo root. Tags and branch names drive the generated semantic version.
Usage notes:
- Create annotated tags like `v1.2.3` or `1.2.3` to mark releases.
- In CI, ensure the Git clone includes tags and history (no shallow single-commit clones).
- The UI should read `AssemblyInformationalVersion` for a friendly version string; fall back to `FileVersion` or `AssemblyName.Version` if needed.
See https://gitversion.net for more configuration options.

View file

@ -43,6 +43,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.2" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.2" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="10.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.7" /> <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.7" />
<PackageReference Include="GitVersion.MsBuild" Version="5.11.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.4.421302"> <PackageReference Include="Microsoft.DotNet.UpgradeAssistant.Extensions.Default.Analyzers" Version="0.4.421302">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>

View file

@ -32,7 +32,7 @@ public partial class MainForm
private readonly PicSettings _picSettings; private readonly PicSettings _picSettings;
public MainForm(DataModel model, ImageCreationStuff imageCreationStuff, PicSettings picSettings, public MainForm(DataModel model, ImageCreationStuff imageCreationStuff, PicSettings picSettings,
ParametriSetup parametriSetup, ILogger<MainForm> logger) ParametriSetup parametriSetup, ILogger<MainForm> logger, IVersionProvider versionProvider)
{ {
Model = model; Model = model;
_parametriSetup = parametriSetup; _parametriSetup = parametriSetup;
@ -55,8 +55,8 @@ public partial class MainForm
btnOpenSourceFolder.Click += BtnOpenSourceFolder_Click; btnOpenSourceFolder.Click += BtnOpenSourceFolder_Click;
btnOpenDestFolder.Click += BtnOpenDestFolder_Click; btnOpenDestFolder.Click += BtnOpenDestFolder_Click;
var version = Assembly.GetExecutingAssembly().GetName().Version; var versionString = versionProvider?.GetVersionString() ?? "0.0.0";
_Label27.Text = $"Version: {version.Major}.{version.Minor}.{version.Build}.{version.Revision}"; _Label27.Text = $"Version: {versionString}";
} }
protected void BindControls() protected void BindControls()

View file

@ -15,7 +15,7 @@ static class Program
{ {
[DllImport("kernel32.dll", SetLastError = true)] [DllImport("kernel32.dll", SetLastError = true)]
private static extern bool AllocConsole(); private static extern bool AllocConsole();
[DllImport("kernel32.dll", SetLastError = true)] [DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle); static extern IntPtr GetStdHandle(int nStdHandle);
@ -53,7 +53,7 @@ static class Program
Console.SetOut(standardOutput); Console.SetOut(standardOutput);
Console.SetError(standardOutput); Console.SetError(standardOutput);
} }
public static IServiceProvider ServiceProvider { get; private set; } public static IServiceProvider ServiceProvider { get; private set; }
[STAThread] [STAThread]
static void Main() static void Main()
@ -64,7 +64,7 @@ static class Program
AllocConsole(); AllocConsole();
RedirectConsoleOutput(); RedirectConsoleOutput();
var serviceCollection = new ServiceCollection(); var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection); ConfigureServices(serviceCollection);
@ -97,10 +97,13 @@ static class Program
"ImageCatalog", "userprefs.xml"); "ImageCatalog", "userprefs.xml");
services.AddSingleton(new ParametriSetup(userPrefsPath)); services.AddSingleton(new ParametriSetup(userPrefsPath));
services.AddSingleton<PicSettings>(); services.AddSingleton<PicSettings>();
// Register your forms // Register your forms
services.AddTransient<MainForm>(); services.AddTransient<MainForm>();
// Version provider for UI and logging
services.AddSingleton<MaddoShared.IVersionProvider, MaddoShared.VersionProvider>();
services.AddLogging(configure => services.AddLogging(configure =>
{ {
configure.AddCustomFormatter(); configure.AddCustomFormatter();
@ -110,40 +113,42 @@ static class Program
} }
} }
public static class ConsoleLoggerExtensions public static class ConsoleLoggerExtensions
{ {
public static ILoggingBuilder AddCustomFormatter( public static ILoggingBuilder AddCustomFormatter(
this ILoggingBuilder builder) => this ILoggingBuilder builder) =>
builder.AddConsole(options => options.FormatterName = nameof(CustomLoggingFormatter)) builder
.AddConsoleFormatter<CustomLoggingFormatter, ConsoleFormatterOptions>(); .AddConsole(options => options.FormatterName = nameof(CustomLoggingFormatter))
} .AddConsoleFormatter<CustomLoggingFormatter, ConsoleFormatterOptions>()
public sealed class CustomLoggingFormatter : ConsoleFormatter, IDisposable .AddFilter("LuckyPennySoftware.AutoMapper.License", LogLevel.None);
{ }
private readonly IDisposable _optionsReloadToken; public sealed class CustomLoggingFormatter : ConsoleFormatter, IDisposable
private ConsoleFormatterOptions _formatterOptions; {
public CustomLoggingFormatter(IOptionsMonitor<ConsoleFormatterOptions> options) private readonly IDisposable _optionsReloadToken;
private ConsoleFormatterOptions _formatterOptions;
public CustomLoggingFormatter(IOptionsMonitor<ConsoleFormatterOptions> options)
// Case insensitive // Case insensitive
: base(nameof(CustomLoggingFormatter)) => : base(nameof(CustomLoggingFormatter)) =>
(_optionsReloadToken, _formatterOptions) = (_optionsReloadToken, _formatterOptions) =
(options.OnChange(ReloadLoggerOptions), options.CurrentValue); (options.OnChange(ReloadLoggerOptions), options.CurrentValue);
private void ReloadLoggerOptions(ConsoleFormatterOptions options) => private void ReloadLoggerOptions(ConsoleFormatterOptions options) =>
_formatterOptions = options; _formatterOptions = options;
public override void Write<TState>( public override void Write<TState>(
in LogEntry<TState> logEntry, in LogEntry<TState> logEntry,
IExternalScopeProvider scopeProvider, IExternalScopeProvider scopeProvider,
TextWriter textWriter) TextWriter textWriter)
{ {
string? message = string? message =
logEntry.Formatter?.Invoke( logEntry.Formatter?.Invoke(
logEntry.State, logEntry.Exception); logEntry.State, logEntry.Exception);
if (message is null) if (message is null)
{ {
return; return;
} }
textWriter.WriteLine($"{message}"); textWriter.WriteLine($"{message}");
} }
public void Dispose() => _optionsReloadToken?.Dispose(); public void Dispose() => _optionsReloadToken?.Dispose();
} }