Skip to content

Commit

Permalink
Add '[GeneratedAppServiceHost]' attribute type
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Jan 19, 2025
1 parent f68b369 commit 70c49d1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
});

// Gather all interfaces, and only enable this branch if the target is a UWP app (the host)
IncrementalValuesProvider<(HierarchyInfo Hierarchy, AppServiceInfo Info)> appServiceHostInfo =
IncrementalValuesProvider<(HierarchyInfo, AppServiceInfo)> appServiceHostInfo =
context.ForAttributeWithMetadataNameAndOptions(
"CommunityToolkit.AppServices.AppServiceAttribute",
static (node, _) => node is InterfaceDeclarationSyntax,
Expand All @@ -105,6 +105,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

token.ThrowIfCancellationRequested();

// Gather all methods for the app service type
ImmutableArray<MethodInfo> methods = MethodInfo.From(typeSymbol, token);

token.ThrowIfCancellationRequested();
Expand All @@ -113,8 +114,47 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
})
.Where(static item => item.Hierarchy is not null);

// Produce the host type
context.RegisterSourceOutput(appServiceHostInfo, static (context, item) =>
// Also gather all explicitly requested host implementation types
IncrementalValuesProvider<(HierarchyInfo, AppServiceInfo)> additionalAppServiceHostInfo =
context.ForAttributeWithMetadataNameAndOptions(
"CommunityToolkit.AppServices.GeneratedAppServiceHostAttribute",
static (node, _) => true,
static (context, token) =>
{
// Only retrieve host info if the target is a UWP application
if (!Helpers.IsUwpTarget(context.SemanticModel.Compilation, context.GlobalOptions))
{
return default;
}

// Get the target interface
if (context.Attributes[0].ConstructorArguments is not [{ Kind: TypedConstantKind.Type, Value: INamedTypeSymbol appServiceType }])
{
return default;
}

// Check if the current interface is in fact an app service type
if (!appServiceType.TryGetAppServicesNameFromAttribute(out string? appServiceName))
{
return default;
}

token.ThrowIfCancellationRequested();

HierarchyInfo hierarchy = HierarchyInfo.From(appServiceType, appServiceType.Name.Substring(1));

token.ThrowIfCancellationRequested();

ImmutableArray<MethodInfo> methods = MethodInfo.From(appServiceType, token);

token.ThrowIfCancellationRequested();

return (Hierarchy: hierarchy, new AppServiceInfo(methods, appServiceName, appServiceType.GetFullyQualifiedName()));
})
.Where(static item => item.Hierarchy is not null);

// Shared helper to emit all discovered types
static void GenerateAppServiceHostType(SourceProductionContext context, (HierarchyInfo Hierarchy, AppServiceInfo Info) item)
{
ConstructorDeclarationSyntax constructorSyntax = Host.GetConstructorSyntax(item.Hierarchy, item.Info);
ImmutableArray<MethodDeclarationSyntax> methodDeclarations = Host.GetMethodDeclarationsSyntax(item.Info);
Expand All @@ -126,6 +166,10 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
$"/// <summary>A generated host implementation for the <see cref=\"{item.Info.InterfaceFullyQualifiedName}\"/> interface.</summary>");

context.AddSource($"{item.Hierarchy.FilenameHint}.g.cs", compilationUnit.GetText(Encoding.UTF8));
});
}

// Produce the host types
context.RegisterSourceOutput(appServiceHostInfo, GenerateAppServiceHostType);
context.RegisterSourceOutput(additionalAppServiceHostInfo, GenerateAppServiceHostType);
}
}
28 changes: 28 additions & 0 deletions components/AppServices/src/GeneratedAppServiceHostAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

namespace CommunityToolkit.AppServices;

/// <summary>
/// An attribute that can be used to request the generator to emit a host implementation of a given app service.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false, Inherited = false)]
public sealed class GeneratedAppServiceHostAttribute : Attribute
{
/// <summary>
/// Creates a new <see cref="GeneratedAppServiceHostAttribute"/> instance with the specified parameters.
/// </summary>
/// <param name="appServiceType">The type of the app service.</param>
public GeneratedAppServiceHostAttribute(Type appServiceType)
{
AppServiceType = appServiceType;
}

/// <summary>
/// Gets the type of the app service.
/// </summary>
public Type AppServiceType { get; }
}

0 comments on commit 70c49d1

Please sign in to comment.