From 08b96711d492b2eab654393e47943227f5e426c1 Mon Sep 17 00:00:00 2001 From: Paul Welter Date: Fri, 6 Sep 2024 19:31:16 -0500 Subject: [PATCH] update to Roslyn 4.4.0 --- .github/dependabot.yml | 2 ++ AssemblyMetadata.Generators.sln | 3 ++- .../AssemblyMetadata.Generators.csproj | 16 ++++++----- .../AssemblyMetadata.Generators.targets | 5 ++++ .../AssemblyMetadataGenerator.cs | 22 ++++++++++++--- .../AssemblyMetadataWriter.cs | 27 ++++++++++++++++++- .../AssemblyMetadata.Generators.Tests.csproj | 4 +-- ...semblyMetadataGeneratorsTests.verified.txt | 2 ++ ...Tests.GenerateMicrosoftCSharp.verified.txt | 2 ++ 9 files changed, 68 insertions(+), 15 deletions(-) create mode 100644 src/AssemblyMetadata.Generators/AssemblyMetadata.Generators.targets diff --git a/.github/dependabot.yml b/.github/dependabot.yml index fa8c583..242a072 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -15,6 +15,8 @@ updates: time: "02:00" timezone: "America/Chicago" open-pull-requests-limit: 10 + ignore: + - dependency-name: "Microsoft.CodeAnalysis.CSharp" groups: Azure: patterns: diff --git a/AssemblyMetadata.Generators.sln b/AssemblyMetadata.Generators.sln index 2ef001f..b354dea 100644 --- a/AssemblyMetadata.Generators.sln +++ b/AssemblyMetadata.Generators.sln @@ -1,6 +1,6 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 +# 17 VisualStudioVersion = 17.0.31903.59 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssemblyMetadata.Generators", "src\AssemblyMetadata.Generators\AssemblyMetadata.Generators.csproj", "{57C67863-03B3-4E43-8881-7B19A5657FE0}" @@ -8,6 +8,7 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{1BA030F4-6549-4F95-ACEA-80E992C519BD}" ProjectSection(SolutionItems) = preProject coverlet.runsettings = coverlet.runsettings + .github\dependabot.yml = .github\dependabot.yml src\Directory.Build.props = src\Directory.Build.props .github\workflows\dotnet.yml = .github\workflows\dotnet.yml README.md = README.md diff --git a/src/AssemblyMetadata.Generators/AssemblyMetadata.Generators.csproj b/src/AssemblyMetadata.Generators/AssemblyMetadata.Generators.csproj index fdde727..133d42c 100644 --- a/src/AssemblyMetadata.Generators/AssemblyMetadata.Generators.csproj +++ b/src/AssemblyMetadata.Generators/AssemblyMetadata.Generators.csproj @@ -2,10 +2,10 @@ netstandard2.0 - - true + + true true - + true false true @@ -13,11 +13,13 @@ - + + - + - + + - + diff --git a/src/AssemblyMetadata.Generators/AssemblyMetadata.Generators.targets b/src/AssemblyMetadata.Generators/AssemblyMetadata.Generators.targets new file mode 100644 index 0000000..c9ba8f7 --- /dev/null +++ b/src/AssemblyMetadata.Generators/AssemblyMetadata.Generators.targets @@ -0,0 +1,5 @@ + + + + + diff --git a/src/AssemblyMetadata.Generators/AssemblyMetadataGenerator.cs b/src/AssemblyMetadata.Generators/AssemblyMetadataGenerator.cs index 16deb4e..0450661 100644 --- a/src/AssemblyMetadata.Generators/AssemblyMetadataGenerator.cs +++ b/src/AssemblyMetadata.Generators/AssemblyMetadataGenerator.cs @@ -1,3 +1,4 @@ +using System.Collections.Immutable; using System.Reflection; using System.Resources; using System.Runtime.Versioning; @@ -36,6 +37,7 @@ public class AssemblyMetadataGenerator : IIncrementalGenerator public void Initialize(IncrementalGeneratorInitializationContext context) { + var provider = context.SyntaxProvider .ForAttributeWithMetadataName( fullyQualifiedMetadataName: "System.Reflection.AssemblyVersionAttribute", @@ -50,11 +52,23 @@ public void Initialize(IncrementalGeneratorInitializationContext context) context.RegisterSourceOutput(diagnostics, ReportDiagnostic); - IncrementalValuesProvider> constants = provider + var constants = provider .Select(static (item, _) => item.Constants) .Where(static item => item.Count > 0); - context.RegisterSourceOutput(constants, GenerateOutput); + var assemblyName = context.CompilationProvider + .Select(static (c, _) => c.AssemblyName); + + var thisNamespace = context.AnalyzerConfigOptionsProvider + .Select(static (c, _) => + { + c.GlobalOptions.TryGetValue("build_property.ThisAssemblyNamespace", out var methodName); + return methodName; + }); + + var options = assemblyName.Combine(thisNamespace); + + context.RegisterSourceOutput(constants.Combine(options), GenerateOutput); } private static bool SyntacticPredicate(SyntaxNode syntaxNode, CancellationToken cancellationToken) @@ -122,9 +136,9 @@ private static void ReportDiagnostic(SourceProductionContext context, EquatableA context.ReportDiagnostic(diagnostic); } - private void GenerateOutput(SourceProductionContext context, EquatableArray constants) + private void GenerateOutput(SourceProductionContext context, (EquatableArray constants, (string? assemblyName, string? thisNamespace) options) parameters) { - var source = AssemblyMetadataWriter.Generate(constants); + var source = AssemblyMetadataWriter.Generate(parameters.constants, parameters.options.assemblyName, parameters.options.thisNamespace); context.AddSource("AssemblyMetadata.g.cs", source); } diff --git a/src/AssemblyMetadata.Generators/AssemblyMetadataWriter.cs b/src/AssemblyMetadata.Generators/AssemblyMetadataWriter.cs index f394582..fd872cc 100644 --- a/src/AssemblyMetadata.Generators/AssemblyMetadataWriter.cs +++ b/src/AssemblyMetadata.Generators/AssemblyMetadataWriter.cs @@ -11,7 +11,7 @@ public static class AssemblyMetadataWriter return attribute?.InformationalVersion ?? "1.0.0.0"; }); - public static string Generate(EquatableArray constants) + public static string Generate(EquatableArray constants, string? assemblyName = null, string? thisNamespace = null) { if (constants == null) throw new ArgumentNullException(nameof(constants)); @@ -21,6 +21,15 @@ public static string Generate(EquatableArray constants) .AppendLine("// ") .AppendLine(); + if (!string.IsNullOrEmpty(thisNamespace)) + { + codeBuilder + .Append("namespace ") + .AppendLine(thisNamespace!) + .AppendLine("{") + .IncrementIndent(); + } + codeBuilder .AppendLine("/// ") .AppendLine("/// Assembly attributes exposed as public constants") @@ -41,6 +50,15 @@ public static string Generate(EquatableArray constants) .IncrementIndent() .AppendLine(); + if (!string.IsNullOrEmpty(assemblyName)) + { + codeBuilder + .Append("public const string AssemblyName = \"") + .Append(assemblyName) + .AppendLine("\";") + .AppendLine(); + } + foreach (var constant in constants) { var name = SafeName(constant.Name); @@ -58,6 +76,13 @@ public static string Generate(EquatableArray constants) .DecrementIndent() .AppendLine("}"); // class + if (!string.IsNullOrEmpty(thisNamespace)) + { + codeBuilder + .DecrementIndent() + .AppendLine("}"); // namespace + } + return codeBuilder.ToString(); } diff --git a/test/AssemblyMetadata.Generators.Tests/AssemblyMetadata.Generators.Tests.csproj b/test/AssemblyMetadata.Generators.Tests/AssemblyMetadata.Generators.Tests.csproj index a20346a..3ad866d 100644 --- a/test/AssemblyMetadata.Generators.Tests/AssemblyMetadata.Generators.Tests.csproj +++ b/test/AssemblyMetadata.Generators.Tests/AssemblyMetadata.Generators.Tests.csproj @@ -32,7 +32,7 @@ v - + runtime; build; native; contentfiles; analyzers; buildtransitive @@ -43,7 +43,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/test/AssemblyMetadata.Generators.Tests/Snapshots/GeneratorTests.GenerateAssemblyMetadataGeneratorsTests.verified.txt b/test/AssemblyMetadata.Generators.Tests/Snapshots/GeneratorTests.GenerateAssemblyMetadataGeneratorsTests.verified.txt index d05287e..ca0454b 100644 --- a/test/AssemblyMetadata.Generators.Tests/Snapshots/GeneratorTests.GenerateAssemblyMetadataGeneratorsTests.verified.txt +++ b/test/AssemblyMetadata.Generators.Tests/Snapshots/GeneratorTests.GenerateAssemblyMetadataGeneratorsTests.verified.txt @@ -8,6 +8,8 @@ internal static partial class ThisAssembly { + public const string AssemblyName = "Test.Generator"; + public const string HardKey = "HardValue"; public const string VerifyTargetFrameworks = ""; diff --git a/test/AssemblyMetadata.Generators.Tests/Snapshots/GeneratorTests.GenerateMicrosoftCSharp.verified.txt b/test/AssemblyMetadata.Generators.Tests/Snapshots/GeneratorTests.GenerateMicrosoftCSharp.verified.txt index f4be789..adc3022 100644 --- a/test/AssemblyMetadata.Generators.Tests/Snapshots/GeneratorTests.GenerateMicrosoftCSharp.verified.txt +++ b/test/AssemblyMetadata.Generators.Tests/Snapshots/GeneratorTests.GenerateMicrosoftCSharp.verified.txt @@ -8,6 +8,8 @@ internal static partial class ThisAssembly { + public const string AssemblyName = "Test.Generator"; + public const string TargetFramework = ".NETCoreApp,Version=v8.0"; public const string Serviceable = "True";