From a02426ddf1e6559878a7a53270d8eb4fa4488300 Mon Sep 17 00:00:00 2001 From: almostchristian Date: Wed, 6 Sep 2023 22:41:26 +0800 Subject: [PATCH 1/2] Targets file no longer required for generator package. Updated versioning of generator dlls. --- .../TestWebApiGenerator/TestWebApiGenerator.csproj | 5 ++--- ...nProcessor.DependencyInjection.Generator.csproj | 2 -- ...Processor.DependencyInjection.Generator.targets | 8 -------- .../Emitter.cs | 2 +- .../Parser.cs | 14 +++++++++++--- src/Directory.Build.props | 10 +++++++++- 6 files changed, 23 insertions(+), 18 deletions(-) delete mode 100644 src/ConfigurationProcessor.DependencyInjection.Generator/ConfigurationProcessor.DependencyInjection.Generator.targets diff --git a/sample/TestWebApiGenerator/TestWebApiGenerator.csproj b/sample/TestWebApiGenerator/TestWebApiGenerator.csproj index 0628a3a..d6a2ec0 100644 --- a/sample/TestWebApiGenerator/TestWebApiGenerator.csproj +++ b/sample/TestWebApiGenerator/TestWebApiGenerator.csproj @@ -1,12 +1,11 @@  - net7.0 enable enable - + true + $(BaseIntermediateOutputPath)\GeneratedFiles diff --git a/src/ConfigurationProcessor.DependencyInjection.Generator/ConfigurationProcessor.DependencyInjection.Generator.csproj b/src/ConfigurationProcessor.DependencyInjection.Generator/ConfigurationProcessor.DependencyInjection.Generator.csproj index f5f75da..05520d1 100644 --- a/src/ConfigurationProcessor.DependencyInjection.Generator/ConfigurationProcessor.DependencyInjection.Generator.csproj +++ b/src/ConfigurationProcessor.DependencyInjection.Generator/ConfigurationProcessor.DependencyInjection.Generator.csproj @@ -5,7 +5,6 @@ 11 true enable - 0.2.0-beta.1 $(PackageTags);source generation Generator This packages uses source generation to generate dependency injection registration methods based on the `appsettings.config` configuration. This is still in beta and the current version partially supports the configuration mechanisms available in the ConfigurationProcessor.DependencyInjection package. @@ -22,7 +21,6 @@ - diff --git a/src/ConfigurationProcessor.DependencyInjection.Generator/ConfigurationProcessor.DependencyInjection.Generator.targets b/src/ConfigurationProcessor.DependencyInjection.Generator/ConfigurationProcessor.DependencyInjection.Generator.targets deleted file mode 100644 index 372adfe..0000000 --- a/src/ConfigurationProcessor.DependencyInjection.Generator/ConfigurationProcessor.DependencyInjection.Generator.targets +++ /dev/null @@ -1,8 +0,0 @@ - - - - - PreserveNewest - - - \ No newline at end of file diff --git a/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Emitter.cs b/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Emitter.cs index 6bc1b40..1f598d9 100644 --- a/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Emitter.cs +++ b/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Emitter.cs @@ -7,7 +7,7 @@ namespace ConfigurationProcessor.DependencyInjection.SourceGeneration; internal class Emitter { - public const string VersionString = "0.2.0"; + public static readonly string VersionString = typeof(Emitter).Assembly.GetCustomAttribute().InformationalVersion; public string Emit(IReadOnlyList generateConfigurationClasses, List references, CancellationToken cancellationToken) { diff --git a/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Parser.cs b/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Parser.cs index 94bf5ab..f0de56b 100644 --- a/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Parser.cs +++ b/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Parser.cs @@ -172,15 +172,23 @@ internal IReadOnlyList GetServiceRegistrationClasses(I var configFile = configurationFile ?? DefaultConfigurationFile; IDictionary configurationValues; - var jsonFile = context.AdditionalFiles.FirstOrDefault(x => Path.GetFileName(x.Path) == configFile); - if (jsonFile == null) + var jsonFilePath = context.AdditionalFiles.FirstOrDefault(x => Path.GetFileName(x.Path) == configFile)?.Path; + + if (jsonFilePath == null && + context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.projectdir", out var projectDir) && + File.Exists(Path.Combine(projectDir, configFile))) + { + jsonFilePath = Path.Combine(projectDir, configFile); + } + + if (jsonFilePath == null) { Diag(DiagnosticDescriptors.ConfigurationFileNotFound, method.GetLocation(), configFile); continue; } else { - configurationValues = JsonConfigurationFileParser.Parse(File.OpenRead(jsonFile.Path)); + configurationValues = JsonConfigurationFileParser.Parse(File.OpenRead(jsonFilePath)); } var methodSignature = string.Join(", ", configurationMethodSymbol.Parameters.Select(ToDisplay)); diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 2701936..62b5fa2 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -6,9 +6,17 @@ 1.12.0 + $(Version) + + + + 0.2.1 + $(Version)-beta.1 + + + MIT $(Version).$([System.DateTime]::Now.ToString(yy))$([System.DateTime]::Now.DayOfYear.ToString(000)) - $(Version) $(FileVersion)-$(GIT_VERSION) almostchristian Apache-2.0 From 1b79776a161906a4652682c6c0a45e83b6abb56b Mon Sep 17 00:00:00 2001 From: almostchristian Date: Thu, 7 Sep 2023 11:12:10 +0800 Subject: [PATCH 2/2] Clean up warnings --- .../Parser.cs | 20 +++++++---- .../Utility/DiagnosticDescriptorHelper.cs | 2 +- .../Utility/DiagnosticDescriptors.cs | 34 ++++++++++++++----- src/Directory.Build.props | 2 +- 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Parser.cs b/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Parser.cs index f0de56b..70a9228 100644 --- a/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Parser.cs +++ b/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Parser.cs @@ -288,7 +288,7 @@ static string ToDisplay(IParameterSymbol parameter) if (foundServiceCollection && matchesServiceCollection) { keepMethod = false; - Diag(DiagnosticDescriptors.InvalidGenerateConfigurationMethodName, paramSymbol.Locations[0]); + Diag(DiagnosticDescriptors.MultipleServiceCollectionParameter, paramSymbol.Locations[0]); break; } else if (matchesServiceCollection) @@ -300,7 +300,7 @@ static string ToDisplay(IParameterSymbol parameter) if (foundConfiguration && matchesConfiguration) { keepMethod = false; - Diag(DiagnosticDescriptors.InvalidGenerateConfigurationMethodName, paramSymbol.Locations[0]); + Diag(DiagnosticDescriptors.MultipleConfigurationParameter, paramSymbol.Locations[0]); break; } else if (matchesConfiguration) @@ -345,7 +345,7 @@ static string ToDisplay(IParameterSymbol parameter) if (foundServiceCollection && matchesServiceCollection) { keepMethod = false; - Diag(DiagnosticDescriptors.InvalidGenerateConfigurationMethodName, paramSymbol.Locations[0]); + Diag(DiagnosticDescriptors.MultipleServiceCollectionParameter, paramSymbol.Locations[0]); break; } else if (matchesServiceCollection) @@ -357,7 +357,7 @@ static string ToDisplay(IParameterSymbol parameter) if (foundConfiguration && matchesConfiguration) { keepMethod = false; - Diag(DiagnosticDescriptors.InvalidGenerateConfigurationMethodName, paramSymbol.Locations[0]); + Diag(DiagnosticDescriptors.MultipleConfigurationParameter, paramSymbol.Locations[0]); break; } else if (matchesConfiguration) @@ -390,12 +390,12 @@ static string ToDisplay(IParameterSymbol parameter) if (multipleServiceCollectionFields) { - Diag(DiagnosticDescriptors.MultipleServiceCollectionFields, method.GetLocation(), classDec.Identifier.Text); + Diag(DiagnosticDescriptors.MultipleServiceCollectionParameter, method.GetLocation(), classDec.Identifier.Text); keepMethod = false; } else if (serviceCollectionField == null) { - Diag(DiagnosticDescriptors.MissingServiceCollectionField, method.GetLocation(), classDec.Identifier.Text); + Diag(DiagnosticDescriptors.MissingServiceCollectionParameter, method.GetLocation(), classDec.Identifier.Text); keepMethod = false; } else @@ -405,7 +405,7 @@ static string ToDisplay(IParameterSymbol parameter) } else if (!foundConfiguration) { - Diag(DiagnosticDescriptors.MissingConfigurationField, method.GetLocation(), classDec.Identifier.Text); + Diag(DiagnosticDescriptors.MissingConfigurationParameter, method.GetLocation(), classDec.Identifier.Text); keepMethod = false; } } @@ -444,6 +444,12 @@ potentialNamespaceParent is not NamespaceDeclarationSyntax && } } + if (string.IsNullOrEmpty(nspace)) + { + keepMethod = false; + Diag(DiagnosticDescriptors.TopLevelClassNotSupported, method.GetLocation()); + } + if (keepMethod) { lc ??= new ServiceRegistrationClass diff --git a/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Utility/DiagnosticDescriptorHelper.cs b/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Utility/DiagnosticDescriptorHelper.cs index ae4a041..44581d2 100644 --- a/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Utility/DiagnosticDescriptorHelper.cs +++ b/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Utility/DiagnosticDescriptorHelper.cs @@ -17,7 +17,7 @@ public static DiagnosticDescriptor Create( string? description = null, params string[] customTags) { - string helpLink = $"https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/{id.ToLowerInvariant()}.md"; + string helpLink = $"https://github.com/almostchristian/ConfigurationProcessor.DependencyInjection/diagnostics/{id.ToLowerInvariant()}.md"; return new DiagnosticDescriptor(id, title, messageFormat, category, defaultSeverity, isEnabledByDefault, description, helpLink, customTags); } diff --git a/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Utility/DiagnosticDescriptors.cs b/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Utility/DiagnosticDescriptors.cs index e17acd7..12ebdd3 100644 --- a/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Utility/DiagnosticDescriptors.cs +++ b/src/ConfigurationProcessor.DependencyInjection.SourceGeneration/Utility/DiagnosticDescriptors.cs @@ -78,26 +78,34 @@ internal static class DiagnosticDescriptors DiagnosticSeverity.Error, isEnabledByDefault: true); - public static DiagnosticDescriptor MissingConfigurationField { get; } = DiagnosticDescriptorHelper.Create( + public static DiagnosticDescriptor MissingConfigurationParameter { get; } = DiagnosticDescriptorHelper.Create( id: "CPGEN1018", - title: "Missing IConfiguration field", - messageFormat: "Missing IConfiguration field", + title: "Missing IConfiguration parameter/field", + messageFormat: "Missing IConfiguration parameter/field", category: Category, DiagnosticSeverity.Error, isEnabledByDefault: true); - public static DiagnosticDescriptor MissingServiceCollectionField { get; } = DiagnosticDescriptorHelper.Create( + public static DiagnosticDescriptor MissingServiceCollectionParameter { get; } = DiagnosticDescriptorHelper.Create( id: "CPGEN1019", - title: "Missing IServiceCollection field", - messageFormat: "Missing IServiceCollection field", + title: "Missing IServiceCollection parameter/field", + messageFormat: "Missing IServiceCollection paramter/field", category: Category, DiagnosticSeverity.Error, isEnabledByDefault: true); - public static DiagnosticDescriptor MultipleServiceCollectionFields { get; } = DiagnosticDescriptorHelper.Create( + public static DiagnosticDescriptor MultipleServiceCollectionParameter { get; } = DiagnosticDescriptorHelper.Create( id: "CPGEN1020", - title: "Multiple IServiceCollection fields", - messageFormat: "Multiple IServiceCollection fields", + title: "Multiple IServiceCollection parameters/fields", + messageFormat: "Multiple IServiceCollection parameters/fields", + category: Category, + DiagnosticSeverity.Error, + isEnabledByDefault: true); + + public static DiagnosticDescriptor MultipleConfigurationParameter { get; } = DiagnosticDescriptorHelper.Create( + id: "CPGEN1021", + title: "Multiple IConfiguration parameters/fields", + messageFormat: "Multiple IConfiguration parameters/fields", category: Category, DiagnosticSeverity.Error, isEnabledByDefault: true); @@ -117,4 +125,12 @@ internal static class DiagnosticDescriptors category: Category, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true); + + public static DiagnosticDescriptor TopLevelClassNotSupported { get; } = DiagnosticDescriptorHelper.Create( + id: "CPGEN1028", + title: "Top level class is not supported.", + messageFormat: "Top level class is not valid.", + category: Category, + defaultSeverity: DiagnosticSeverity.Error, + isEnabledByDefault: true); } \ No newline at end of file diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 62b5fa2..da9c9dd 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -11,7 +11,7 @@ 0.2.1 - $(Version)-beta.1 + $(Version)-beta.2