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

Expose MagicOnionClientGenerationAttribute from MagicOnion.Client #889

Merged
merged 6 commits into from
Jan 7, 2025
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 0 additions & 4 deletions samples/ChatApp/ChatApp.Console/ChatApp.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\ChatApp.Unity\Assets\Scripts\Generated\MessagePack.Generated.cs" Link="MessagePack.Generated.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\MagicOnion.Client.SourceGenerator\MagicOnion.Client.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\..\src\MagicOnion.Client\MagicOnion.Client.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion samples/ChatApp/ChatApp.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
BuiltinResolver.Instance,
PrimitiveObjectResolver.Instance,
MagicOnionGeneratedClientInitializer.Resolver,
MessagePack.Resolvers.GeneratedResolver.Instance
StandardResolver.Instance
);
MessagePackSerializer.DefaultOptions = MessagePackSerializer.DefaultOptions.WithResolver(StaticCompositeResolver.Instance);
}
Expand Down
12 changes: 0 additions & 12 deletions samples/ChatApp/ChatApp.Shared/ChatApp.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,4 @@
<ItemGroup>
<ProjectReference Include="..\..\..\src\MagicOnion.Abstractions\MagicOnion.Abstractions.csproj" />
</ItemGroup>

<Target Name="RestoreLocalTools" BeforeTargets="GenerateMessagePack">
<Exec Command="dotnet tool restore" />
</Target>

<Target Name="GenerateMessagePack" AfterTargets="Build">
<PropertyGroup>
<_MessagePackGeneratorArguments>-i ./ChatApp.Shared.csproj -o ../ChatApp.Unity/Assets/Scripts/Generated/MessagePack.Generated.cs</_MessagePackGeneratorArguments>
</PropertyGroup>
<Exec Command="dotnet tool run mpc $(_MessagePackGeneratorArguments)" />
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,68 +9,12 @@ namespace MagicOnion.Client.SourceGenerator;

