Skip to content

Commit

Permalink
Merge pull request #92 from CommunityToolkit/dev/remove-leftover-head…
Browse files Browse the repository at this point in the history
…-files

Disable generation of header files if no targets are present
  • Loading branch information
Sergio0694 authored Jan 14, 2022
2 parents eff7168 + c38a4a2 commit f2cfa3a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,7 +43,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -62,4 +63,25 @@ public static void FilterWithLanguageVersion<T>(
.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);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,7 +49,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.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();
Expand Down

0 comments on commit f2cfa3a

Please sign in to comment.