Skip to content

Commit

Permalink
Generate the string that contains the version of this library instead…
Browse files Browse the repository at this point in the history
… of using reflection.
  • Loading branch information
dlemstra committed Sep 7, 2024
1 parent e43b8a5 commit fa18ebc
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 13 deletions.
10 changes: 1 addition & 9 deletions src/Magick.NET/MagickNET.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using ImageMagick.Configuration;
Expand Down Expand Up @@ -153,14 +152,7 @@ public static string ImageMagickVersion
/// Gets the version of Magick.NET.
/// </summary>
public static string Version
{
get
{
var title = TypeHelper.GetCustomAttribute<AssemblyTitleAttribute>(typeof(MagickNET));
var version = TypeHelper.GetCustomAttribute<AssemblyFileVersionAttribute>(typeof(MagickNET));
return title.Title + " " + version.Version;
}
}
=> MagickVersion.Version;

/// <summary>
/// Gets the ImageMagick delegate libraries.
Expand Down
2 changes: 2 additions & 0 deletions src/Magick.NET/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Runtime.InteropServices;
using ImageMagick.SourceGenerator;

[assembly: MagickVersion]
[assembly: ComVisible(false)]
[assembly: CLSCompliant(false)]
4 changes: 0 additions & 4 deletions src/Shared/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ namespace ImageMagick;

internal static class TypeHelper
{
public static T GetCustomAttribute<T>(Type type)
where T : Attribute
=> (T)type.Assembly.GetCustomAttributes(typeof(T), false)[0];

public static Stream GetManifestResourceStream(Type type, string resourcePath, string resourceName)
=> type.Assembly.GetManifestResourceStream(resourcePath + "." + resourceName)!;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

#nullable enable

using System;

namespace ImageMagick.SourceGenerator;

[AttributeUsage(AttributeTargets.Assembly)]
internal sealed class MagickVersionAttribute : Attribute;
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

#nullable enable

using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;

namespace ImageMagick.SourceGenerator;

[Generator]
internal class MagickVersionGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.RegisterPostInitializationOutput(context => context.AddAttributeSource<MagickVersionAttribute>());
context.RegisterAttributeCodeGenerator<MagickVersionAttribute, string>(GetVersion, GenerateCode);
}

private string GetVersion(GeneratorAttributeSyntaxContext context)
{
var assemblyAttributes = context.SemanticModel.Compilation.Assembly.GetAttributes();

var title = assemblyAttributes
.Single(attribute => attribute.AttributeClass?.ToDisplayString() == typeof(AssemblyTitleAttribute).FullName)
.ConstructorArguments.FirstOrDefault().Value?.ToString();

var version = assemblyAttributes
.Single(attribute => attribute.AttributeClass?.ToDisplayString() == typeof(AssemblyVersionAttribute).FullName)
.ConstructorArguments.FirstOrDefault().Value?.ToString();

if (title is null || version is null)
return "\"";

return $"{title} {version}";
}

private void GenerateCode(SourceProductionContext context, string version)
{
var codeBuilder = new CodeBuilder();
codeBuilder.AppendLine("#nullable enable");
codeBuilder.AppendLine();
codeBuilder.AppendLine("namespace ImageMagick;");
codeBuilder.AppendLine();

codeBuilder.AppendLine("internal static class MagickVersion");
codeBuilder.AppendOpenBrace();
codeBuilder.Append(@"public const string Version = """);
codeBuilder.Append(version);
codeBuilder.AppendLine(@""";");
codeBuilder.AppendCloseBrace();

context.AddSource("MagickVersion.g.cs", SourceText.From(codeBuilder.ToString(), Encoding.UTF8));
}
}

0 comments on commit fa18ebc

Please sign in to comment.