public partial class MagicOnionClientSourceGenerator
{
public const string SourceGeneratorAttributesHintName = "MagicOnionClientSourceGeneratorAttributes.g.cs";
public const string MagicOnionClientGenerationAttributeShortName = "MagicOnionClientGeneration";
public const string MagicOnionClientGenerationAttributeName = $"{MagicOnionClientGenerationAttributeShortName}Attribute";
public const string MagicOnionClientGenerationAttributeFullName = $"MagicOnion.Client.{MagicOnionClientGenerationAttributeName}";

static class Emitter
{
public static void AddAttributeSources(Action<string, string> addSource)
{
addSource(SourceGeneratorAttributesHintName, $$"""
// <auto-generated />
namespace MagicOnion.Client
{
/// <summary>
/// Marker attribute for generating clients of MagicOnion.
/// The source generator collects the classes specified by this attribute and uses them to generate source.
/// </summary>
[global::System.Diagnostics.Conditional("__MagicOnion_Client_SourceGenerator__DesignTimeOnly__")]
[global::System.AttributeUsage(global::System.AttributeTargets.Class, AllowMultiple = false)]
internal class {{MagicOnionClientGenerationAttributeName}} : global::System.Attribute
{
/// <summary>
/// Gets or sets whether to disable automatically calling `Register` during start-up. (Automatic registration requires .NET 5+ or Unity)
/// </summary>
public bool DisableAutoRegistration { get; set; }

/// <summary>
/// Gets or set the serializer used for message serialization. The default value is <see cref="GenerateSerializerType.MessagePack"/>.
/// </summary>
public global::MagicOnion.Client.{{MagicOnionClientGenerationAttributeName}}.GenerateSerializerType Serializer { get; set; } = global::MagicOnion.Client.{{MagicOnionClientGenerationAttributeName}}.GenerateSerializerType.MessagePack;

/// <summary>
/// Gets or set the namespace of pre-generated MessagePackFormatters. The default value is <c>MessagePack.Formatters</c>.
/// </summary>
public string MessagePackFormatterNamespace { get; set; } = "MessagePack.Formatters";

/// <summary>
/// Gets or set whether to enable the StreamingHandler diagnostic handler. This is for debugging purpose. The default value is <see langword="false" />.
/// </summary>
public bool EnableStreamingHubDiagnosticHandler { get; set; } = false;

public string GenerateFileHintNamePrefix { get; set; } = string.Empty;

public global::System.Type[] TypesContainedInTargetAssembly { get; }

/// <param name="typesContainedInTargetAssembly">Types contained in the scan target assembly</param>
public {{MagicOnionClientGenerationAttributeName}}(params global::System.Type[] typesContainedInTargetAssembly)
{
TypesContainedInTargetAssembly = typesContainedInTargetAssembly;
}

// This enum must be mirror of `SerializerType` (MagicOnionClientSourceGenerator)
internal enum GenerateSerializerType
{
MessagePack = 0,
MemoryPack = 1,
}
}
}
""");
}

public static void Emit(GenerationContext context, ImmutableArray<INamedTypeSymbol> interfaceSymbols, ReferenceSymbols referenceSymbols)
{
var (serviceCollection, diagnostics) = MethodCollector.Collect(interfaceSymbols, referenceSymbols, context.SourceProductionContext.CancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
predicate: static (node, cancellationToken) => node is ClassDeclarationSyntax,
transform: static (ctx, cancellationToken) => ((ClassDeclarationSyntax)ctx.TargetNode, ctx.Attributes, ctx.SemanticModel));

context.RegisterPostInitializationOutput(static context => Emitter.AddAttributeSources(context.AddSource));

context.RegisterSourceOutput(generationAttr.Combine(referenceSymbols), static (sourceProductionContext, value) =>
{
var ((initializerClassDecl, attrs, semanticModel), referenceSymbols) = value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// <auto-generated />
using System;
using System.Collections.Generic;
using System.Text;

namespace MagicOnion.Client
{
/// <summary>
/// Marker attribute for generating clients of MagicOnion.
/// The source generator collects the classes specified by this attribute and uses them to generate source.
/// </summary>
[global::System.Diagnostics.Conditional("__MagicOnion_Client_SourceGenerator__DesignTimeOnly__")]
[global::System.AttributeUsage(global::System.AttributeTargets.Class, AllowMultiple = false)]
internal class MagicOnionClientGenerationAttribute : global::System.Attribute
[AttributeUsage(global::System.AttributeTargets.Class, AllowMultiple = false)]
public class MagicOnionClientGenerationAttribute : Attribute
{
/// <summary>
/// Gets or sets whether to disable automatically calling `Register` during start-up. (Automatic registration requires .NET 5+ or Unity)
Expand All @@ -17,7 +19,7 @@ internal class MagicOnionClientGenerationAttribute : global::System.Attribute
/// <summary>
/// Gets or set the serializer used for message serialization. The default value is <see cref="GenerateSerializerType.MessagePack"/>.
/// </summary>
public global::MagicOnion.Client.MagicOnionClientGenerationAttribute.GenerateSerializerType Serializer { get; set; } = global::MagicOnion.Client.MagicOnionClientGenerationAttribute.GenerateSerializerType.MessagePack;
public GenerateSerializerType Serializer { get; set; } = GenerateSerializerType.MessagePack;

/// <summary>
/// Gets or set the namespace of pre-generated MessagePackFormatters. The default value is <c>MessagePack.Formatters</c>.
Expand All @@ -31,19 +33,19 @@ internal class MagicOnionClientGenerationAttribute : global::System.Attribute

public string GenerateFileHintNamePrefix { get; set; } = string.Empty;

public global::System.Type[] TypesContainedInTargetAssembly { get; }
public Type[] TypesContainedInTargetAssembly { get; }

/// <param name="typesContainedInTargetAssembly">Types contained in the scan target assembly</param>
public MagicOnionClientGenerationAttribute(params global::System.Type[] typesContainedInTargetAssembly)
public MagicOnionClientGenerationAttribute(params Type[] typesContainedInTargetAssembly)
{
TypesContainedInTargetAssembly = typesContainedInTargetAssembly;
}

// This enum must be mirror of `SerializerType` (MagicOnionClientSourceGenerator)
internal enum GenerateSerializerType
public enum GenerateSerializerType
{
MessagePack = 0,
MemoryPack = 1,
}
}
}
}
16 changes: 16 additions & 0 deletions src/MagicOnion.Client/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ MagicOnion.Client.MagicOnionClientBase<T>
MagicOnion.Client.MagicOnionClientBase<T>.MagicOnionClientBase(MagicOnion.Client.MagicOnionClientOptions! options) -> void
MagicOnion.Client.MagicOnionClientFactoryDelegate<T>
MagicOnion.Client.MagicOnionClientFactoryProvider
MagicOnion.Client.MagicOnionClientGenerationAttribute
MagicOnion.Client.MagicOnionClientGenerationAttribute.DisableAutoRegistration.get -> bool
MagicOnion.Client.MagicOnionClientGenerationAttribute.DisableAutoRegistration.set -> void
MagicOnion.Client.MagicOnionClientGenerationAttribute.EnableStreamingHubDiagnosticHandler.get -> bool
MagicOnion.Client.MagicOnionClientGenerationAttribute.EnableStreamingHubDiagnosticHandler.set -> void
MagicOnion.Client.MagicOnionClientGenerationAttribute.GenerateFileHintNamePrefix.get -> string!
MagicOnion.Client.MagicOnionClientGenerationAttribute.GenerateFileHintNamePrefix.set -> void
MagicOnion.Client.MagicOnionClientGenerationAttribute.GenerateSerializerType
MagicOnion.Client.MagicOnionClientGenerationAttribute.GenerateSerializerType.MemoryPack = 1 -> MagicOnion.Client.MagicOnionClientGenerationAttribute.GenerateSerializerType
MagicOnion.Client.MagicOnionClientGenerationAttribute.GenerateSerializerType.MessagePack = 0 -> MagicOnion.Client.MagicOnionClientGenerationAttribute.GenerateSerializerType
MagicOnion.Client.MagicOnionClientGenerationAttribute.MagicOnionClientGenerationAttribute(params System.Type![]! typesContainedInTargetAssembly) -> void
MagicOnion.Client.MagicOnionClientGenerationAttribute.MessagePackFormatterNamespace.get -> string!
MagicOnion.Client.MagicOnionClientGenerationAttribute.MessagePackFormatterNamespace.set -> void
MagicOnion.Client.MagicOnionClientGenerationAttribute.Serializer.get -> MagicOnion.Client.MagicOnionClientGenerationAttribute.GenerateSerializerType
MagicOnion.Client.MagicOnionClientGenerationAttribute.Serializer.set -> void
MagicOnion.Client.MagicOnionClientGenerationAttribute.TypesContainedInTargetAssembly.get -> System.Type![]!
MagicOnion.Client.MagicOnionClientOptions
MagicOnion.Client.MagicOnionClientOptions.CallInvoker.get -> Grpc.Core.CallInvoker!
MagicOnion.Client.MagicOnionClientOptions.CallOptions.get -> Grpc.Core.CallOptions
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading