Skip to content

Commit

Permalink
Starting to test source gen
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Jun 1, 2024
1 parent 4ca2e91 commit 23ac3ba
Show file tree
Hide file tree
Showing 19 changed files with 84 additions and 23 deletions.
3 changes: 2 additions & 1 deletion Build.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"src\\Shiny.Mediator\\Shiny.Mediator.csproj",
"src\\Shiny.Mediator.Contracts\\Shiny.Mediator.Contracts.csproj",
"src\\Shiny.Mediator.Blazor\\Shiny.Mediator.Blazor.csproj",
"src\\Shiny.Mediator.Maui\\Shiny.Mediator.Maui.csproj"
"src\\Shiny.Mediator.Maui\\Shiny.Mediator.Maui.csproj",
"src\\Shiny.Mediator.SourceGenerators\\Shiny.Mediator.SourceGenerators.csproj"
]
}
}
2 changes: 1 addition & 1 deletion Sample/Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<ApplicationVersion>1</ApplicationVersion>

<ShinyVersion>3.3.3</ShinyVersion>

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<!--slower build, faster runtime in DEBUG-->
<!-- <_MauiForceXamlCForDebug Condition="'$(Configuration)' == 'Debug'">true</_MauiForceXamlCForDebug> -->
</PropertyGroup>
Expand Down
1 change: 1 addition & 0 deletions Sample/SingletonRequestHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Sample;


