diff --git a/src/ThisAssembly.Git/ThisAssembly.Git.csproj b/src/ThisAssembly.Git/ThisAssembly.Git.csproj index 387072ba..5b2319b1 100644 --- a/src/ThisAssembly.Git/ThisAssembly.Git.csproj +++ b/src/ThisAssembly.Git/ThisAssembly.Git.csproj @@ -5,6 +5,7 @@ latest true true + false diff --git a/src/ThisAssembly.Metadata/CSharp.sbntxt b/src/ThisAssembly.Metadata/CSharp.sbntxt deleted file mode 100644 index bfcdd8d8..00000000 --- a/src/ThisAssembly.Metadata/CSharp.sbntxt +++ /dev/null @@ -1,40 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -using System.CodeDom.Compiler; -using System.Runtime.CompilerServices; - -/// -/// Provides access to the current assembly information as pure constants, -/// without requiring reflection. -/// -partial class ThisAssembly -{ - /// - /// Gets the assembly metadata. - /// - [GeneratedCode("ThisAssembly.Metadata", "{{ Version }}")] - [CompilerGenerated] - public static partial class Metadata - { - {{~ for md in Metadata ~}} - /// {{ md.Key }} = {{ md.Value | string.split "\n" | array.first | string.replace "\r" "" | string.lstrip | string.rstrip }} - {{~ if RawStrings ~}} - public const string {{ md.Key }} = -""" -{{ md.Value }} -"""; - {{~ else ~}} - /// {{ prop.Key }} = {{ prop.Value }} - public const string {{ md.Key }} = @"{{ md.Value }}"; - {{~ end ~}} - - {{~ end ~}} - } -} \ No newline at end of file diff --git a/src/ThisAssembly.Metadata/MetadataGenerator.cs b/src/ThisAssembly.Metadata/MetadataGenerator.cs deleted file mode 100644 index 98b6612b..00000000 --- a/src/ThisAssembly.Metadata/MetadataGenerator.cs +++ /dev/null @@ -1,76 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Linq; -using System.Text; -using System.Threading; -using System.Xml.Linq; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.Text; -using Scriban; -using Scriban.Parsing; - -namespace ThisAssembly -{ - [Generator] - public class MetadataGenerator : IIncrementalGenerator - { - public void Initialize(IncrementalGeneratorInitializationContext context) - { - var metadata = context.SyntaxProvider - .CreateSyntaxProvider( - predicate: static (s, _) => s is AttributeSyntax, - transform: static (ctx, token) => GetSemanticTargetForGeneration(ctx, token)) - .Where(static m => m is not null) - .Select(static (m, _) => m!.Value) - .Collect(); - - context.RegisterSourceOutput( - metadata.Combine(context.ParseOptionsProvider), - GenerateSource); - } - - static KeyValuePair? GetSemanticTargetForGeneration(GeneratorSyntaxContext ctx, CancellationToken token) - { - var attributeNode = (AttributeSyntax)ctx.Node; - - if (attributeNode.ArgumentList?.Arguments.Count != 2) - return null; - - if (ctx.SemanticModel.GetSymbolInfo(attributeNode, token).Symbol is not IMethodSymbol ctor) - return null; - - var attributeType = ctor.ContainingType; - if (attributeType == null) - return null; - - if (attributeType.Name != nameof(System.Reflection.AssemblyMetadataAttribute)) - return null; - - var keyExpr = attributeNode.ArgumentList!.Arguments[0].Expression; - var key = ctx.SemanticModel.GetConstantValue(keyExpr, token).ToString(); - var valueExpr = attributeNode.ArgumentList!.Arguments[1].Expression; - var value = ctx.SemanticModel.GetConstantValue(valueExpr, token).ToString(); - return new KeyValuePair(key, value); - } - - void GenerateSource(SourceProductionContext spc, (ImmutableArray> attributes, ParseOptions parse) arg) - { - var (attributes, parse) = arg; - - var model = new Model(attributes.ToList()); - if (parse is CSharpParseOptions cs && (int)cs.LanguageVersion >= 1100) - model.RawStrings = true; - - var file = parse.Language.Replace("#", "Sharp") + ".sbntxt"; - var template = Template.Parse(EmbeddedResource.GetContent(file), file); - var output = template.Render(model, member => member.Name); - - spc.AddSource( - "ThisAssembly.Metadata.g.cs", - SourceText.From(output, Encoding.UTF8)); - } - } -} diff --git a/src/ThisAssembly.Metadata/Model.cs b/src/ThisAssembly.Metadata/Model.cs deleted file mode 100644 index 9ded172b..00000000 --- a/src/ThisAssembly.Metadata/Model.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Reflection; - -namespace ThisAssembly -{ - public class Model - { - public Model(IEnumerable> metadata) => Metadata = metadata.ToList(); - - public bool RawStrings { get; set; } = false; - public string Version => Assembly.GetExecutingAssembly().GetName().Version.ToString(3); - - public List> Metadata { get; } - } -} diff --git a/src/ThisAssembly.Metadata/ThisAssembly.Metadata.csproj b/src/ThisAssembly.Metadata/ThisAssembly.Metadata.csproj index 0b4e6fa7..af1617e0 100644 --- a/src/ThisAssembly.Metadata/ThisAssembly.Metadata.csproj +++ b/src/ThisAssembly.Metadata/ThisAssembly.Metadata.csproj @@ -4,6 +4,7 @@ netstandard2.0 latest true + false @@ -54,13 +55,10 @@ C#: + - - - - - - + + diff --git a/src/ThisAssembly.Metadata/ThisAssembly.Metadata.targets b/src/ThisAssembly.Metadata/ThisAssembly.Metadata.targets index c6c08bfa..66c7fc9f 100644 --- a/src/ThisAssembly.Metadata/ThisAssembly.Metadata.targets +++ b/src/ThisAssembly.Metadata/ThisAssembly.Metadata.targets @@ -6,5 +6,13 @@ + + + + + + \ No newline at end of file diff --git a/src/ThisAssembly.Metadata/Visual Basic.sbntxt b/src/ThisAssembly.Metadata/Visual Basic.sbntxt deleted file mode 100644 index 230a1df5..00000000 --- a/src/ThisAssembly.Metadata/Visual Basic.sbntxt +++ /dev/null @@ -1,22 +0,0 @@ -'''------------------------------------------------------------------------------ -''' -''' This code was generated by a tool. -''' -''' Changes to this file may cause incorrect behavior And will be lost if -''' the code Is regenerated. -''' -'''------------------------------------------------------------------------------ - -Namespace Global - ''' - ''' Provides access to the current assembly information as pure constants, - ''' without requiring reflection. - ''' - Partial Class ThisAssembly - Partial Class Metadata - {{~ for md in Metadata ~}} - Public Const {{ md.Key }} As String = @"{{ md.Value }}" - - End Class - End Class -End Namespace \ No newline at end of file diff --git a/src/ThisAssembly.Tests/Tests.cs b/src/ThisAssembly.Tests/Tests.cs index d9944274..fe2f6b7f 100644 --- a/src/ThisAssembly.Tests/Tests.cs +++ b/src/ThisAssembly.Tests/Tests.cs @@ -47,6 +47,10 @@ public void CanUseFileConstantLinkedFile() public void CanUseMetadata() => Assert.Equal("Bar", ThisAssembly.Metadata.Foo); + [Fact] + public void CanUseHierarchicalMetadata() + => Assert.Equal("Baz", ThisAssembly.Metadata.Root.Foo.Bar); + [Fact] public void CanUseProjectProperty() => Assert.Equal("Bar", ThisAssembly.Project.Foo); diff --git a/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj b/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj index f35c3183..b51b9cab 100644 --- a/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj +++ b/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj @@ -28,7 +28,7 @@ - + @@ -72,6 +72,7 @@ +