Skip to content

Commit

Permalink
Merge pull request #11 from almostchristian/develop
Browse files Browse the repository at this point in the history
Allow for excluding subsections
  • Loading branch information
almostchristian authored Sep 15, 2023
2 parents d43c28d + cc0ed9f commit 44fecea
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
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")]
[GenerateServiceRegistration("Services", ExcludedSections = new[] { "Hsts" })]
public static partial void AddServicesFromConfiguration(this WebApplicationBuilder builder);

// [GenerateServiceRegistration("Services")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public GenerateServiceRegistrationAttribute(string configurationSection)
/// </summary>
public string ConfigurationFile { get; set; } = DefaultConfigurationFile;

/// <summary>
/// Sections to exclude.
/// </summary>
public string[] ExcludedSections { get; set; } = Array.Empty<string>();

/// <summary>
/// Gets the configuration section.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Immutable;
using System.Data;
using System.Diagnostics;
using ConfigurationProcessor.DependencyInjection.SourceGeneration.Parsing;
using ConfigurationProcessor.DependencyInjection.SourceGeneration.Utility;
Expand Down Expand Up @@ -83,7 +84,7 @@ internal IReadOnlyList<ServiceRegistrationClass> GetServiceRegistrationClasses(I
IMethodSymbol? configurationMethodSymbol = sm.GetDeclaredSymbol(method, cancellationToken)!;
Debug.Assert(configurationMethodSymbol != null, "configuration method is present.");
(string configurationSection, string? configurationFile) = (string.Empty, null);

string[] excluded = Array.Empty<string>();
foreach (AttributeListSyntax mal in method.AttributeLists)
{
foreach (AttributeSyntax ma in mal.Attributes)
Expand Down Expand Up @@ -158,6 +159,10 @@ internal IReadOnlyList<ServiceRegistrationClass> GetServiceRegistrationClasses(I
case "ConfigurationFile":
configurationFile = (string?)GetItem(value);
break;
case "ExcludedSections":
var values = (ImmutableArray<TypedConstant>)GetItem(value)!;
excluded = values.Select(x => $"{configurationSection}:{x.Value}").ToArray();
break;
}
}
}
Expand Down Expand Up @@ -189,6 +194,13 @@ internal IReadOnlyList<ServiceRegistrationClass> GetServiceRegistrationClasses(I
else
{
configurationValues = JsonConfigurationFileParser.Parse(File.OpenRead(jsonFilePath));

if (excluded.Length > 0)
{
configurationValues = configurationValues
.Where(x => !excluded.Any(z => x.Key.StartsWith(z)))
.ToDictionary(x => x.Key, x => x.Value);
}
}

var methodSignature = string.Join(", ", configurationMethodSymbol.Parameters.Select(ToDisplay));
Expand Down
4 changes: 2 additions & 2 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
</PropertyGroup>

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

<PropertyGroup>
Expand Down

0 comments on commit 44fecea

Please sign in to comment.