Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated source generation documentation. #13

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace TestWebApiGenerator;

internal static partial class ServiceRegistrationExtensions
{
[GenerateServiceRegistration("Services", ExcludedSections = new[] { "Hsts" }, ImplicitSuffixes = new[] { "Instrumentation", "Exporter" })]
[GenerateConfiguration("Services", ExcludedSections = new[] { "Hsts" }, ImplicitSuffixes = new[] { "Instrumentation", "Exporter" })]
public static partial void AddServicesFromConfiguration(this WebApplicationBuilder builder);

// [GenerateServiceRegistration("Services")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
/// Attribute for generating service registration.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class GenerateServiceRegistrationAttribute : Attribute
public class GenerateConfigurationAttribute : Attribute
{
/// <summary>
/// The default configuration file name.
/// </summary>
public const string DefaultConfigurationFile = "appsettings.json";

/// <summary>
/// Initializes a new instance of the <see cref="GenerateServiceRegistrationAttribute"/> class.
/// Initializes a new instance of the <see cref="GenerateConfigurationAttribute"/> class.
/// </summary>
public GenerateServiceRegistrationAttribute(string configurationSection)
public GenerateConfigurationAttribute(string configurationSection)
{
ConfigurationSection = configurationSection;
}
Expand Down
12 changes: 6 additions & 6 deletions src/ConfigurationProcessor.Generator/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ConfigurationProcessor.DependencyInjection.Generator
# ConfigurationProcessor.Generator

[![NuGet](https://img.shields.io/nuget/v/ConfigurationProcessor.DependencyInjection.Generator.svg?label=ConfigurationProcessor.DependencyInjection.Generator)](https://www.nuget.org/packages/ConfigurationProcessor.DependencyInjection.Generator)
[![NuGet](https://img.shields.io/nuget/v/ConfigurationProcessor.Generator.svg?label=ConfigurationProcessor.Generator)](https://www.nuget.org/packages/ConfigurationProcessor.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](https://www.nuget.org/packages/ConfigurationProcessor.DependencyInjection/) package.

Expand All @@ -11,18 +11,18 @@ This packages uses source generation to generate dependency injection registrati
```csharp
internal static partial class ServiceRegistrations
{
[GenerateServiceRegistration("Services")]
[GenerateConfiguration("Services")]
internal static partial void RegisterServices(this IServiceCollection services, IConfiguration configuration);

[GenerateServiceRegistration("Services")]
[GenerateConfiguration("Services")]
public static partial void AddServicesFromConfiguration(this WebApplicationBuilder builder);
}
```

Create a static partial method in a partial class decorated with `GenerateServiceRegistrationAttribute` and that contains an `IServiceCollection` and `IConfiguration` parameters or a single parameter with a parameter type that contains a single `IServiceCollection` and `IConfiguration` property.
Create a static partial method in a partial class decorated with `GenerateConfigurationAttribute` and that contains an `IServiceCollection` and `IConfiguration` parameters or a single parameter with a parameter type that contains a single `IServiceCollection` and `IConfiguration` property.


The `GenerateServiceRegistrationAttribute` requires a single argument , `configurationSection` which will be the configuration section in the `appsettings.json` to read from.
The `GenerateConfigurationAttribute` requires a single argument , `configurationSection` which will be the configuration section in the `appsettings.json` to read from.

### Sample configuration

Expand Down
2 changes: 1 addition & 1 deletion src/ConfigurationProcessor.SourceGeneration/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private static bool IsSyntaxTargetForGeneration(SyntaxNode node) =>
INamedTypeSymbol attributeContainingTypeSymbol = attributeSymbol.ContainingType;
string fullName = attributeContainingTypeSymbol.ToDisplayString();

if (fullName == Parser.GenerateServiceRegistrationAttribute)
if (fullName == Parser.GenerateConfigurationAttributeName)
{
return methodDeclarationSyntax.Parent as ClassDeclarationSyntax;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ConfigurationProcessor.SourceGeneration/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace ConfigurationProcessor.SourceGeneration;
internal class Parser
{
internal const string DefaultConfigurationFile = "appsettings.json";
internal const string GenerateServiceRegistrationAttribute = "ConfigurationProcessor.GenerateServiceRegistrationAttribute";
internal const string GenerateConfigurationAttributeName = "ConfigurationProcessor.GenerateConfigurationAttribute";
internal const string ServiceCollectionTypeName = "Microsoft.Extensions.DependencyInjection.IServiceCollection";
internal const string WebApplicationBuilderTypeName = "Microsoft.AspNetCore.Builder.WebApplicationBuilder";
private readonly GeneratorExecutionContext context;
Expand All @@ -28,7 +28,7 @@ public Parser(GeneratorExecutionContext context, Action<Diagnostic> reportDiagno

internal IReadOnlyList<ServiceRegistrationClass> GetServiceRegistrationClasses(IEnumerable<ClassDeclarationSyntax> classes)
{
INamedTypeSymbol? generateServiceRegistrationAttribute = context.Compilation.GetBestTypeByMetadataName(GenerateServiceRegistrationAttribute);
INamedTypeSymbol? generateServiceRegistrationAttribute = context.Compilation.GetBestTypeByMetadataName(GenerateConfigurationAttributeName);
if (generateServiceRegistrationAttribute == null)
{
// nothing to do if this type isn't available
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<PropertyGroup Condition="$(MSBuildProjectName.EndsWith('.Generator')) OR $(MSBuildProjectName.EndsWith('.SourceGeneration'))">
<Version>0.3.0</Version>
<Version>0.4.0</Version>
<PackageVersion>$(Version)-beta.1</PackageVersion>
</PropertyGroup>

Expand Down
Loading