Skip to content

Commit

Permalink
Source gen improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Jun 10, 2024
1 parent b17d910 commit db53665
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Shiny.Mediator.Maui/Shiny.Mediator.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.40" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.10" />
<ProjectReference Include="..\Shiny.Mediator\Shiny.Mediator.csproj" />
</ItemGroup>

Expand Down
12 changes: 10 additions & 2 deletions src/Shiny.Mediator.SourceGenerators/MediatorSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ public void Execute(GeneratorExecutionContext context)

var nameSpace = context.GetMSBuildProperty("RootNamespace") ?? context.Compilation.AssemblyName;
var assName = context.Compilation.AssemblyName?.Replace(".", "_");

var classes = this.syntaxReceiver
.Classes
.Select(x => x.ToDisplayString())
.Distinct()
.ToList();

if (!classes.Any())
return;

var sb = new StringBuilder();
sb
.AppendLine($"namespace {nameSpace};")
Expand All @@ -66,7 +74,7 @@ public void Execute(GeneratorExecutionContext context)
.AppendLine($"\tpublic static global::Microsoft.Extensions.DependencyInjection.IServiceCollection AddDiscoveredMediatorHandlersFrom{assName}(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)")
.AppendLine("\t{");

var classes = this.syntaxReceiver.Classes.Select(x => x.ToDisplayString()).Distinct();

foreach (var cls in classes)
sb.AppendLine($"\t\tservices.AddSingletonAsImplementedInterfaces<{cls}>();");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Microsoft.CodeAnalysis;
using SourceGeneratorsKit;

Expand All @@ -10,6 +11,9 @@ public class RegisterHandlerAttributeSyntaxReceiver : SyntaxReceiver

protected override bool ShouldCollectClassSymbol(INamedTypeSymbol classSymbol)

Check warning on line 12 in src/Shiny.Mediator.SourceGenerators/RegisterHandlerAttributeSyntaxReceiver.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'RegisterHandlerAttributeSyntaxReceiver.ShouldCollectClassSymbol(INamedTypeSymbol)'

Check warning on line 12 in src/Shiny.Mediator.SourceGenerators/RegisterHandlerAttributeSyntaxReceiver.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'RegisterHandlerAttributeSyntaxReceiver.ShouldCollectClassSymbol(INamedTypeSymbol)'
{
if (classSymbol.ContainingAssembly.Name.StartsWith("Shiny.Mediator", StringComparison.CurrentCultureIgnoreCase))
return false;

var hasAttribute = classSymbol.HasAttribute("RegisterHandlerAttribute");
if (hasAttribute)
return true;
Expand Down
1 change: 1 addition & 0 deletions tests/Shiny.Mediator.Tests/Shiny.Mediator.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<AssemblyName>UnitTests</AssemblyName>
<!--<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GF</CompilerGeneratedFilesOutputPath>-->
<!-- <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>-->
<!-- <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>-->
Expand Down
2 changes: 1 addition & 1 deletion tests/Shiny.Mediator.Tests/SourceGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void DidRegister()
{
var services = new ServiceCollection();
services.AddShinyMediator();
services.AddDiscoveredMediatorHandlersFromShiny_Mediator_Tests();
services.AddDiscoveredMediatorHandlersFromUnitTests(); // assembly name changed to get around assembly name (Shiny.Mediator) detection
var sp = services.BuildServiceProvider();

sp.GetService<IEventHandler<SourceGenEvent>>().Should().NotBeNull("Event Handler not found");
Expand Down

0 comments on commit db53665

Please sign in to comment.