-
-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate the string that contains the version of this library instead…
… of using reflection.
- Loading branch information
Showing
5 changed files
with
72 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
tools/Magick.NET.SourceGenerator/AssemblyInfo/MagickVersionAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
58 changes: 58 additions & 0 deletions
58
tools/Magick.NET.SourceGenerator/AssemblyInfo/MagickVersionGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |