Disable generation of header files if no targets are present

This commit is contained in:
Sergio Pedri 2022-01-14 14:19:48 +01:00
Родитель eff71685c7
Коммит c38a4a2118
3 изменённых файлов: 26 добавлений и 2 удалений

Просмотреть файл

@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
using CommunityToolkit.Mvvm.SourceGenerators.Input.Models;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
@ -42,7 +43,7 @@ public sealed partial class ObservableValidatorValidateAllPropertiesGenerator :
.Select(static (item, _) => item.Length > 0);
// Generate the header file with the attributes
context.RegisterImplementationSourceOutput(isHeaderFileNeeded, static (context, item) =>
context.RegisterConditionalImplementationSourceOutput(isHeaderFileNeeded, static context =>
{
CompilationUnitSyntax compilationUnit = Execute.GetSyntax();

Просмотреть файл

@ -2,6 +2,7 @@
// 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;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
@ -62,4 +63,25 @@ internal static class IncrementalGeneratorInitializationContextExtensions
.Where(static item => item.IsGeneratorSupported)
.Select(static (item, _) => item.Data);
}
/// <summary>
/// Conditionally invokes <see cref="IncrementalGeneratorInitializationContext.RegisterImplementationSourceOutput{TSource}(IncrementalValueProvider{TSource}, System.Action{SourceProductionContext, TSource})"/>
/// if the value produced by the input <see cref="IncrementalValueProvider{TValue}"/> is <see langword="true"/>.
/// </summary>
/// <param name="context">The input <see cref="IncrementalGeneratorInitializationContext"/> value being used.</param>
/// <param name="source">The source <see cref="IncrementalValueProvider{TValues}"/> instance.</param>
/// <param name="action">The conditional <see cref="Action{T}"/> to invoke.</param>
public static void RegisterConditionalImplementationSourceOutput(
this IncrementalGeneratorInitializationContext context,
IncrementalValueProvider<bool> source,
Action<SourceProductionContext> action)
{
context.RegisterImplementationSourceOutput(source, (context, flag) =>
{
if (flag)
{
action(context);
}
});
}
}

Просмотреть файл

@ -5,6 +5,7 @@
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
using CommunityToolkit.Mvvm.SourceGenerators.Input.Models;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
@ -48,7 +49,7 @@ public sealed partial class IMessengerRegisterAllGenerator : IIncrementalGenerat
.Select(static (item, _) => item.Length > 0);
// Generate the header file with the attributes
context.RegisterImplementationSourceOutput(isHeaderFileNeeded, static (context, item) =>
context.RegisterConditionalImplementationSourceOutput(isHeaderFileNeeded, static context =>
{
CompilationUnitSyntax compilationUnit = Execute.GetSyntax();