Skip to content
Open
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
42 changes: 24 additions & 18 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
<Project>
<!-- https://learn.microsoft.com/nuget/consume-packages/central-package-management -->
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="AspNetCore.SignalR.OpenTelemetry" Version="1.6.0" />
<PackageVersion Include="TypedSignalR.Client" Version="3.6.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageVersion Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.13" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.14" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.14" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageVersion Include="xunit" Version="2.9.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
</ItemGroup>
</Project>
<!-- https://learn.microsoft.com/nuget/consume-packages/central-package-management -->
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="AspNetCore.SignalR.OpenTelemetry" Version="1.6.0" />
<PackageVersion Include="Microsoft.AspNetCore.SignalR.Core" Version="1.2.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" Version="1.1.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.8.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.8.0" />
<PackageVersion Include="TypedSignalR.Client" Version="3.6.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageVersion Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.13" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.14" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.14" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageVersion Include="xunit" Version="2.9.3" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageVersion Include="coverlet.collector" Version="6.0.4" />
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions TypedSignalR.Client.sln
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Directory.Packages.props = Directory.Packages.props
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TypedSignalR.Client.SourceGeneratorTests", "tests\TypedSignalR.Client.SourceGeneratorTests\TypedSignalR.Client.SourceGeneratorTests.csproj", "{547B45CB-8CAD-6B6B-5E80-88AE3B93A43F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -86,6 +88,10 @@ Global
{4AD46B59-DB78-465F-BF10-F0F74B34722D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4AD46B59-DB78-465F-BF10-F0F74B34722D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4AD46B59-DB78-465F-BF10-F0F74B34722D}.Release|Any CPU.Build.0 = Release|Any CPU
{547B45CB-8CAD-6B6B-5E80-88AE3B93A43F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{547B45CB-8CAD-6B6B-5E80-88AE3B93A43F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{547B45CB-8CAD-6B6B-5E80-88AE3B93A43F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{547B45CB-8CAD-6B6B-5E80-88AE3B93A43F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -102,6 +108,7 @@ Global
{0628FEE3-2C6E-4C02-A398-4E55781A5A85} = {2F614DC6-F5D9-4872-9B4E-70F39B02D6EC}
{4D174400-02DF-4816-8CFC-A94DB3DBBBFA} = {2F614DC6-F5D9-4872-9B4E-70F39B02D6EC}
{4AD46B59-DB78-465F-BF10-F0F74B34722D} = {196DB73F-4D3E-4A90-AE48-40925CB33560}
{547B45CB-8CAD-6B6B-5E80-88AE3B93A43F} = {2F614DC6-F5D9-4872-9B4E-70F39B02D6EC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3C64E712-2479-4BF8-9169-B21DC3E787EC}
Expand Down
11 changes: 11 additions & 0 deletions src/TypedSignalR.Client/CodeAnalysis/MethodMetadata.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis;

Expand All @@ -12,6 +13,7 @@ public sealed class MethodMetadata
public IReadOnlyList<ParameterMetadata> Parameters { get; }

public string MethodName { get; }
public string SignalRMethodName { get; }
public string ReturnType { get; }
public bool IsGenericReturnType { get; }
public string? GenericReturnTypeArgument { get; }
Expand All @@ -25,6 +27,15 @@ public MethodMetadata(IMethodSymbol methodSymbol)
.Select(x => new ParameterMetadata(x))
.ToArray();

SignalRMethodName =
(
methodSymbol.GetAttributes()
.FirstOrDefault(x => x.AttributeClass?.ToDisplayString() == "Microsoft.AspNetCore.SignalR.HubMethodNameAttribute")
?.ConstructorArguments.SingleOrDefault()
)
?.Value?.ToString()
?? MethodName;

ReturnType = methodSymbol.ReturnType.ToDisplayString(SymbolDisplayFormatRule.FullyQualifiedNullableReferenceTypeFormat);

INamedTypeSymbol? returnTypeSymbol = methodSymbol.ReturnType as INamedTypeSymbol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private string CreateRegistrationString(TypeMetadata receiverType)
private string CreateRegistrationStringCore(MethodMetadata method)
{
return $$"""
compositeDisposable.Add(global::Microsoft.AspNetCore.SignalR.Client.HubConnectionExtensions.On(connection, nameof(receiver.{{method.MethodName}}), {{method.CreateParameterTypeArrayString(_specialSymbols)}}, HandlerConverter.Convert{{method.CreateTypeArgumentsStringForHandlerConverter(_specialSymbols)}}(receiver.{{method.MethodName}})));
compositeDisposable.Add(global::Microsoft.AspNetCore.SignalR.Client.HubConnectionExtensions.On(connection, "{{method.SignalRMethodName}}", {{method.CreateParameterTypeArrayString(_specialSymbols)}}, HandlerConverter.Convert{{method.CreateTypeArgumentsStringForHandlerConverter(_specialSymbols)}}(receiver.{{method.MethodName}})));
""";
}
}
12 changes: 6 additions & 6 deletions src/TypedSignalR.Client/Templates/MethodMetadataExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ private static string CreateUnaryMethodString(MethodMetadata method)
return $$"""
public {{method.ReturnType}} {{method.MethodName}}({{method.CreateParametersString()}})
{
return global::Microsoft.AspNetCore.SignalR.Client.HubConnectionExtensions.InvokeCoreAsync{{method.CreateGenericReturnTypeArgumentString()}}(_connection, nameof({{method.MethodName}}), {{method.CreateArgumentsString()}}, _cancellationToken);
return global::Microsoft.AspNetCore.SignalR.Client.HubConnectionExtensions.InvokeCoreAsync{{method.CreateGenericReturnTypeArgumentString()}}(_connection, "{{method.SignalRMethodName}}", {{method.CreateArgumentsString()}}, _cancellationToken);
}
""";
}
Expand All @@ -288,7 +288,7 @@ private static string CreateServerStreamingMethodAsAsyncEnumerableString(MethodM
return $$"""
public {{method.ReturnType}} {{method.MethodName}}({{method.CreateParametersString()}})
{
return _connection.StreamAsyncCore{{method.CreateGenericReturnTypeArgumentString()}}(nameof({{method.MethodName}}), {{method.CreateArgumentsStringExceptCancellationToken(specialSymbols)}}, {{method.CreateCancellationTokenString("_cancellationToken", specialSymbols)}});
return _connection.StreamAsyncCore{{method.CreateGenericReturnTypeArgumentString()}}("{{method.SignalRMethodName}}", {{method.CreateArgumentsStringExceptCancellationToken(specialSymbols)}}, {{method.CreateCancellationTokenString("_cancellationToken", specialSymbols)}});
}
""";
}
Expand All @@ -298,7 +298,7 @@ private static string CreateServerStreamingMethodAsTaskAsyncEnumerableString(Met
return $$"""
public {{method.ReturnType}} {{method.MethodName}}({{method.CreateParametersString()}})
{
var ret = _connection.StreamAsyncCore{{method.CreateGenericReturnTypeArgumentStringForStreaming()}}(nameof({{method.MethodName}}), {{method.CreateArgumentsStringExceptCancellationToken(specialSymbols)}}, {{method.CreateCancellationTokenString("_cancellationToken", specialSymbols)}});
var ret = _connection.StreamAsyncCore{{method.CreateGenericReturnTypeArgumentStringForStreaming()}}("{{method.SignalRMethodName}}", {{method.CreateArgumentsStringExceptCancellationToken(specialSymbols)}}, {{method.CreateCancellationTokenString("_cancellationToken", specialSymbols)}});
return global::System.Threading.Tasks.Task.FromResult(ret);
}
""";
Expand All @@ -309,7 +309,7 @@ private static string CreateServerStreamingMethodAsChannelString(MethodMetadata
return $$"""
public {{method.ReturnType}} {{method.MethodName}}({{method.CreateParametersString()}})
{
return global::Microsoft.AspNetCore.SignalR.Client.HubConnectionExtensions.StreamAsChannelCoreAsync{{method.CreateGenericReturnTypeArgumentStringForStreaming()}}(_connection, nameof({{method.MethodName}}), {{method.CreateArgumentsStringExceptCancellationToken(specialSymbols)}}, {{method.CreateCancellationTokenString("_cancellationToken", specialSymbols)}});
return global::Microsoft.AspNetCore.SignalR.Client.HubConnectionExtensions.StreamAsChannelCoreAsync{{method.CreateGenericReturnTypeArgumentStringForStreaming()}}(_connection, "{{method.SignalRMethodName}}", {{method.CreateArgumentsStringExceptCancellationToken(specialSymbols)}}, {{method.CreateCancellationTokenString("_cancellationToken", specialSymbols)}});
}
""";
}
Expand All @@ -319,7 +319,7 @@ private static string CreateClientStreamingMethodAsAsyncEnumerableString(MethodM
return $$"""
public {{method.ReturnType}} {{method.MethodName}}({{method.CreateParametersString()}})
{
return _connection.SendCoreAsync(nameof({{method.MethodName}}), {{method.CreateArgumentsString()}}, _cancellationToken);
return _connection.SendCoreAsync("{{method.SignalRMethodName}}", {{method.CreateArgumentsString()}}, _cancellationToken);
}
""";
}
Expand All @@ -330,7 +330,7 @@ private static string CreateClientStreamingMethodAsChannelString(MethodMetadata

public {{method.ReturnType}} {{method.MethodName}}({{method.CreateParametersString()}})
{
return _connection.SendCoreAsync(nameof({{method.MethodName}}), {{method.CreateArgumentsString()}}, _cancellationToken);
return _connection.SendCoreAsync("{{method.SignalRMethodName}}", {{method.CreateArgumentsString()}}, _cancellationToken);
}
""";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Xunit;

namespace TypedSignalR.Client.SourceGeneratorTests;

using System.Collections.Generic;
using global::TypedSignalR.Client;

public static class CompilationHelper
{
public static (ImmutableArray<Diagnostic> diagnostics, Dictionary<string, string> outputs) GetGeneratedOutput(string source)
{
const LanguageVersion LanguageVersion = LanguageVersion.CSharp12;

var generator = new SourceGenerator();

var sourceSyntaxTree = CSharpSyntaxTree.ParseText(source, path: "source.cs");
var parsingOptions = new CSharpParseOptions(LanguageVersion);
var sourceSyntaxTreeWithOptions = sourceSyntaxTree.WithRootAndOptions(sourceSyntaxTree.GetRoot(), parsingOptions);

var references = AppDomain.CurrentDomain.GetAssemblies()
.Where(assembly => !assembly.IsDynamic && !string.IsNullOrWhiteSpace(assembly.Location))
.Select(assembly => MetadataReference.CreateFromFile(assembly.Location))
.Concat([
MetadataReference.CreateFromFile(generator.GetType().Assembly.Location),
MetadataReference.CreateFromFile(typeof(System.Threading.Tasks.Task).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Microsoft.AspNetCore.SignalR.HubMethodNameAttribute).Assembly.Location),
MetadataReference.CreateFromFile(typeof(Microsoft.AspNetCore.SignalR.Client.HubConnection).Assembly.Location),
MetadataReference.CreateFromFile(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute).Assembly.Location),
MetadataReference.CreateFromFile(typeof(System.CodeDom.Compiler.GeneratedCodeAttribute).Assembly.Location)
]);

var compilation = CSharpCompilation.Create(
"generator",
[sourceSyntaxTreeWithOptions],
references,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));


GeneratorDriver driver =
CSharpGeneratorDriver.Create(
[generator.AsSourceGenerator()],
driverOptions: new GeneratorDriverOptions(
disabledOutputs: IncrementalGeneratorOutputKind.None,
trackIncrementalGeneratorSteps: true),
optionsProvider: null,
parseOptions: new CSharpParseOptions(LanguageVersion));

driver = driver.RunGenerators(compilation);

var runResult = driver.GetRunResult();

return (runResult.Diagnostics, runResult.Results.SelectMany(r => r.GeneratedSources).ToDictionary(s => s.HintName, s => s.SourceText.ToString()));
}
}
Loading