// [RegisterHandler]
public class SingletonRequestHandler(IMediator mediator, AppSqliteConnection data) : IRequestHandler<MyMessageRequest, MyMessageResponse>
{
public async Task<MyMessageResponse> Handle(MyMessageRequest request, CancellationToken cancellationToken)
Expand Down
1 change: 1 addition & 0 deletions ShinyMediator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution", "Solution", "{F3
src\Directory.build.props = src\Directory.build.props
src\Directory.build.targets = src\Directory.build.targets
src\nuget.txt = src\nuget.txt
Build.slnf = Build.slnf
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shiny.Mediator.SourceGenerators", "src\Shiny.Mediator.SourceGenerators\Shiny.Mediator.SourceGenerators.csproj", "{DC52EE6B-CE7D-4711-9BF9-A174E20B3D77}"
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ and dependency injection can't reach.

## Getting Started

Install [Shiny.Mediator](https://www.nuget.org/packages/Shiny.Mediator) from NuGet

First, let's create our request & event handlers

```
Expand Down
2 changes: 1 addition & 1 deletion src/Shiny.Mediator.Blazor/Shiny.Mediator.Blazor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components" Version="8.0.5"/>
<ProjectReference Include="..\Shiny.Mediator.Contracts\Shiny.Mediator.Contracts.csproj" />
<ProjectReference Include="..\Shiny.Mediator\Shiny.Mediator.csproj"/>
</ItemGroup>

</Project>
6 changes: 0 additions & 6 deletions src/Shiny.Mediator.Contracts/Shiny.Mediator.Contracts.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Shiny.Mediator</RootNamespace>
</PropertyGroup>

<!-- <ItemGroup>-->
<!-- <ProjectReference Include="..\Shiny.Mediator.SourceGenerators\Shiny.Mediator.SourceGenerators.csproj"/>-->
<!-- </ItemGroup>-->

</Project>
3 changes: 1 addition & 2 deletions src/Shiny.Mediator.Maui/Shiny.Mediator.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
</PropertyGroup>

<ItemGroup>
<!--<PackageReference Include="Microsoft.Maui.Core" Version="8.0.40" />-->
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.40" />
<ProjectReference Include="..\Shiny.Mediator.Contracts\Shiny.Mediator.Contracts.csproj"/>
<ProjectReference Include="..\Shiny.Mediator\Shiny.Mediator.csproj" />
</ItemGroup>

</Project>
11 changes: 6 additions & 5 deletions src/Shiny.Mediator.SourceGenerators/MediatorSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForPostInitialization(x => x.AddSource("RegisterHandlerAttribute.g.cs", SourceText.From(
"""
namespace Shiny.Mediator;
[System.AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public class RegisterHandlerAttribute : System.Attribute {}
namespace Shiny.Mediator
{
[System.AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public class RegisterHandlerAttribute : System.Attribute {}
}
"""
)));
context.RegisterForSyntaxNotifications(() => syntaxReceiver);
// context.RegisterForSyntaxNotifications(() => syntaxReceiver);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<DevelopmentDependency>true</DevelopmentDependency>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
<IsRoslynComponent>true</IsRoslynComponent>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
Expand All @@ -14,9 +14,4 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.9.2" PrivateAssets="All" />
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
</ItemGroup>

<ItemGroup>
<None Include="build\**" PackagePath="buildTransitive\" Pack="true" />
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>
</Project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions src/Shiny.Mediator/Shiny.Mediator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@

<ItemGroup>
<ProjectReference Include="..\Shiny.Mediator.Contracts\Shiny.Mediator.Contracts.csproj" />
<ProjectReference Include="..\Shiny.Mediator.SourceGenerators\Shiny.Mediator.SourceGenerators.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<None Include="..\Shiny.Mediator.SourceGenerators\bin\$(Configuration)\netstandard2.0\Shiny.Mediator.SourceGenerators.dll"
Pack="true"
PackagePath="analyzers/dotnet/cs"
Visible="false" />
</ItemGroup>
</Project>
5 changes: 5 additions & 0 deletions src/Shiny.Mediator/build/Package.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project>
<ItemGroup Condition=" '$(ImplicitUsings)' == 'true' OR '$(ImplicitUsings)' == 'enable' ">
<Using Include="Shiny.Mediator" />
</ItemGroup>
</Project>
13 changes: 12 additions & 1 deletion tests/Shiny.Mediator.Tests/Shiny.Mediator.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<!--<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GF</CompilerGeneratedFilesOutputPath>-->
<!-- <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>-->
<!-- <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>-->
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" Version="1.1.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.9.2" PrivateAssets="all" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
Expand All @@ -17,6 +25,9 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Shiny.Mediator\Shiny.Mediator.csproj" />
<ProjectReference Include="..\..\src\Shiny.Mediator\Shiny.Mediator.csproj" />
<ProjectReference Include="..\..\src\Shiny.Mediator.SourceGenerators\Shiny.Mediator.SourceGenerators.csproj"
ReferenceOutputAssembly="false"
OutputItemType="Analyzer" />
</ItemGroup>
</Project>
42 changes: 42 additions & 0 deletions tests/Shiny.Mediator.Tests/SourceGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// using FluentAssertions;
// using Shiny.Mediator;
//
// namespace Shiny.Mediator.Tests;
//
// public class SourceGeneratorTests
// {
// [Fact]
// public void DidRegister()
// {
// var services = new ServiceCollection();
// services.AddShinyMediator();
// // TODO: source gen call
// var sp = services.BuildServiceProvider();
//
// sp.GetService<IEventHandler<SourceGenEvent>>().Should().NotBeNull("Event Handler not found");
// sp.GetService<IRequestHandler<SourceGenRequest>>().Should().NotBeNull("Request Handler not found");
// sp.GetService<IRequestHandler<SourceGenResponseRequest, SourceGenResponse>>().Should().NotBeNull("Request/Response Handler not found");
// }
// }
//
// public record SourceGenRequest : IRequest;
// public record SourceGenResponseRequest : IRequest<SourceGenResponse>;
// public record SourceGenResponse;
// public record SourceGenEvent : IEvent;
//
// [RegisterHandler]
// public class SourceGenRequestHandler : IRequestHandler<SourceGenRequest>
// {
// public Task Handle(SourceGenRequest request, CancellationToken cancellationToken) => Task.CompletedTask;
// }
// [RegisterHandler]
// public class SourceGenResponseRequestHandler : IRequestHandler<SourceGenResponseRequest, SourceGenResponse>
// {
// public Task<SourceGenResponse> Handle(SourceGenResponseRequest request, CancellationToken cancellationToken)
// => Task.FromResult(new SourceGenResponse());
// }
// [RegisterHandler]
// public class SourceGenEventHandler : IEventHandler<SourceGenEvent>
// {
// public Task Handle(SourceGenEvent @event, CancellationToken cancellationToken) => Task.CompletedTask;
// }

0 comments on commit 23ac3ba

Please sign in to comment.