diff --git a/src/Burkus.Mvvm.Maui.SourceGenerators/AppReceiver.cs b/src/Burkus.Mvvm.Maui.SourceGenerators/AppReceiver.cs index b0c4872..47e61d6 100644 --- a/src/Burkus.Mvvm.Maui.SourceGenerators/AppReceiver.cs +++ b/src/Burkus.Mvvm.Maui.SourceGenerators/AppReceiver.cs @@ -1,5 +1,6 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis; +using System.Collections.Generic; namespace Burkus.Mvvm.Maui; @@ -8,16 +9,16 @@ namespace Burkus.Mvvm.Maui; /// internal class AppReceiver : ISyntaxReceiver { - public ClassDeclarationSyntax? AppClass { get; private set; } + public List AppClasses { get; } = new List(); public void OnVisitSyntaxNode(SyntaxNode syntaxNode) { // look for a class declaration named App - if (syntaxNode is ClassDeclarationSyntax classDeclaration && - classDeclaration.Identifier.ValueText == "App") + if (syntaxNode is ClassDeclarationSyntax classDeclaration + && classDeclaration.Identifier.ValueText == "App") { - // store the first one found - AppClass ??= classDeclaration; + // store all the ones found + AppClasses.Add(classDeclaration); } } } diff --git a/src/Burkus.Mvvm.Maui.SourceGenerators/AppSourceGenerator.cs b/src/Burkus.Mvvm.Maui.SourceGenerators/AppSourceGenerator.cs index a4c4a3b..6021a54 100644 --- a/src/Burkus.Mvvm.Maui.SourceGenerators/AppSourceGenerator.cs +++ b/src/Burkus.Mvvm.Maui.SourceGenerators/AppSourceGenerator.cs @@ -21,15 +21,28 @@ public void Execute(GeneratorExecutionContext context) if (context.SyntaxReceiver is not AppReceiver receiver) return; - var semanticModel = context.Compilation.GetSemanticModel(receiver.AppClass.SyntaxTree); - var isPartial = receiver.AppClass.Modifiers + INamedTypeSymbol chosenAppSymbol = null; + + foreach (var appClass in receiver.AppClasses) + { + var semanticModel = context.Compilation.GetSemanticModel(appClass.SyntaxTree); + + var isPartial = appClass.Modifiers .Any(m => m.IsKind(SyntaxKind.PartialKeyword)); - // check if the App class is partial and inherits from Application - var appSymbol = semanticModel.GetDeclaredSymbol(receiver.AppClass); - if (appSymbol is null || !isPartial || !appSymbol.BaseType.Equals(semanticModel.Compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.Application"))) + // check if the App class is partial and inherits from Application + var appSymbol = semanticModel.GetDeclaredSymbol(appClass); + + if (appSymbol is not null && isPartial && appSymbol.BaseType.Equals(semanticModel.Compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.Application"))) + { + chosenAppSymbol = appSymbol; + break; + } + } + + if (chosenAppSymbol == null) { - throw new Exception("You must have a partial class called \"App\" that inherits from Application in your .NET MAUI project."); + return; } // get the MAUI program we are running in diff --git a/tests/DemoApp.UnitTests/DemoApp.UnitTests.csproj b/tests/DemoApp.UnitTests/DemoApp.UnitTests.csproj index 32ac1d2..2eed6a4 100644 --- a/tests/DemoApp.UnitTests/DemoApp.UnitTests.csproj +++ b/tests/DemoApp.UnitTests/DemoApp.UnitTests.csproj @@ -12,9 +12,9 @@ - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all