Added new Changelog generator

This commit is contained in:
2025-02-18 21:08:16 +00:00
parent 1443ddda68
commit ac4862a6b3
15 changed files with 265 additions and 1 deletions
@@ -0,0 +1,18 @@
using System;
namespace OmegaLeo.HelperLib.Changelog.Attributes
{
[Changelog("v1.0.0", "Created ChangelogAttribute to be used to register changes in the code.")]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)]
public class ChangelogAttribute : Attribute
{
public string Version { get; }
public string Description { get; }
public ChangelogAttribute(string version, string description)
{
Version = version;
Description = description;
}
}
}
@@ -0,0 +1,7 @@
# Changelog
## Version v1.0.0
- **Class: ChangelogTool**: Created the class ChangelogTool to extract changelogs via attributes
- **Class: ChangelogAttribute**: Created ChangelogAttribute to be used to register changes in the code.
@@ -0,0 +1,14 @@
namespace OmegaLeo.HelperLib.Changelog.Models
{
public class Changelog
{
public string Target;
public string Description;
public Changelog(string target, string description)
{
Target = target;
Description = description;
}
}
}
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -0,0 +1,63 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using OmegaLeo.HelperLib.Changelog.Attributes;
namespace OmegaLeo.HelperLib.Changelog.Tools
{
[Changelog("v1.0.0", "Created the class ChangelogTool to extract changelogs via attributes")]
public class ChangelogTool
{
public static Dictionary<string, List<Models.Changelog>> ExtractChangelog(Assembly assembly)
{
var changelogByVersion = new Dictionary<string, List<Models.Changelog>>();
foreach (var type in assembly.GetTypes())
{
foreach (var attr in type.GetCustomAttributes<ChangelogAttribute>())
{
AddChangelogEntry(changelogByVersion, attr.Version, $"Class: {type.Name}", attr.Description);
}
foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static))
{
foreach (var attr in method.GetCustomAttributes<ChangelogAttribute>())
AddChangelogEntry(changelogByVersion, attr.Version, $"Method: {type.Name}.{method.Name}()", attr.Description);
}
}
changelogByVersion = new Dictionary<string, List<Models.Changelog>>(changelogByVersion.OrderByDescending(x => x.Key));
return changelogByVersion;
}
public static string GetMarkdown(Assembly assembly)
{
var changelog = ExtractChangelog(assembly);
var sb = new StringBuilder();
sb.AppendLine("# Changelog\n");
foreach (var version in changelog)
{
sb.AppendLine($"## Version {version.Key}\n");
foreach (var entry in version.Value)
{
sb.AppendLine($"- **{entry.Target}**: {entry.Description}");
}
sb.AppendLine();
}
return sb.ToString();
}
private static void AddChangelogEntry(Dictionary<string, List<Models.Changelog>> changelog, string version, string target, string description)
{
if (!changelog.ContainsKey(version))
changelog[version] = new List<Models.Changelog>();
changelog[version].Add(new Models.Changelog(target, description));
}
}
}